#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

全部评论

相关推荐

10-21 16:54
门头沟学院 Java
后端转测开第一人:微服务没用 校招都不看微服务的 还有就是后端行情是这样的 找实习纯看运气 秋招更是吃运气和缘分 如果对代码没有极致的追求 可以转测开
应届生简历当中,HR最关...
点赞 评论 收藏
分享
评论
2
收藏
分享

创作者周榜

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