UVA11371 Number Theory for Newbies水题

Posted 海岛Blog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVA11371 Number Theory for Newbies水题相关的知识,希望对你有一定的参考价值。

Given any positive integer, if we permute its digits, the difference between the number we get and the given number will always be divisible by 9.
For example, if the given number is 123, we may rearrange the digits to get 321. The difference = 321 - 123 = 198, which is a multiple of 9 (198 = 9 × 22).
We can prove this fact fairly easily, but since we are not having a maths contest, we instead try to illustrate this fact with the help of a computer program.
Input
Each line of input gives a positive integer n (≤ 2000000000). You are to find two integers a and b formed by rearranging the digits of n, such that a − b is maximum. a and b should NOT have leading zeros.
Output
You should then show that a − b is a multiple of 9, by expressing it as ‘9 * k’, where k is an integer. See the sample output for the correct output format.
Sample Input
123
2468
Sample Output
321 - 123 = 198 = 9 * 22
8642 - 2468 = 6174 = 9 * 686

问题链接UVA11371 Number Theory for Newbies
问题简述:给定一个正整数n,将其每位数字重新组合成,可以得到整数a和b,使得a-b最大且满足是9的倍数。
问题分析:简单题,不解释。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA11371 Number Theory for Newbies */

#include <bits/stdc++.h>

using namespace std;

char s[10 + 1];

int main()
{
    while (scanf("%s", s) == 1) {
        int len = strlen(s);
        sort(s, s + len);
        for (int i = 0; i < len; i++)
            if (s[i] != '0') {
                swap(s[0], s[i]);
                break;
            }

        long long a, b;
        sscanf(s, "%lld", &b);
        sort(s, s + len);
        for (int i = 0, j = len - 1; i < j; i++, j--)
            swap(s[i], s[j]);
        sscanf(s, "%lld", &a);

        printf("%lld - %lld = %lld = 9 * %lld\\n", a, b, a - b, (a - b) / 9);
    }

    return 0;
}

以上是关于UVA11371 Number Theory for Newbies水题的主要内容,如果未能解决你的问题,请参考以下文章

FZU 2297 Number theory线段树/单点更新/思维

A. Number Theory Problem

[BZOJ4026]dC Loves Number Theory

A1-2017级算法上机第一次练习赛 P ModricWang's Number Theory II

bzoj 4026 dC Loves Number Theory (主席树+数论+欧拉函数)

bzoj4026dC Loves Number Theory 可持久化线段树