Codeforces April Fools Day Contest 2021-B. DMCA-题解
目录
Codeforces April Fools Day Contest 2021-B. DMCA
传送门
Time Limit: 1 seconds
Memory Limit: 256 megabytes
Many people are aware of DMCA – Digital Millennium Copyright Act. But another recently proposed DMCA – Digital Millennium Calculation Act – is much less known.
In this problem you need to find a root of a number according to this new DMCA law.
Input
The input contains a single integer a ( 1 ≤ a ≤ 1000000 ) a (1≤a≤1000000) a(1≤a≤1000000).
Output
Output the result – an integer number.
Example
Input
1
OutPut
1
Input
81
OutPut
9
题目大意
新出了DMCA,想要解答这道题,请依据DMCA方案
能知道的,就是与DMCA有关。
题解
将一个数的每一位相加得到一个新数,用新数代替旧数,直至变成个位数为止并输出。
这怎么能想得到
另外
比赛的时候,乍一看像是开根号。不能正好开根号怎么办?试试向上向下取整,都不对
样例分析
81 ⇒ 8 1 ⇒9(8+1)
1598 ⇒ 1 5 9 8 ⇒ 23(1+5+9+8) ⇒ 2 3 ⇒ 5(2+3)
AC代码
#include <bits/stdc++.h>
using namespace std;
#define mem(a) memset(a, 0, sizeof(a))
#define dbg(x) cout << #x << " = " << x << endl
#define fi(i, l, r) for (int i = l; i < r; i++)
#define cd(a) scanf("%d", &a)
typedef long long ll;
int New(int n) //老数生新数
{
int ans = 0;
while (n) //取出n的每一位
{
ans += n % 10;
n /= 10;
}
return ans;
}
int main()
{
int n;
cin >> n;
while (n / 10) //n不是个位数
n = New(n);
cout << n << endl;
return 0;
}
原创不易,转载请附上原文链接哦~
Tisfy:https://letmefly.blog.csdn.net/article/details/115416777
更多推荐
所有评论(0)