树根

树根

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit Status Practice HDU 1013
Appoint description: System Crawler  (2015-04-27)

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
是位数上的和相加,一直到和为一位数:
你可以这么写:
#include <iostream>
#include <cstdlib>
#include <cstdio> #include <algorithm> #include <vector> #include <queue> #include <cmath> #include <stack> #include <cstring> using namespace std; #define INF 0xfffffff #define maxn 1200 #define min(a,b) (a<b?a:b) #define max(a,b) (a>b?a:b) int Root(int n) { if(n < 10) { return n; } int a = 0; while(n) { a += n%10; n /= 10; } Root(a); //递归,因为要做的步骤一样 } int main() { char str[maxn]; int n; while(cin >>str, strcmp(str, "0")) { n = 0; for(int i=0; str[i]; i++) n += str[i] - '0'; n = Root(n); // 就是正常思维一步步求树根 cout << n << endl; } return 0; }


你也可以这么写:
#include<stdio.h>
#include<string.h> char s[1010]; int main() { int sum,i,len,x; while(gets(s)&&s[0]!='0') { sum=0; len=strlen(s); for(i=0;i<len;i++) sum+=s[i]-'0'; x=sum%9; // 求余9是因为十进制满10进1,38%9 = 2;38的树根是,3 + 8 = 11, 1 + 1 = 2,是2;自己悟=。=|| if(x==0) x+=9; printf("%d\n",x); } return 0; }

转载于:https://www.cnblogs.com/Tinamei/p/4468288.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/400451.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Scrapy 1.4 文档 01 初窥 Scrapy

初窥 Scrapy Scrapy 是用于抓取网站并提取结构化数据的应用程序框架&#xff0c;其应用非常广泛&#xff0c;如数据挖掘&#xff0c;信息处理或历史存档。 尽管 Scrapy 最初设计用于网络数据采集&#xff08;web scraping&#xff09;&#xff0c;但它也可用于使用 API&#xf…

【51单片机快速入门指南】4: 软件 I2C

目录硬知识I2C 介绍I2C 物理层I2C 协议层数据有效性规定起始和停止信号应答响应总线的寻址方式数据传输示例程序Soft_I2C.cSoft_I2C.h普中51-单核-A2 STC89C52 Keil uVision V5.29.0.0 PK51 Prof.Developers Kit Version:9.60.0.0 硬知识 摘自《普中 51 单片机开发攻略》 I2…

C# TextBox 换行 滚动到最后一行

1、要让一个Windows Form的TextBox显示多行文本就得把它的Multiline属性设置为true。 这个大家都知道&#xff0c;可是当你要在代码中为Text属性设置多行文本的时候可能会遇到点麻烦&#xff1a;&#xff09;你往往会想到直接付给一个含有换行符"\n"的字符串给Text属…

this的用法this.name=name 这个什么意思

public Employee(string name, string alias){ // Use this to qualify the fields, name and alias: this.name name; this.alias alias;}this的用法this.namename 这个什么意思啊 具体点我有点狠难理解 这是个构造函数&#xff0c;this.name指当前类的属性&#x…

【51单片机快速入门指南】4.1: I2C 与 AT24C02 (EEPROM) 的跨页连续读写

目录硬知识AT24Cxx 介绍引脚排列引脚说明存储结构器件寻址器件操作待机模式存储复位写操作字节写页写应答查询读操作当前地址读随机读顺序读示例程序24C02.c24C02.h测试程序main.c实验现象通讯波形写入部分读取部分普中51-单核-A2 STC89C52 Keil uVision V5.29.0.0 PK51 Prof…

nyist oj 37 回文字符串 (动态规划经典)

回文字符串 时间限制&#xff1a;3000 ms | 内存限制&#xff1a;65535 KB难度&#xff1a;4描写叙述所谓回文字符串&#xff0c;就是一个字符串。从左到右读和从右到左读是全然一样的。比方"aba"。当然&#xff0c;我们给你的问题不会再简单到推断一个字符串是不是…

iOS Application Security

文章分A,B,C,D 4个部分。 A) iOS Application Security 下面介绍iOS应用安全&#xff0c;如何分析和动态修改app。 1&#xff09;iOS Application security Part 1 – Setting up a mobile pentesting platform Part1介绍如何在越狱的设备上搭建用来测试iOS安全的环境。 2&…

openlayers基础系列教程(一)

openlayers基础系列教程(一) http://www.openlayers.cn/portal.php?modview&aid19

