当前位置:网站首页>Code shoe set - mt2201 · sum of all
Code shoe set - mt2201 · sum of all
2022-07-22 15:33:00 【Tisfy】
Portal
Sum of you
The time limit :1 second
Space restriction :128M
Title Description
Small brother is super powerful , Any number can be changed into the sum of numbers on its various digits through one operation .
Now little brother has found a number n, After several operations, it can become a single digit .
Input description
Input contains an integer n n n
Data range
1 < = n < = 1 0 100000 1 <= n <= 10^{100000} 1<=n<=10100000
Output description
Output a number per line to indicate the answer
Example 1
Input
991
Output
3
Topic analysis
Directly as required , Just simulate .
In fact, I see 1 0 100000 10^{100000} 10100000 Don't be afraid , Directly treat the input number as a string .
1 0 100000 10^{100000} 10100000 That is, the maximum length of the input string is 100000 100000 100000, and 100000 100000 100000 The maximum sum of numbers is less than 1 0 6 10^6 106( most 6 6 6 position )
So it will soon converge to a single digit . Don't worry about time at all .
character string の Everyone and
The bit sum of the string is relatively easy , Traversing the string directly , And add up every bit .
ll getS(string s) {
ll ans = 0;
for (char& c : s)
ans += c - '0';
return ans;
}
Numbers の Everyone and
The sum of figures is not difficult . When the number is not zero , Take out the single digit of the number every time , Then divide the number by 10 10 10 that will do .
ll getS(ll n) {
ll ans = 0;
while (n) {
ans += n % 10;
n /= 10;
}
return ans;
}
AC Code
/* * @Author: LetMeFly * @Date: 2022-07-21 09:46:18 * @LastEditors: LetMeFly * @LastEditTime: 2022-07-21 09:48:26 */
#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;
ll getS(string s) {
ll ans = 0;
for (char& c : s)
ans += c - '0';
return ans;
}
ll getS(ll n) {
ll ans = 0;
while (n) {
ans += n % 10;
n /= 10;
}
return ans;
}
int main() {
string s;
cin >> s;
if (s.size() == 1) {
puts("0");
return 0;
}
ll n = getS(s);
int ans = 1;
while (true) {
if (n < 10)
break;
ans++;
n = getS(n);
}
cout << ans << endl;
return 0;
}
Although the code can be copied , But it's better to knock after understanding
Originality is not easy. , Reprint please attach Link to the original text Oh ~
Tisfy:https://letmefly.blog.csdn.net/article/details/125918169
边栏推荐
- Barcode (bar code)
- Waiting insurance compliance 2022 series | what should you know about waiting insurance this year?
- plt 画图并保存结果
- 对原数组有影响的几个方法
- 什么是探索性测试?探索性测试有哪些方法?
- 优炫数据库上可以搭建Oracle RAC吗?
- How to optimize this SQL?
- 重载(overload)和重写(override)的区别
- df.describe() 详解+用法+示例
- [information collection] write data from fofa API interface into txt and excel
猜你喜欢
随机推荐
AI chief architect 11 - "3d+ai" application and expansion in smart Sports
Values swxxdp calculation in Res
TDengine学习笔记
How to optimize this SQL?
【C语言-文件】数据终于可以出内存,到外面的世界看看了/(ㄒoㄒ)/~~
【C语言-程序编译】一行行代码究竟是怎么一步步到可执行程序的?
MySQL写循环语句的方法
Graffiti Wi Fi & ble SoC development slide strip (6) -- slide strip function demonstration
How Linux queries Oracle error logs
Redis高可用原理 主从哨兵集群
Waiting insurance compliance 2022 series | what should you know about waiting insurance this year?
MySQL的增删查改(第二话)
Two people line up to install locally & x360ce simulation handle Tutorial & xpadder handle simulation keyboard and mouse
MySQL练习一数据库的知识
Pyside2 as a simple browser
电流探头应该如何选择
Tdengine experimental cluster establishment success
LeetCode 0814. 二叉树剪枝
JMeter笔记1 | JMeter简介及体系结构
JMeter notes 1 | introduction and architecture of JMeter