(Hdu step 1.2.8) Specialized Four-Digit Numbers (calculate the sum of Numbers on each Digit), hdu1.2.8
Question:
Specialized Four-Digit Numbers |
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) |
Total Submission (s): 2027 Accepted Submission (s): 1349 |
|
Problem DescriptionFind and list all four-digit numbers in decimal notation that have the property that the sum of its four digits equals the sum of its digits when represented in hexadecimal (base 16) notation and also equals the sum of its digits when represented in duodecimal (base 12) notation.
For example, the number 2991 has the sum of (decimal) digits 2 + 9 + 9 + 1 = 21. since 2991 = 1*1728 + 8*144 + 9*12 + 3, its duodecimal representation is 1893 (12), and these digits also sum up to 21. but in hexadecimal 2991 is BAF16, and 11 + 10 + 15 = 36, so 2991 shocould be rejected by your program.
The next number (2992), however, has digits that sum to 22 in all three representations (including BB016), so 2992 shocould be on the listed output. (We don't want decimal numbers with fewer than four digits-excluding leading zeroes-so that 2992 is the first correct answer .) |
InputThere is no input for this problem. |
OutputYour output is to be 2992 and all larger four-digit numbers that satisfy the requirements (in strictly increasing order), each on a separate line with no leading or trailing blanks, ending with a new-line character. there are to be no blank lines in the output. the first few lines of the output are shown below. |
Sample InputThere is no input for this problem. |
Sample Output29922993299429952996299729982999 |
|
SourcePacific Northwest 2004 |
RecommendIgnatius. L |
Question:
Lists All four digits with the same sum of digits in decimal, decimal, and hexadecimal notation.
Question Analysis:
Returns the sum of all digits of a number.
The Code is as follows:
/** I. cpp ** Created on: January 28, 2015 * Author: Administrator */# include <iostream> # include <cstdio> # include <cstdlib> using namespace std; /*** calculate the value of the number on each digit in base notation * num: digit in decimal notation * base: hexadecimal **/int getSum (int num, int base) {char arr [10]; itoa (num, arr, base); // note that itoa () the returned value of this function is greater than 10 and is represented by a. // cout <arr <endl; int sum = 0; int I = 0; while (arr [I]! = '\ 0') {// if the string has not reached the end of if (arr [I]> = 'A ') {// if this is a large bathroom, sum + = arr [I]-'A' + 10; // do not forget + 10} else {// If This Is A number smaller than 10 sum + = arr [I]-'0';} I ++ ;} return sum;} int main () {int I; for (I = 1000; I <10000; ++ I) {if (getSum (I, 10) = getSum (I, 12) & (getSum (I, 10) = getSum (I, 16) {printf ("% d \ n ", i) ;}// printf ("% d \ n", getSum (2992,10); // printf ("% d \ n", getSum (2992,12 )); // printf ("% d \ n", getSum (2992,16); return 0 ;}