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 ...
随机推荐
- 【JavaSE】泛型
Java泛型 2019-07-05 22:00:24 by冲冲 1. 泛型的引例 1 List list = new ArrayList(); 2 list.add(1022); //向集合中添加 ...
- salesforce零基础学习(一百零九)Lightning Login启用以及配置
本篇参考:https://help.salesforce.com/s/articleView?id=sf.security_ll_overview.htm&type=5 我们在之前的篇中提到过 ...
- 【Microsoft Azure 的1024种玩法】七.Azure云端搭建部署属于自己的维基百科
[简介] MediaWiki是全球最著名的开源wiki程序,运行于PHP+MySQL环境.MediaWiki从2002年2月25日被作为维基百科全书的系统软件,并有大量其他应用实例.MediaWiki ...
- SNP 过滤(二)
本文转载于https://www.jianshu.com/p/e6d5dd774c6e SNP位点过滤 SNP过滤有两种情况,一种是仅根据位点质量信息(测序深度,回帖质量等)对SNP进行粗过滤.如果使 ...
- 【宏蛋白组】iMetaLab平台分析肠道宏蛋白质组数据
目录 一.iMetaLab简介 二.内置工具与模块 1. Data Processing module 2. Functional Analysis 3. R Developing environme ...
- MySQL 的查询优化
说起 MySQL 的查询优化,相信大家收藏了一堆奇技淫巧:不能使用 SELECT *.不使用 NULL 字段.合理创建索引.为字段选择合适的数据类型..... 你是否真的理解这些优化技巧?是否理解它背 ...
- 总结HashSet以及分析部分底层源码
总结HashSet以及分析部分底层源码 1. HashSet继承的抽象类和实现的接口 继承的抽象类:AbstractSet 实现了Set接口 实现了Cloneable接口 实现了Serializabl ...
- .NET Core基础篇之:集成Swagger文档与自定义Swagger UI
Swagger大家都不陌生,Swagger (OpenAPI) 是一个与编程语言无关的接口规范,用于描述项目中的 REST API.它的出现主要是节约了开发人员编写接口文档的时间,可以根据项目中的注释 ...
- 云原生时代,为什么基础设施即代码(IaC)是开发者体验的核心?
作者 | 林俊(万念) 来源 |尔达 Erda 公众号 从一个小故事开始 你是一个高级开发工程师. 某天,你自信地写好了自动煮咖啡功能的代码,并在本地调试通过.代码合并入主干分支后,你准备把服务发布到 ...
- words in English that contradict themselves
[S1E10, TBBT]Leonard: I don't get it. I already told her a lie. Why would I replace it with a differ ...