[规律]Snuke Numbers

题目描述
Let S(n) denote the sum of the digits in the decimal notation of n. For example, S(123)=1+2+3=6.
We will call an integer n a Snuke number when, for all positive integers m such that m>n,  holds.
Given an integer K, list the K smallest Snuke numbers.
Constraints
1≤K
The K-th smallest Snuke number is not greater than 1015.
 

 

输入
Input is given from Standard Input in the following format:

K

 

输出
Print K lines. The i-th line should contain the i-th smallest Snuke number.

 

样例输入

复制样例数据

10
样例输出
1
2
3
4
5
6
7
8
9
19

 思路:找到规律如下:

即只需在加到9时判断下一次是加第一位还是第二位

AC代码:

#include <iostream>
#include<cstdio>
typedef long long ll;
using namespace std;

double f(ll x){
  ll sum=0;
  ll cop=x;
  while(x){
    sum+=x%10;
    x/=10;
  }
  return cop*1.0/(sum*1.0);
}

int main()
{
    //printf("%.6f %.6f\n",f(20999),f(21999));
    ll k;scanf("%lld",&k);
    ll tot=0;
    ll now=0,x=1;
    while(tot<k){
        if((now/x)%10!=9){
            now+=x;
            printf("%lld\n",now);tot++;
        }
        else{
            if(f(now+x)>f(now+10*x)){
                x=10*x;
            }
            now+=x;
            printf("%lld\n",now);tot++;
        }
        //printf("tot=%lld \n",tot);
    }
    return 0;
}

转载于:https://www.cnblogs.com/lllxq/p/10041270.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值