zoukankan      html  css  js  c++  java
  • [规律]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
    ",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
    ",now);tot++;
            }
            else{
                if(f(now+x)>f(now+10*x)){
                    x=10*x;
                }
                now+=x;
                printf("%lld
    ",now);tot++;
            }
            //printf("tot=%lld 
    ",tot);
        }
        return 0;
    }
    转载请注明出处:https://www.cnblogs.com/lllxq/
  • 相关阅读:
    CentOS7搭建SFTP服务
    MySQL主从异常恢复
    MySQL主从复制配置
    Docker安装MySQL8.0
    CentOS7安装JDK1.8
    RabbitMQ死信队列
    RabbitMQ重试机制
    RabbitMQ消息可靠性传输
    TCP/IP的Socket编程
    c#网络编程使用tcpListener和tcpClient
  • 原文地址:https://www.cnblogs.com/lllxq/p/10041270.html
Copyright © 2011-2022 走看看