Codeforces 876C Classroom Watch:枚举

题目链接:http://codeforces.com/contest/876/problem/C

题意:

  定义函数:f(x) = x + 十进制下x各位上的数字之和

  给你f(x)的值(f(x) <= 1e9),让你输出所有可能的x值。

题解:

  部分枚举。

  考虑可能的x的范围:

    ∵ x < f(x)

    ∴ 十进制下x各位上的数字之和 < 9*9 ≈ 100

  所以x枚举[f(x)-100, f(x)]之间的数就好了。

AC Code:

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string.h>
 4 #include <vector>
 5 
 6 using namespace std;
 7 
 8 int n;
 9 vector<int> v;
10 
11 int main()
12 {
13     cin>>n;
14     for(int i=max(1,n-100);i<n;i++)
15     {
16         int sum=0;
17         int t=i;
18         while(t)
19         {
20             sum+=t%10;
21             t/=10;
22         }
23         if(sum+i==n) v.push_back(i);
24     }
25     cout<<v.size()<<endl;
26     for(int i=0;i<v.size();i++)
27     {
28         cout<<v[i]<<endl;
29     }
30 }
原文地址:https://www.cnblogs.com/Leohh/p/7679068.html