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 }
  • 相关阅读:
    【读书笔记】MSDN 上关于加密解密的一个例子
    【读书笔记】创建泛型编程类 由链表而深入
    HDFS数据加密空间Encryption zone
    各种数据库的连接方法
    Java基础5
    Java基础4
    Java基础2
    博客优化、收录、RSS技巧
    Java基础3
    Java基础8
  • 原文地址:https://www.cnblogs.com/dragondragon/p/11196849.html
Copyright © 2011-2022 走看看