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 }
  • 相关阅读:
    Linux解压bz2文件的方法
    Linux系统解压.tar.gz文件方法
    nginx实现负载均衡
    nginx实现反向代理demo
    spring注解版
    使用poi导入excel中的数据
    springmvc 拦截器
    springmvc之上传文件
    springmvc自定义异常处理器
    springmvc自定义参数转换
  • 原文地址:https://www.cnblogs.com/dragondragon/p/11196849.html
Copyright © 2011-2022 走看看