UVa 11371 number theory for newbies (water ver.)

Source: Internet
Author: User
Tags diff integer time limit

11371-number Theory for Newbies

Time limit:1.000 seconds

Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem &problem=2366

Given any positive integer, if we permute it digits, the difference between the number we get and the Given number would a Lways is divisible by 9. For example, if the given number is 123, we'll rearrange the digits to get 321. The difference = 321-123 = 198, which is a multiple of 9 (198 = 9x22).

We can prove this fact fairly easily, but since we are do not have a maths contest, we instead try to illustrate this fact With the help of a computer program.

Input and Output

Each line of input gives a positive integer n (≤2000000000). You are to find two Integersa andb formed by rearranging the digits ofn, such thata-b is MAXIMUM.A andb-should not have LE Ading Zeros. You are should then show Thata-b are a multiple of 9, by expressing it as 9XK, and Wherek is an integer. The sample output for the correct output format.

Sample Input

123
2468

Sample Output

321-123 = 198 = 9 *
8642-2468 = 6174 = 9 * 686

Long long, grave-Egg!

Complete code:

/*0.015s*/
  
#include <bits/stdc++.h>
using namespace std;
  
Char a[15], b[15];
  
int main ()
{
    int len, I;
    Long long diff;
    while (gets (a))
    {
        len = strlen (a);
        Sort (A, a + Len, greater<char> ());
        memcpy (b, A, sizeof (a));///This is b for
        (i = 0; i < len; ++i) A[i] = b[len-1-i];
        for (i = 0;!) ( A[i] & 15); ++i)
            ;
        Swap (a[0], a[i]);
        diff = Atoll (b)-Atoll (a);
        printf ("%s-%s =%LLD = 9 *%lld\n", B, A, diff, diff/9);
    }
    return 0;
}

Author: csdn Blog Synapse7

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.