【刷题】Sum of Digits【1】

一、题目

1.题目描述

题目:
在这里插入图片描述

输入
在这里插入图片描述

输出
在这里插入图片描述

示例 :
在这里插入图片描述
提示:
In the first sample the number already is one-digit — Herald can’t cast a spell.
The second test contains number 10. After one casting of a spell it becomes 1, and here the process is completed. Thus, Gerald can only cast the spell once.
The third test contains number 991. As one casts a spell the following transformations take place: 991 → 19 → 10 → 1. After three transformations the number becomes one-digit.

二、解题报告

1.思路分析

  • 不断对数字按位进行累加,直到变成一个数字为止

2.代码详解

python👇【检验超时】

a=input()
b=list(a)
c=[]
s=0
i=1
while 1:
    for j in range(len(b)):
        s+=int(b[j])
    if s==0:
        print(0)
        break
    if s<9:
        print(i)
        break
    else:
        b=list(str(s))
        c=[]
        s=0
        i+=1

C++👇

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include<set>
#include <algorithm>
using namespace std;
 
char s[100100];
 
int main() 
{
	int n,cnt,i,res;
	scanf("%s",s);
	cnt=0;
	while (s[1]) 
	{
		cnt++;
		res=0;
		for(i=0;s[i];i++)
		{
			res+=s[i]-'0';
		}
		sprintf(s,"%d",res);
	}
	printf("%d\n",cnt);
	return 0;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

司六米希

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值