HDU1163 Eddy's digital Roots

问题链接:HDU1013 Digital Roots。入门练习题,用C语言编写程序。

这个问题是对于输入的n,计算n^n的数根。

先看一下以下式子:

因为:(10*a+b)*(10*a+b)=100*a*a+10*2*a*b+b*b 

所以右边式子的数根(中间结果,也是左边式子的数根)为:a*a+2*a*b+b*b=(a+b)*(a+b)

故:对于两位数n,n*n的数根=n的树根×n的树根。

同理可以推出,对于任意位数的n,也满足:n*n的数根=n的树根×n的树根。 

程序中,实现一个计算整数数根的函数,利用这个函数来计算n^n的数根。

这个问题还有一种解决办法是利用9余数定理来实现。

AC的C语言程序如下:

/* HDU1163 Eddy's digital Roots */

#include <stdio.h>

// 计算数根函数
int digitalroots(int val)
{
    int result, temp;

    while(val) {
        result = 0;
        temp = val;

        while(temp) {
            result += temp % 10;
            temp /= 10;
        }

        if(result < 10)
            break;

        val = result;
    }

    return result;
}

int main(void)
{
    int n, ans, nr, i;

    while(scanf("%d", &n) != EOF) {
        if(n == 0)
            break;

        // 计算n的数根
        ans = nr = digitalroots(n);

        // 计算n^n的数根
        for(i=2; i<=n; i++) {
            ans = digitalroots(ans * nr);
        }

        // 输出结果
        printf("%d\n", ans);
    }

    return 0;
}

HDU1163 Eddy's digital Roots

时间: 2024-10-05 23:56:25

HDU1163 Eddy's digital Roots的相关文章

HDU1163 Eddy&#39;s digital Roots【九余数定理】

Eddy's digital Roots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4436    Accepted Submission(s): 2505 Problem Description The digital root of a positive integer is found by summing the digi

Hdu-1163 Eddy&#39;s digital Roots(九余数定理)

题意:对于一个数,只要它不是单个数,就把它各位的数相加,直到成为单个数. 九余数定理 一个数对九取余后的结果称为九余数. 一个数的各位数字之和想加后得到的<10的数字称为这个数的九余数(如果相加结果大于9,则继续各位相加) 因为数据挺大的,不能暴力.所以可以边乘边取余.很像快速幂. 1 #include<iostream> 2 #include<algorithm> 3 using namespace std; 4 5 int main(){ 6 int n; 7 while

HDU 1163 Eddy&#39;s digital Roots

Eddy's digital Roots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5783    Accepted Submission(s): 3180 Problem Description The digital root of a positive integer is found by summing the digit

HDOJ 1163 Eddy&#39;s digital Roots(简单数论)

[思路]:http://blog.csdn.net/iamskying/article/details/4738838 求解思路: 现在分析一个问题,假设将十位数为a,个位数为b的一个整数表示为ab,则推导得 ab*ab = (a*10+b)*(a*10+b) = 100*a*a+10*2*a*b+b*b 根据上式可得:root(ab*ab) = a*a+2*a*b+b*b = (a+b)*(a+b);[公式一] 同理也可证得:root(ab*ab*ab) = (a+b)*(a+b)*(a+b)

Eddy&#39;s digital Roots(九余数定理)

Eddy's digital Roots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5113    Accepted Submission(s): 2851 Problem Description The digital root of a positive integer is found by summing the digit

杭电 HDU 1163 Eddy&#39;s digital Roots

Eddy's digital Roots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4904    Accepted Submission(s): 2743 Problem Description The digital root of a positive integer is found by summing the digi

hdu 1163 Eddy&#39;s digital Roots(九余数定理)

hdu 1163 Eddy's digital Roots 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

Eddy&#39;s digital Roots

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4793    Accepted Submission(s): 2672 Problem Description The digital root of a positive integer is found by summing the digits of the integer. If

杭电 1163 Eddy&#39;s digital Roots

Eddy's digital Roots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4275    Accepted Submission(s): 2404 Problem Description The digital root of a positive integer is found by summing the digi