CodeForces

题意:已知n,问满足条件"x的各个数字之和+x=n"的x有几个并按升序输出。

分析:

1、n最大1e9,10位数,假设每一位都为9的话,可知x的各个数字之和最大可以贡献90。

2、枚举n-90~90即可。

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define lowbit(x) (x & (-x))
const double eps = 1e-8;
inline int dcmp(double a, double b){
    if(fabs(a - b) < eps) return 0;
    return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 10000 + 10;
const int MAXT = 10000 + 10;
using namespace std;
char s[20];
vector<int> ans;
int main(){
    int n;
    scanf("%d", &n);
    int cnt = 0;
    for(int i = n - 90; i <= n; ++i){
        if(i < 0) continue;
        sprintf(s, "%d", i);
        int len = strlen(s);
        int sum = i;
        for(int j = 0; j < len; ++j){
            sum += s[j] - '0';
        }
        if(sum == n){
            ans.push_back(i);
        }
    }
    int l = ans.size();
    printf("%d
", l);
    for(int i = 0; i < l; ++i){
        if(i) printf(" ");
        printf("%d", ans[i]);
    }
    printf("
");
    return 0;
}

  

原文地址:https://www.cnblogs.com/tyty-Somnuspoppy/p/7688548.html