zoukankan      html  css  js  c++  java
  • HDU1013

    It is a easy problem. 

    But we can have two ways to solve this problem.

    The first way is simulation that calculate each number repeatedly.

    The second way is using numeric method that one numer mod 9 is equal to summing those digits mod 9.

    Like Follow:

    Because: ABCD...XYZ%9 == [ A(10^(n-1)) + B(10^(n-2)) + ... + Z ]%9

    Suppose: ABCD...XYZ%9 == (A+B+C+D+...+X+Y+Z)%9

    So:

    ABCD...XYZ%9 - (A+B+C+D+...+X+Y+Z)%9 = 0

    [ A(10^(n-1)) + B(10^(n-2)) + ... + Z ]%9 - (A+B+C+D+...+X+Y+Z)%9 = 0

    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.

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

    #include <iostream>
    #include <string>
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    using namespace std; 
    
    int const MAXN = 1000000;
    
    int N;
    
    
    
    int rootStr(char *str)
    {
    	int addNum;
    	int len; 
    	
    	addNum = 0; 
    	len = strlen(str);	
    	for(int i=0; i<len; i++)
    		addNum += str[i] - '0';
    	
    	while(addNum > 9)
    	{
    		addNum = 0; 
    		len = strlen(str);
    		for(int i=0; i<len; i++)
    			addNum += str[i] - '0';
    		sprintf(str, "%d", addNum);
    	}
    	return addNum;
    }
    
    int rootStr2(char *str)
    {
    	int addNum;
    	int len; 
    	
    	addNum = 0; 
    	len = strlen(str);	
    	for(int i=0; i<len; i++)
    		addNum += str[i] - '0';
    	
    	if(addNum%9==0)
    		return 9;
    	else 
    		return addNum%9;
    }
    
    int main()
    {
    
    #ifndef ONLINE_JUDGE
    	freopen("in.txt", "r", stdin);
    #endif	
    	
    	char a[MAXN];
    	while(~scanf("%s", a) && a[0] !='0')
    	{
    	//	int ans = rootStr(a);
    		int ans = rootStr2(a);
    		printf("%d
    ", ans);
    	}
    
    
    	return 0; 
    }
    

      

  • 相关阅读:
    Mac OS X上安装配置apache服务器
    eclipse 发布web工程,修改tomcat端口
    masonry瀑布流的使用
    fullcalendar 使用教程
    -webkit-line-clamp下多行文字溢出点点点...显示实例页面
    HTML5 Audio标签方法和函数API介绍
    Canvas与Image互相转换
    iOS绘图UIBezierPath 和 Core Graphics框架
    实现外卖选餐时两级 tableView 联动效果
    零行代码为 App 添加异常加载占位图
  • 原文地址:https://www.cnblogs.com/jluzhsai/p/4988064.html
Copyright © 2011-2022 走看看