HDOJ 1013题Digital Roots 大数,9余数定理

简介: HDOJ 1013题Digital Roots 大数,9余数定理

Problem Description

The digital root of a positive integer is found by summing the digits of the integer. If the resulting value is a single digit then that digit is the digital root. If the resulting value contains two or more digits, those digits are summed and the process is repeated. This is continued as long as necessary to obtain a single digit.


For example, consider the positive integer 24. Adding the 2 and the 4 yields a value of 6. Since 6 is a single digit, 6 is the digital root of 24. Now consider the positive integer 39. Adding the 3 and the 9 yields 12. Since 12 is not a single digit, the process must be repeated. Adding the 1 and the 2 yeilds 3, a single digit and also the digital root of 39.


Input

The input file will contain a list of positive integers, one per line. The end of the input will be indicated by an integer value of zero.


Output

For each integer in the input, output its digital root on a separate line of the output.


Sample Input

 


  24

39

0

 

Sample Output

 


  6

3

 



一个数对九取余,得到的数称之为九余数;

一个数的九余数等于它的各个数位上的数之和的九余数!


题目大意:

给定一个正整数,根据一定的规则求出该数的“数根”,其规则如下:

       例如给定 数字 24,将24的各个位上的数字“分离”,分别得到数字 2 和 4,而2+4=6;

       因为 6 < 10,所以就认为6是数字24的“数根”;

       而对于数字 39 , 将39的各个位上的数字“分离”,分别得到数字 3 和 9,而3+9=12,且12>10;

      所以依据规则再对 12 进行相应的运算,最后得到数字3,而3<10,所以就认为3是数字39的“数根”。

   

             通过运算可以发现任何一个数的“数根”都是一个取值范围在 1 ~ 9之间的正整数,

    且任何一个正整数都只有唯一的一个“数根”与其相对应。

             题目要求数字 n^n 的“数根”

解题思路:

九余数定理

一个数对九取余后的结果称为九余数。

一个数的各位数字之和想加后得到的<10的数字称为这个数的九余数(如果相加结果大于9,则继续各位相加)

代码如下:

#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int main()
{
    char a[1010];
    int i,j,s,l;
    while(~scanf("%s",&a)&&a[0]!='0')
    {
        l=strlen(a);
        s=0;
        for(i=0;i<l;i++)
        {
            s=s+a[i]-'0';
        }
        s=s%9;
        if(s==0)
            s=9;
        printf("%d\n",s);
    }
    return 0;
}




一个数对九取余,得到的数称之为九余数;

一个数的九余数等于它的各个数位上的数之和的九余数!


目录
相关文章
|
算法 Java Linux
Linux下文件增删改查定位压缩操作与权限所属用户
Linux下文件增删改查定位压缩操作与权限所属用户
144 0
|
9月前
|
人工智能 自然语言处理 安全
国内如何使用claude?人工智能claude国内使用方法来了!
Claude AI 是由 Anthropic 公司开发的一款新一代 AI 助手,旨在成为更安全、更友好、更可靠的 AI 系统。它基于 Anthropic 对 AI 安全性的深入研究,并采用 “Constitutional AI” (宪法式 AI) 的训练方法,
|
负载均衡 监控 安全
构建高效微服务架构:API网关的作用与实践
在当今的软件开发中,微服务架构因其灵活性和可扩展性而受到青睐。然而,随之而来的复杂性也给维护和性能带来了挑战。本文探讨了API网关在微服务架构中的关键作用,包括请求路由、负载均衡、安全性控制以及限流熔断等方面。通过具体案例分析,我们展示了如何实现一个高性能的API网关,以支持动态的后端服务,并确保系统的高可用性和安全性。
172 2
|
算法 算法框架/工具 Android开发
LeetCode 周赛上分之旅 #47 前后缀分解结合单调栈的贡献问题
学习数据结构与算法的关键在于掌握问题背后的算法思维框架,你的思考越抽象,它能覆盖的问题域就越广,理解难度也更复杂。在这个专栏里,小彭与你分享每场 LeetCode 周赛的解题报告,一起体会上分之旅。
114 0
|
存储 JavaScript 前端开发
在 JavaScript 中,我们能为原始类型添加一个属性或方法吗?
在 JavaScript 中,我们能为原始类型添加一个属性或方法吗?
302 0
|
Kubernetes 关系型数据库 数据库
微服务分布式事务最佳实践
微服务应用存在多个服务间的事务,采用全局事务服务高效提供事务方案,满足数据一致性。
微服务分布式事务最佳实践
|
Java Shell 程序员
从重复到重用
本文是我之前写的文章——《你试过这样写C程序吗》——的第二版,并把文章名改成更贴切的“从重复到重用”。
2875 1
|
架构师 开发框架 uml
项目经理 架构师的区别
<h1 class="postTitle" style="font-size:14.7px; margin-bottom:10px; color:rgb(75,75,75); font-family:Verdana,Geneva,Arial,Helvetica,sans-serif"> <a target="_blank" id="cb_post_title_url" class="po
6048 0
|
2天前
|
SpringCloudAlibaba 负载均衡 Dubbo
微服务架构下Feign和Dubbo的性能大比拼,到底鹿死谁手?
本文对比分析了SpringCloudAlibaba框架下Feign与Dubbo的服务调用性能及差异。Feign基于HTTP协议,使用简单,适合轻量级微服务架构;Dubbo采用RPC通信,性能更优,支持丰富的服务治理功能。通过实际测试,Dubbo在调用性能、负载均衡和服务发现方面表现更出色。两者各有适用场景,可根据项目需求灵活选择。
332 123
微服务架构下Feign和Dubbo的性能大比拼,到底鹿死谁手?