[WUST2017]一组简单一点的题目(一)W - Digital Roots

news/2024/7/2 21:30:00

题目:W - Digital Roots
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

大意:就是输入一个数,将其每一位上的数都取出来相加,看能否出现满足这个数是个位数;

思路:因为这个题为表明其范围,所以有可能位数非常多,即数非常大;则不能通过一个一个取位的方法来进行;则这时候要通过字符串取,并且每次相加的时候,如果这个数变成两位数了(即大于9了),则立马将其分解成个位数,进而相加,这样就不用再等全加完在继续了。这里提前分解相加和全相加之后分解本质上是一样的,因为就像10+6=16,前面先分开则为1+0+6,后面则为1+6,所以一样的;

小技巧:对于这类数的问题,为表明范围的时候要特意考虑一下有没有可能出现非常大的数或者非常小的数之类的,还有对于每个位相加的问题,即10+6=16,前面先分开则为1+0+6,后面则为1+6,所以一样的
,这种出现不能用常规方式处理的数时,就很有可能用一些小技巧,其满足什么条件之后接着进行分解之类的。小技巧的利用非常重要,自己要加油体会到;

代码:

#include<stdio.h>
#include<string.h>
char s[1000000];
int main()
{
    int n,sum;

    while(scanf("%s",s)!=EOF)
    {
        if(strcmp(s,"0")==0)
            break;
        n=strlen(s);
        sum=0;
        for(int i=0;i<n;i++)
        {
            sum+=s[i]-'0';
            if(sum>=10)
                sum=sum%10+sum/10;
        }
        printf("%d\n",sum);
    }
    return 0;
}

http://www.niftyadmin.cn/n/1103879.html

相关文章

虚拟机安装苹果系统 出现不可恢复的错误解决办法

使用VMware Workstation 12 安装MacOS系统时出现以下错误&#xff1a; VMware Workstation 不可恢复错误: (vcpu-0) vcpu-0:VERIFY vmcore/vmm/main/physMem_monitor.c:1123 日志文件位于“E:\VMStore\OS X\vmware.log”中。 您可以请求支持。 要收集数据提交给 VMware…

Brich层次聚类

Brich层次聚类 参数&#xff1a; threshold : float, default 0.5 通过合并新样本和最接近的子集群而获得的子集群的半径应小于阈值。否则&#xff0c;将启动一个新的子集群。将此值设置为非常低会促进分裂&#xff0c;反之亦然。 branching_factor : int, default 50 每个…

view_init(elev,azim)函数的使用

转换视角进行观察&#xff0c;通过设置view_init(elev,azim)两个参数变化时&#xff0c;观察图像的变化 # ############################################################################# # Compute clustering print("Compute unstructured hierarchical clustering.…

Cocos2d中的Menu使用

学习cocos2d-x中的菜单主要须要了解&#xff1a;菜单(CCMenu)和菜单项(CCMenuItem)以及CCMenuItem的详细子类。a. 以下来学习一下相关的类。 1. CCMenu 菜单&#xff0c;是CCLayer的子类&#xff0c;是一个层(容器)&#xff0c;能够往里面加入菜单项。以下是它的类结构图&#…

路由更新控制

路由更新控制 首先为各个接口配置IP地址及掩码 网络拓扑图如下&#xff1a; 查看配置信息如下&#xff1a; 测试直连链路的连通性&#xff1a; 步骤一、配置RT、RA、RB和RC的OSPF路由协议 RT: [041740509-RT]ospf [041740509-RT-ospf-1]area 0 [041740509-RT-ospf-1…

属于两个不同的MUX VLAN之间的用户,互相通信是否能实现?

属于两个不同的MUX VLAN之间的用户&#xff0c;互相通信是否能实现&#xff1f; 答案&#xff1a; 属于两个不同的MUX VLAN之间的用户&#xff0c;互相通信是否能实现。 分析&#xff1a; MUX VLAN 提供了一种在VLAN内的端口间进行二层流量隔离机制&#xff0c;通过配置MUX…

jekins手动选择不同变量来构建不同环境

1&#xff1a;下载插件 下载安装&#xff1a;Extended Choice Parameter Plug-In 官方配置&#xff1a;https://wiki.jenkins.io/display/JENKINS/ExtendedChoiceParameterplugin2&#xff1a; 创建项目 3&#xff1a;配置项目4&#xff1a;配置脚本脚本&#xff1a;roo…

OSPF/2/NBRCHG:The status of the non-virtual neighbor changes.

OSPF告警NBRCHG&#xff1a;The status of the non-virtual neighbor changes. 告警解释:OSPF邻居状态发生变化&#xff0c;可能是由于该邻居所在的接口状态发生变化&#xff0c;或者是收到的Hello报文中的内容发生变化。 并且在此对告警参数的进行相关解释&#xff1a; oid…