zoukankan      html  css  js  c++  java
  • Codeforces Round #570 (Div. 3 )A

    A. Nearest Interesting Number

    题目链接:http://codeforces.com/contest/1183/problem/A

    题目:

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

    Help Polycarp find the nearest larger or equal interesting number for the given number a. That is, find the interesting number n such that n≥a and nis minimal.
    Input

    The only line in the input contains an integer a(1≤a≤1000).
    Output

    Print the nearest greater or equal interesting number for the given number a. In other words, print the interesting number n such that n≥a and n is minimal.
    Examples
    Input
    432
    Output
    435

    Input
    99

    Output
    103
    Input
    237
    Output
    237
    Input
    42
    Output
    44
    题意:
    Polycarp知道如果一个数字的数字之和可以被3整除,那么数字本身就可以被3整除。他假设数字,其中数字的总和可被4整除,也有点有趣。 因此,如果数字的总和可以被4整除,他认为正整数是有趣的.Help Polycarp找到给定数字a的最近或更大的有趣数字也就是说,找到有趣的数字n使得n≥a且n是最小的。
    输入

    输入中唯一的行包含一个整数a1≤a≤1000
    输出

    打印给定数字a的最近或更大的有趣数字换句话说,打印有趣数字n使得n≥a且n最小
    思路:用sprintf和sscanf两个库函数,暴力即可,不满足,就加一
    //
    // Created by hanyu on 2019/7/7.
    //
    #include<iostream>
    #include<queue>
    #include<cstring>
    #include<cstdio>
    #include<bits/stdc++.h>
    using namespace std;
    bool change(char z[1005])
    {
        int sum = 0;
        int len = strlen(z);
        for (int i = 0; i < len; i++) {
            sum+=z[i]-'0';
        }
        if(sum%4==0)
            return true;
        else
            return false;
    }
    int main()
    {
        char str[1005];
        while(cin>>str) {
            if(change(str))
                cout<<str<<endl;
            else
            {   int a;
                sscanf(str,"%d",&a);
                 for(int i=0;;i++)
                 {
                     a++;
                     char str1[1005];
                     sprintf(str1,"%d",a);
                     if(change(str1))
                     {
    
                         cout<<str1<<endl;
                         break;
                     } else
                     {
                         sscanf(str1,"%d",&a);
                     }
                 }
            }
        }
        return 0;
    }
  • 相关阅读:
    android签名文件(.keystore)
    server2008部署网狐荣耀遇到的问题
    网关详解
    服务端架构
    rest service下载文件与上传
    mysql 按类别之用一条SQL语句查询出每个班前10名学生数据
    windows mysql 和linux mysql解决乱码问题
    关于redis的安装
    收藏nginx学习
    linux在tomcat中指定jdk
  • 原文地址:https://www.cnblogs.com/Vampire6/p/11148866.html
Copyright © 2011-2022 走看看