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 }
  • 相关阅读:
    BZOJ 3041 水叮当的舞步
    Codevs 1744 格子染色==BZOJ 1296 粉刷匠
    洛谷P1077 摆花
    1256 打鼹鼠
    mybatis--面向接口编程
    柳峰微信公众平台开发教程企业号改动篇(企业菜单篇)
    com关于引用计数
    oracle用户管理实例
    用 JSQMessagesViewController 创建一个 iOS 聊天 App
    循环链表设计与API实现
  • 原文地址:https://www.cnblogs.com/dragondragon/p/11196849.html
Copyright © 2011-2022 走看看