codeforces Round #441 C Classroom Watch【枚举/注意起点】
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 xwritten 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.
The first line contains integer n (1 ≤ n ≤ 109).
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.
21
1
15
20
0
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 + x各位数和=n,有多组输出方案数和方案。
【分析】:暴力枚举数位和。注意因为最大就是999999999=81 ,更小的数一定凑不到n, so枚举起点是max(n-81,1)
【代码】:
#include <bits/stdc++.h> using namespace std; int save[100]; int putu(int n,int i) { int p=i; while(p) { i+=p%10; p/=10; } return i==n; } int main(void) { int n,ans=0; scanf("%d",&n); for(int i=max(n-81,0);i<=n;i++) { if(putu(n,i)) { save[ans++]=i; } } printf("%d\n",ans ); if(ans>0) { for(int i=0;i<ans;i++) printf("%d\n",save[i] ); } return 0; }