Codeforces 919B (Prefect Number)

We consider a positive integer perfect, if and only if the sum of its digits is exactly 10. Given a positive integer k, your task is to find the k-th smallest perfect positive integer.

Input
A single line with a positive integer k (1 ≤ k ≤ 10 000).

Output
A single number, denoting the k-th smallest perfect integer.

Examples
inputCopy
1
output
19
inputCopy
2
output
28
Note
The first perfect integer is 19 and the second one is 28.

题意:

先给出了完美数的定义:一个数的所有数位和为10.如181=1+8+1=10 是完美数。然后输入一个n,问你第n个完美数是什么。

思路:

我也不知道第一遍的时候脑子怎么了,竟然认为从19向上每次加9就成立,确实至少是很多成立的但却是错误的。
然后看了一下数据量和给出的时间,这个题目完全可以暴力判断打表。做这个题的时候自己一定是脑子瓦特了 哈

代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>

using namespace std;
const int N=1e5+10;
long long a[N],b[N];

int f(int x)//判断是否是完美数的函数
{
    int sum=0;
    while(x)
    {
        sum+=x%10;
        x/=10;
        if(sum>10)//这里剪枝一下 这样会省一部分时间
            return 0;
    }
    if(sum==10)
        return 1;
    else
        return 0;
}

int main()
{
    ios::sync_with_stdio(false);
    int k=1;
    for(long long i=19; k<=10010; i++)//打表 这里k是小于10000所以就
    {
        if(f(i)==1)
        {
            a[k++]=i;
        }
    }
    long long n;
    while(cin>>n)
    {
        cout<<a[n]<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Puppet__/article/details/79344723