zoukankan      html  css  js  c++  java
  • sicily 1325. Digit Generator

    Description
    For a positive integer N , the digit-sum of N is defined as the sum of N itself and its digits. When M is the digitsum of N , we call N a generator of M .
    For example, the digit-sum of 245 is 256 (= 245 + 2 + 4 + 5). Therefore, 245 is a generator of 256.
    Not surprisingly, some numbers do not have any generators and some numbers have more than one generator. For example, the generators of 216 are 198 and 207.
    You are to write a program to find the smallest generator of the given integer.

    Input
    Your program is to read from standard input. The input consists of T test cases. The number of test cases T is given in the first line of the input. Each test case takes one line containing an integer N , 1≤N≤100, 000 .

    Output
    Your program is to write to standard output. Print exactly one line for each test case. The line is to contain a generator of N for each test case. If N has multiple generators, print the smallest. If N does not have any generators, print 0.
    The following shows sample input and output for three test cases.

    对于我等菜鸟来说根本不水的水题= = 关键是注意搜索上下界的优化,如果按照这种搜索的思路来做又不做这个搜索范围的优化的话,就会TLE。

    因为generator各位数字的和范围肯定在1~9*位数之间(1的情况是第一位是1后面都是0的generator,9*数字位数就是整个数字是一堆9的generator),所以generator本身的范围肯定在[原数字-1]~[原数字-9*位数]之间,往上就是原数字自己了,各位数字之和又不可能是0,所以不可能;再往下,用来补足generator和原数字之差的各位数字和也补不够这个差,所以也不可能。如果在这个范围找不到,那其他地方也肯定找不到generator了,所以return 0;

    这个方法的AC Run Time是0.01s,貌似还有其他很多方法,找了一些代码,要么是C++的要么会跑出break或者在代码中间出现变量声明,不想用……等以后回头再看看有什么更好的解决办法

    View Code
     1 #include <stdio.h>
     2 int generator( int num );
     3 
     4 int main()   
     5 {   
     6     int t, n;
     7     int i; 
     8      
     9      scanf( "%d", &t );   
    10      
    11      for ( i = 0; i < t; i++ ) 
    12      {   
    13         scanf( "%d", &n ); 
    14            
    15         printf( "%d\n", generator(n) );
    16     }
    17       
    18     return 0;
    19 }
    20 
    21 int generator( int num )
    22 {
    23     int len = 0, sum = 0;
    24     int i, k;
    25     
    26     for ( i = num; i != 0; i /= 10 ) 
    27     {
    28         len++;        /* 计算位数 */
    29     }
    30     
    31     /* 在 num - 9 * len ~ num - 1的范围内搜索generator,依据题意从小往大搜 */
    32     for ( i = num - 9 * len; i <= num - 1; i++ )
    33     {
    34         sum = 0;
    35         
    36         for ( k = i; k != 0; k /= 10 ) 
    37         {
    38             sum += ( k % 10 );    /* 计算i的各位数字之和 */
    39         }
    40         
    41         if ( i + sum == num ) 
    42         {
    43             return i;            /* 如果i符合题意,它就是generator */
    44         } 
    45     } 
    46     
    47     /* 如果没有搜到,generator是0 */ 
    48     return 0;
    49     
    50 }
  • 相关阅读:
    2018年11月1日开通博客园感想!
    Aspnet MVC 异步调用
    AspNet WebApi : MessageHandler(消息处理器 )
    AspNet MVC : 操作/控制器过滤器(action filter)
    PHP 面向对对象基础(接口,类)
    原生Ajax + Promise
    基于Qt QGraphicsView的多点触摸绘图
    node应用通过multer模块实现文件上传
    AspNet WebApi: 了解下HttpControllerDispatcher,控制器的创建和执行
    视频投影(二维视频投影到三维模型上)
  • 原文地址:https://www.cnblogs.com/joyeecheung/p/2798541.html
Copyright © 2011-2022 走看看