CF102B Sum of Digits 题解
Content
给定一个数 \(n\),每次操作可以将 \(n\) 变成 \(n\) 各位数之和。问你几次操作之后可以将 \(n\) 变为一位数。
数据范围:\(1\leqslant n\leqslant 10^{10^5}\)。
Solution
一看这么大个数字我们就不能够用 int,long long 之类的类型读入了,只能够用字符串、字符数组。然后考虑将所有的数位暴力拆开求和,然后再代入求出操作后的数字,直到变成一位数为止。
Code
请注意下面的代码需要特判是否本来就是一位数。
const int N = 1e5 + 7;
int digit[N], tmp[N], tmp2[N], sum, ans;
inline int td(char x) {return x - '0';}
int main() {
crstr(s, n);
if(n == 1) return printf("0"), 0;
F(i, 0, n - 1) digit[i + 1] = td(s[i]);
while(1) {
memset(tmp, 0, sizeof(tmp));
memset(tmp2, 0, sizeof(tmp2));
sum = 0; //Clear all!!!
F(i, 1, n) sum += digit[i];
while(sum) {
tmp[++tmp[0]] = sum % 10;
sum /= 10;
}
R(i, tmp[0], 1) tmp2[i] = tmp[tmp[0] - i + 1];
memset(digit, 0, sizeof(digit));
F(i, 1, tmp[0]) digit[i] = tmp2[i];
n = tmp[0], ans++;
if(n == 1) break;
}
return write(ans), 0;
}
CF102B Sum of Digits 题解的更多相关文章
- CodeForces 489C Given Length and Sum of Digits... (贪心)
Given Length and Sum of Digits... 题目链接: http://acm.hust.edu.cn/vjudge/contest/121332#problem/F Descr ...
- Sum of Digits / Digital Root
Sum of Digits / Digital Root In this kata, you must create a digital root function. A digital root i ...
- Maximum Sum of Digits(CodeForces 1060B)
Description You are given a positive integer nn. Let S(x) be sum of digits in base 10 representation ...
- Codeforces Round #277.5 (Div. 2)C——Given Length and Sum of Digits...
C. Given Length and Sum of Digits... time limit per test 1 second memory limit per test 256 megabyte ...
- CodeForces 1060 B Maximum Sum of Digits
Maximum Sum of Digits You are given a positive integer n. Let S(x)S(x) be sum of digits in base 10 r ...
- Hdu3022 Sum of Digits
Sum of Digits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tot ...
- codeforces#277.5 C. Given Length and Sum of Digits
C. Given Length and Sum of Digits... time limit per test 1 second memory limit per test 256 megabyte ...
- cf#513 B. Maximum Sum of Digits
B. Maximum Sum of Digits time limit per test 2 seconds memory limit per test 512 megabytes input sta ...
- CodeForces 489C Given Length and Sum of Digits... (dfs)
C. Given Length and Sum of Digits... time limit per test 1 second memory limit per test 256 megabyte ...
随机推荐
- 一个初步的lilypond模板
代码文档在下方,涉及了许多基本的文档操作,包括: 1)页面设置,包括纸张大小.页边距 2)段落设置,包括不同内容之间的行距 3)乐谱设置,包括设置谱子大小.谱号.调号.拍号,甚至还有拍号/小节线不可见 ...
- JavaScript高级程序设计读后感(一)之零碎知识点查漏补缺
目录 1-script延迟脚本defer及异步脚本async,区别及应用场景 2-未声明的变量,未初始化变量 3-Number parseInt 字符串转数值 ,进制转换 4-undefined &a ...
- 第41篇-JNIEnv与JavaVM的初始化
JavaVM和JNIEnv的初始化和JVM各模块的初始化都是在JNI_CreateJavaVM()函数中完成.这一篇将详细介绍JavaVM和JNIEnv的初始化过程. 1.初始化JavaVM Java ...
- Codeforces 1464F - My Beautiful Madness(树的直径)
Codeforces 题面传送门 & 洛谷题面传送门 树上数据结构大杂烩(?) 首先考虑什么样的点能够在所有路径的 \(d\) 邻居的交集内.显然如果一个点在一条路径的 \(d\) 邻居内则必 ...
- 洛谷 P7324 - [WC2021] 表达式求值(状压+dp)
题面传送门 现场人傻系列-- 首先建出 \(E\) 的表达式树,具体来说表达式的每一个叶子节点表示一个数组 \(A_i\),每一个非叶子节点都表示一次运算,它的值表示左右儿子进行该运算后得到的结果.这 ...
- Atcoder Grand Contest 031 D - A Sequence of Permutations(置换+猜结论)
Atcoder 题面传送门 & 洛谷题面传送门 猜结论神题. 首先考虑探究题目中 \(f\) 函数的性质,\(f(p,q)_{p_i}=q_i\leftarrow f(p,q)\circ p= ...
- Python list的深拷贝和浅拷贝
深拷贝和浅拷贝 列表存储数据,列表拷贝就是数据备份 浅拷贝 优点:占用内存较少 缺点:修改深层数据,会影响原数据 深拷贝 优点:修改数据,互不影响 缺点:占用内存较大 ""&quo ...
- [Linux] 非root安装GCC9.1.0
说明 一般Linux系统自带或公共的GCC版本都很低,如目前我们的服务器版本的GCC还停留在gcc-4.9.3,而官网已到达9.2版本(下载http://ftp.gnu.org/gnu/gcc/) , ...
- 【3】蛋白鉴定软件之Mascot
目录 1.简介 2.配置 2.1在线版本 2.2 服务器版本 3.运行 3.1 在线版本 3.2 服务器版本 4.结果 1.简介 Mascot是非常经典的蛋白鉴定软件,被Frost & Sul ...
- R语言与医学统计图形【2】散点图、盒形图
R语言基础绘图系统 基础图形--散点图.盒形图 plot是一个泛型函数(generic method),对于不同的数据绘制不同的图形. par函数的大部分参数在plot中通用. 1.散点图 plot绘 ...