Codeforces Round #441 (Div. 2, by Moscow Team Olympiad) C. Classroom Watch
地址:http://codeforces.com/contest/876/problem/C
题目:
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.
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.
思路:
分析一下可以发现,x最多是9位数,所以x的每一位数字之和最大也就是100,。
所以直接从n向下枚举100多就可以了。
1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 #define MP make_pair 6 #define PB push_back 7 typedef long long LL; 8 typedef pair<int,int> PII; 9 const double eps=1e-8; 10 const double pi=acos(-1.0); 11 const int K=1e6+7; 12 const int mod=1e9+7; 13 14 int n; 15 vector<int>vc; 16 int main(void) 17 { 18 cin>>n; 19 for(int i=n;i>0&&i>n-200;i--) 20 { 21 int x=i,ls=1e9,sum=i; 22 while(x) 23 sum+=x/ls,x%=ls,ls/=10; 24 if(sum==n) 25 vc.PB(i); 26 } 27 printf("%d\n",vc.size()); 28 reverse(vc.begin(),vc.end()); 29 for(auto &x:vc) 30 printf("%d ",x); 31 return 0; 32 }
作者:weeping
出处:www.cnblogs.com/weeping/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。