zoukankan      html  css  js  c++  java
  • codeforces 876 C. Classroom Watch

    http://codeforces.com/contest/876/problem/C

    C. Classroom Watch
    time limit per test
    1 second
    memory limit per test
    512 megabytes
    input
    standard input
    output
    standard output

    Eighth-grader Vova is on duty today in the class. After classes, he went into the office to wash the board, and found on it the number n. He asked what is this number and the teacher of mathematics Inna Petrovna answered Vova that n is the answer to the arithmetic task for first-graders. In the textbook, a certain positive integer x was given. The task was to add x to the sum of the digits of the number xwritten in decimal numeral system.

    Since the number n on the board was small, Vova quickly guessed which x could be in the textbook. Now he wants to get a program which will search for arbitrary values of the number n for all suitable values of x or determine that such x does not exist. Write such a program for Vova.

    Input

    The first line contains integer n (1 ≤ n ≤ 109).

    Output

    In the first line print one integer k — number of different values of x satisfying the condition.

    In next k lines print these values in ascending order.

    Examples
    input
    21
    output
    1
    15
    input
    20
    output
    0
    Note

    In the first test case x = 15 there is only one variant: 15 + 1 + 5 = 21.

    In the second test case there are no such x.

    题意:

    找出所有的x,满足x+x的每一位=n

    因为n<=1e9,所以最大的数位之和为81

    从n-81 开始枚举即可

    #include<cstdio>
    #include<algorithm>
    
    using namespace std;
    
    int n,tot,ans[82];
    
    bool check(int x)
    {
        int y=x,cnt=x;
        while(y) cnt+=y%10,y/=10;
        return cnt==n;
    }
    
    int main()
    {
        scanf("%d",&n);
        for(int i=max(1,n-81);i<=n;i++) 
         if(check(i)) ans[++tot]=i;
        printf("%d
    ",tot);
        for(int i=1;i<=tot;i++) printf("%d ",ans[i]);
    }
  • 相关阅读:
    thoughtworks家庭作业C++版本
    删除数组中等于某个key的所有元素
    一些必读的开源项目
    库函数strlen源码重现及注意问题
    判断相同树或者对称树
    约瑟夫环问题
    Loadrunner 脚本录制策略
    品味性能之道<十一>:JAVA中switch和if性能比较
    白盒静态自动化测试工具:FindBugs使用指南
    六个步骤把资料转换成知识
  • 原文地址:https://www.cnblogs.com/TheRoadToTheGold/p/7711515.html
Copyright © 2011-2022 走看看