Codeforces Round#441 Div1 A P875A Classroom Watch

CodeForces Round#441 Div1 A 解题报告

题目

好久好久没写过博客了,碰巧打CF的时候做到了一道水题,就拿来写写题解
原题面:

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 x written 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.


大概题意:

\(f(n)=n+n\)的十进制的各位的和,已知\(f(n)\)求n。(其实以本蒟蒻的英语水平是看不懂题意的,但还好ranwen大佬及时伸出援手,蒟蒻才搞完了这道水题。。。)


样例:

Input
21

Output
1
15

Input
20

Output
0

数据范围:

\(1\leq n\leq 10^9\)


分析:

刚开始想了半天的数论,但实在是想不出来个啥,就往暴力的方向去想了,其实暴力还是很好想的。首先,因为\(n\leq 10^9\)所以可以很简单的证明n的十进制各位相加最大不超过90。
剩下的就很好做了,直接从n开始从大到小枚举,判断是否满足题意后输出即可。(注意输出的时候要从小到大输出)


AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;
int dig[10];
long long get(long long num)
{
    int cnt=0;
    while(num>=10)
    {
        cnt++;
        dig[cnt]=num%10;
        num=num/10;
    }
    cnt++;
    dig[cnt]=num%10;
    long long ans=0;
    for(int i=1;i<=cnt;i++)
    {
        ans+=dig[i];
    }
    return ans;
}
long long fans[105];
int main()
{
    long long n;
    cin>>n;
    int cnt=0;
    for(int i=1;i<=99;i++)
    {
        if(n-i<0)
        {
            break;
        }
        long long mid=n-i;
        if(mid+get(mid)==n)
        {
            cnt++;
            fans[cnt]=mid;
        }
    }
    cout<<cnt<<endl;
    for(int i=cnt;i>=1;i--)
    {
        cout<<fans[i]<<" ";
    }
    return 0;
}

总结:
有时候暴力也许是很好的方向。。。

posted @ 2017-10-17 21:59  黑泽斯  阅读(271)  评论(1编辑  收藏  举报