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 ...
随机推荐
- xml的语法规则
XML 文档必须有根元素 XML 文档必须有关闭标签 XML 标签对大小写敏感 XML 元素必须被正确的嵌套 XML 属性必须加引号 针对元数据的 XML 属性 有时候会向元素分配 ID 引用.这些 ...
- 353 stars Java项目!Java小白必看!austin介绍 【第一话】
有好几个群友问我为什么最近更新变慢了.工作忙是一方面,另一方面是我更新文章的动力确实下降了.近大半年一直在更新的<对线面试官>系列,到现在已经40篇了. 说实话,当时我更新该系列有很大一部 ...
- Linux—ps -ef 命令输出信息的具体含义(显示所有正在运行的命令程序)
linux 中使用 ps -ef 输出参数的具体含义 功能:显示所有正在运行的命令程序 UID: 说明该程序被谁拥有PID:就是指该程序的 IDPPID: 就是指该程序父级程序的 IDC: 指的是 C ...
- rkhunter使用
1.下载地址:http://jaist.dl.sourceforge.net/project/rkhunter/rkhunter/1.4.6/ 2.上传至Linux后解压 3.编译安装 [root@t ...
- 57-Palindrome Linked List
Palindrome Linked List My Submissions QuestionEditorial Solution Total Accepted: 46990 Total Submiss ...
- hash_map,map,unordered_map效率
利用unordered_map代替hash_map 实验环境 操作系统 fedora9 编译器版本 gcc4.3 实验方式 各种map使用插入和查找,比较速度和相关性能 代码 参考代码 下面测试说明了 ...
- mac 下 如何在同一窗口打开多个终端并实现快捷键切换
相信大家编代码的时候都会遇到,每次需要在头文件,库文件和源码文件中编代码的时候,总是需要在几个文件中切换来切换去的,而且一个文件就一个终端窗口,每次都要用鼠标点来点去,非常麻烦,所以如果能把这几个文件 ...
- 在Kubernetes上安装Percona XtraDB集群
官方文档地址:https://www.percona.com/doc/kubernetes-operator-for-pxc/kubernetes.html 一.简介 Percona XtraDB C ...
- Levenshtein莱文斯坦算法在项目中的应用
简介 根据维基百科的描述,在信息理论.语言学和计算机科学中,莱文斯坦距离是一个测量两个序列之间差异的字符串度量.非正式地,两个单词之间的莱文斯坦距离是将一个单词改变为另一个单词所需的最小单字符编辑次数 ...
- Mybatis相关知识点(二)
Mybatis解决jdbc编程的问题 1. 数据库连接创建.释放频繁造成系统资源浪费从而影响系统性能,如果使用数据库连接池可解决此问题. 解决:在SqlMapConfig.xml中配置数据连接池,使用 ...