#Digital Roots#写个while循环就行

Digital Roots

https://www.nowcoder.com/practice/cef727d0af33479c9fb4a9c120702414

//Java写法
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNextInt()) {
            int n = sc.nextInt();
            while (n > 9) {
                int sum = 0;
                int temp = n;
                while (temp > 0) {
                    sum += temp % 10;
                    temp /= 10;
                }
                n = sum;
            }
            System.out.println(n);
        }
    }
}
#Python写法
while True:
    try:
        n = int(input())
        while n > 9:
            n = sum(map(int, str(n)))
        print(n)
    except:
        break

全部评论

相关推荐

不愿透露姓名的神秘牛友
06-11 13:34
offe从四面八方来:我真的没时间陪你闹了
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-07 14:00
不想多说了,什么逆天HR,还要教我礼貌😂
机械打工仔:这不纯傻卵吗,他还操心上别人老板了
投递BOSS直聘等公司7个岗位
点赞 评论 收藏
分享
评论
2
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务