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然后让你在1-n中找所有的x(x要求为x+x的每一个位数的答案为n)
看一下数据1e9一个个for过去绝对超时,然后想到n最大为99999999(9个9)9*9=81;所以一个数最多只能+81;然后就只要从n-81这个数开始枚举就行,当然如果n小于81就从0开始枚举(真是神题)
代码:
#include <iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;
int a[110000];
int main()
{
int n,sum,tmp=0;
cin>>n;
for(int i=max(n-81,0);i<=n;i++){
sum=i;
int num=i;
while(num){
sum+=num%10;
num/=10;
}
if(sum==n){
a[tmp++]=i;
}
}cout<<tmp<<endl;
if(tmp){
for(int i=0;i<tmp;i++)
cout<<a[i]<<endl;
}
return 0;
}