zoukankan      html  css  js  c++  java
  • HDU 4608 I-number

    连接http://acm.hdu.edu.cn/showproblem.php?pid=4608

    题目大意:给你一个数N,让你每一位上的数字加起来之后可以被10整除的大于N的数中最小的一个。

    一开始想错了,想BFS,后来一想肯定不行然后去看了1003,然后发现1003不大适合我。然后就换了1009,看了一会就想想把所有的数加起来看他离%10 == 0差多少,然后从各位开始补,如果个位能补到10那就是结果,如果不能那么用两位或者更多位一定能补掉这个结果(num[n-1]+1000000-bit)%10,bit为其他加1的个数。

    代码如下:

     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <cstring>
     4 using namespace std;
     5 char str[100050];
     6 int num[100050];
     7 int main()
     8 {
     9     int t,i;
    10     while(~scanf("%d",&t))
    11     {
    12         while(t--)
    13         {
    14             scanf("%s",str);
    15             int n;
    16             n = strlen(str);
    17             for(i = 0;i < n;i++)
    18             num[i] = str[i]-'0';
    19 
    20             int rest;
    21             rest  = 0;
    22             for(i = 0;i < n;i++)
    23             rest += num[i];
    24             rest = 10-rest%10;
    25 
    26             num[n-1] += rest;
    27             int bit;
    28             bit = 0;
    29             if(num[n-1] > 9)
    30             {
    31                 for(i = n-2;i >= 0;i--)
    32                 {
    33                     bit++;
    34                     num[i]++;
    35                     num[i] %= 10;
    36                     if(num[i])
    37                     break;
    38                 }
    39                 if(i<0)
    40                 {
    41                     printf("1");
    42                     for(i = n-1;i > 0;i--)
    43                     printf("0");
    44                     puts("9");
    45                 }
    46                 else
    47                 {
    48                     num[n-1] = (num[n-1]+1000000-bit)%10;
    49                     for(i = 0;i < n;i++)
    50                     printf("%d",num[i]);
    51                     puts("");
    52                 }
    53             }
    54             else
    55             {
    56                 for(i = 0;i < n;i++)
    57                 printf("%d",num[i]);
    58 
    59                 puts("");
    60             }
    61         }
    62     }
    63     return 0;
    64 }
    View Code

    另外还有一种方法是用大整数加法+1逐个去找,找的话会发现不会超过20个就可以找到结果~

  • 相关阅读:
    ccc pool
    ccc 模拟重力 正太分布
    ccc 正态分布
    ccc this 指针
    基本语法HelloWorld
    css选择器
    css基本语法
    表单
    表格
    超链接
  • 原文地址:https://www.cnblogs.com/0803yijia/p/3209429.html
Copyright © 2011-2022 走看看