CodeForces 875A Classroom Watch

2724 단어 OJ
Description
Eighth-grader Vova is on duty today in the class. After classes, he went into the office to wash the board, and found on it the number n. He asked what is this number and the teacher of mathematics Inna Petrovna answered Vova that n is the answer to the arithmetic task for first-graders. In the textbook, a certain positive integer x was given. The task was to add x to the sum of the digits of the number x written in decimal numeral system.
Since the number n on the board was small, Vova quickly guessed which x could be in the textbook. Now he wants to get a program which will search for arbitrary values of the number n for all suitable values of x or determine that such x does not exist. Write such a program for Vova.
Input
The first line contains integer n (1 ≤ n ≤ 109).
Output
In the first line print one integer k — number of different values of x satisfying the condition.
In next k lines print these values in ascending order.
Example
Input
21

Output
1
15

Input
20

Output
0

Note
In the first test case x = 15 there is only one variant: 15 + 1 + 5 = 21.
In the second test case there are no such x.
제목 대의:
n을 세어 드리겠습니다. x를 찾아서 x에 자신의 모든 자리의 합이 n과 같게 하고 조건에 맞는 x의 개수를 출력하십시오.
문제 해결 방법:
먼저 생각나는 것은 1e9의 데이터량에 대해 폭력적인 해결 문제는 반드시 시간을 초과할 것이다. 그러면 우리는 x에 대해 x의 크기는 n에서 n을 뺀 모든 사람의 합보다 작지 않을 것이라고 생각한다. 왜냐하면 x가 현재의 이 수보다 작다면 이 숫자를 구성할 수 없기 때문이다. 그러면 매번 1에서 n까지 일치하는 숫자의 개수를 찾을 필요가 없다. 
코드:
#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std;/* 
ios::sync_with_stdio(false);
*/typedef long long ll;typedef unsigned long long ull;const int dir[5][2] = {0, 1, 0, -1, 1, 0, -1, 0, 0, 0};const int inf = 0x7fffffff;const int mod = 1000000;const int Max = 1000000;int temp[10], ans[Max];void check(char *s) {     int index = 0, dec, sum = 0, cnt = 0;     dec = atoi(s);     memset(temp, 0, sizeof(temp));     for (int i = 0; i < strlen(s);++i)         sum += s[i];     for (int i = max((dec - sum), 0); i <= dec;++i) {         int temp = i, t = i;         while (temp) {             t += (temp % 10);             temp/= 10;         }         if (t == dec) {             ans[cnt++] = i;         }     }     printf("%d", cnt);     for (int i = 0; i < cnt;++i) {         printf("%d%c", ans[i], i == cnt - 1 ? '' : ' ');     } }int main() {     char num[10];     scanf("%s", num);     check(num);     return 0; }

좋은 웹페이지 즐겨찾기