【51单片机快速入门指南】4.2: SSD1306 OLED屏(0.96寸、1.3寸)的I2C控制详解

目录硬知识SSD1306简介I2C 接口从机地址位&#xff08;SA0&#xff09;I2C 总线写数据命令解码器晶振电路和显示时间发生器复位图形显示数据RAM (GDDRAM)命令表基本命令表部分指令详解为 BANK0 设置对比度控制&#xff08;81h&#xff09;全部显示开启&#xff08;A4h/A5h&…

使用GNS3和Cisco IOU搭建路由交换实验-安装篇

如何使用GNS3和Cisco IOU搭建路由交换实验-安装篇GNS3软件的安装建议大家从官网直接下载最新版本的GNS3&#xff0c;官网连接http://www.gns3.com/根据系统类型选择相应的版本&#xff0c;这里我选择的是Windwos系统的最新版本1.3.2下载好安装包后直接运行安装包&#xff0c;在…

符号代码写法大全

摘自&#xff1a;http://www.w3schools.com/tags/ref_symbols.asp Math Symbols Supported by HTML CharacterEntity NumberEntity NameDescription∀&forall;for all∂&part;part∃&exist;exists∅&empty;empty∇&nabla;nabla∈&isin;isin∉&noti…

解决问题E: 无法获得锁 /var/lib/dpkg/lock - open (11: 资源暂时不可用) E: 无法锁定管理目录,

在用sudo apt-get install kmymoney2安装软件kmymoney2时&#xff0c;由于速度太慢&#xff0c;想换个软件源&#xff0c;直接关闭了终端&#xff0c;apt&#xff0d;get但进程没有结束&#xff0c;结果终端提示 &#xff1a;“E: 无法获得锁 /var/lib/dpkg/lock - open (11: 资…

1787: [Ahoi2008]Meet 紧急集合

1787: [Ahoi2008]Meet 紧急集合 Time Limit: 20 Sec Memory Limit: 162 MBSubmit: 1482 Solved: 652[Submit][Status][Discuss]Description Input Output Sample Input 6 4 1 2 2 3 2 4 4 5 5 6 4 5 6 6 3 1 2 4 4 6 6 6 Sample Output 5 2 2 5 4 1 6 0 HINT Sou…

一步一步学Vue(四)

接上篇。上篇中给出了代码框架&#xff0c;没有具体实现&#xff0c;这一篇会对上篇定义的几个组件进行分别介绍和完善&#xff1a; 1、TodoContainer组件 TodoContainer组件&#xff0c;用来组织其它组件&#xff0c;这是react中推荐的方式&#xff0c;也是redux中高阶组件一般…

【51单片机快速入门指南】4.3: I2C读取MPU6050陀螺仪的原始数据

目录硬知识特性参数MPU6050 简介模块重要寄存器简介电源管理寄存器 1陀螺仪配置寄存器加速度传感器配置寄存器FIFO 使能寄存器陀螺仪采样率分频寄存器配置寄存器电源管理寄存器 2陀螺仪数据输出寄存器加速度传感器数据输出寄存器温度传感器示例程序MPU6050.cMPU6050.hmain.c实验…

OSM数据的获取及格式转换

转自 &#xff1a;http://blog.sina.com.cn/s/blog_72f0b6080102w39z.html 前言&#xff1a;本篇博文将介绍如何对OSM数据进行获取&#xff0c;以及格式的转换&#xff08;转为shapefile格式&#xff09;。以供OSM数据获取失败、OSM editor操作失败的朋友参考。由于并不是多么高…

再读TCP/IP网络7层协议

随着工作的深入&#xff0c;每次读这7层协议&#xff0c;每次都有不同的理解。 分层名 分层号 描述 比喻 应用层Application La…

LVS之一:基本命令和调度方法

实验环境&#xff1a;fedora_server最新版本1.查看内核是否支持ipvsgrep -i vs /boot/config-3.17.4-301.fc21.i686PAE2.安装ipvsadmyum install ipvsadm3.ipvsadm命令1、管理集群服务1.添加&#xff1a;-Aipvsadm -A|E -t|u|f service-address-t&#xff1a;tcp协议集群 -u&am…

windows下安装mysql教程

下载mysql压缩包 我的系统是windows10&#xff0c;64位的&#xff0c;我下载了最新版的MySQL Community Server。这是社区版的mysql服务器。自己根据自己系统酌情下载。 解压和检查包内容 用你喜欢的软件将你下载来的压缩包解压&#xff0c;解压位置随意&#xff0c;因为可以配…