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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值