zoukankan      html  css  js  c++  java
  • Nearest Interesting Number

    Polycarp knows that if the sum of the digits of a number is divisible by 33, then the number itself is divisible by 33. He assumes that the numbers, the sum of the digits of which is divisible by 44, are also somewhat interesting. Thus, he considers a positive integer nn interesting if its sum of digits is divisible by 44.

    Help Polycarp find the nearest larger or equal interesting number for the given number aa. That is, find the interesting number nn such that nan≥a and nn is minimal.

    Analysis:

    Even if we will iterate over all possible numbers starting from aa and check if sum of digits of the current number is divisible by 44, we will find the answer very fast. The maximum possible number of iterations is no more than 55.

    Codes:

    My previous codes:

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 int f(int n)
     4 {
     5     int s=0;
     6     while(n){
     7         s+=n%10;
     8         n=n/10; 
     9     }
    10     return s;
    11 }
    12 int main()
    13 {
    14     int n;
    15     while(cin>>n)
    16     {
    17         for(int i=n;i<=1000;i++)   //运行老是无以通过!
    18         {
    19             if(f(i)%4==0) break;
    20         }
    21         cout<<f(i)<<endl;
    22     }
    23     return 0;
    24 }

    By learning:

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 int sum(int n){
     4     int s=0;
     5     while(n){
     6     s+=n%10;
     7     n/=10;
     8     }
     9     return s;
    10 }
    11 
    12 int main(){
    13     int n;
    14     while(cin>>n){
    15     while(sum(n)%4!=0){
    16     n++;  //很简洁优美的一定情况下的自增哦!
    17     }
    18     cout<<n<<endl;
    19     }
    20 }
  • 相关阅读:
    Mysql Select 语句中实现的判断
    SQL根据一个字符串集合循环保存数据库
    SQL语句 不足位数补0
    SVN常见错误
    svn架构
    关于EXCEL显示数字
    exception from hresult:0x8000401A(excel文档导出)
    WIN7安装注意事项
    解决卸载时残留目标文件夹的问题
    Installshield执行多条批处理的方法
  • 原文地址:https://www.cnblogs.com/dragondragon/p/11196849.html
Copyright © 2011-2022 走看看