zoukankan      html  css  js  c++  java
  • codeforces Round #441 C Classroom Watch【枚举/注意起点】

    C.
    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.

     【题意】:给出n,求x满足x + x各位数和=n,有多组输出方案数和方案。

    【分析】:暴力枚举数位和。注意因为最大就是999999999=81 ,更小的数一定凑不到n, so枚举起点是max(n-81,1)

    【代码】:

    #include <bits/stdc++.h>
    using namespace std;
    int save[100];
    int putu(int n,int i)
    {
        int p=i;
        while(p)
        {
            i+=p%10;
            p/=10;
        }
        return i==n;
    }
    int main(void)
    {
        int n,ans=0;
        scanf("%d",&n);
        for(int i=max(n-81,0);i<=n;i++)
        {
            if(putu(n,i))
            {
                save[ans++]=i;
            }
        }
        printf("%d
    ",ans );
        if(ans>0)
        {
            for(int i=0;i<ans;i++)
                printf("%d
    ",save[i] );
        }
        return 0;
    }
    View Code
  • 相关阅读:
    【POJ2777】Count Color(线段树)
    【HDU】I love sneakers!(分组背包)
    【HDU1712】ACboy needs your help(分组背包)
    【POJ1338】Ugly Numbers(暴力打表)
    【POJ2242】The Circumference of the Circle(初等几何)
    【POJ2136】Vertical Histogram(简单模拟)
    uvalive 6300 Signed Binary Representation of Integers
    第四届福建省大学生程序设计竞赛
    Codeforces Round #282 (Div. 1) B. Obsessive String
    Codeforces Round #282 (Div. 1) A. Treasure
  • 原文地址:https://www.cnblogs.com/Roni-i/p/7679090.html
Copyright © 2011-2022 走看看