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 }
  • 相关阅读:
    解题报告 校门外的树
    解题报告 最长上升子序列
    解题报告 poj 2528 (罕见的浮水法解这个题的。。。。。。)
    解题报告 整数划分
    悲剧的程序员
    解题报告 sgu 102
    NOI 2007 社交网络
    解题报告 poj 1087
    知识点梳理 离散化
    解题报告 noi 2002 robot
  • 原文地址:https://www.cnblogs.com/dragondragon/p/11196849.html
Copyright © 2011-2022 走看看