Hang Dian 1163 digital root problem

Digital root


#include<stdio.h>
void main(){
    
    
	int a,i,n;
	while(~scanf("%d",&n)&&n){
    
    
		a=n;
		for(i=2;i<=n;i++)
			a=(a*n)%9;
		if(a==0) printf("9\n");
		else printf("%d\n",a);
}}

Summary
// Divide a number by 9 and take the remainder = the digital root of this number;
//In the process of finding the digital root of the product of multiple elements, you can replace this element with the digital root of the element without affecting the final result. (Eg: 612 81->9 => 9 9->9)

Here is the code required to write my own digital root of it is how subtle and perfect! Combined with the knowledge of base number conversion, what kind of a smart brain is this!

#include<stdio.h>
#include<stdlib.h>
void main(){
    
    
	__int64 n;
	while(~scanf("%I64d",&n)&&n){
    
    
		int i=0;
		while(n>=10){
    
    
			__int64 a[50];
			while(n){
    
    
				n/=10;
				a[i++]=n%10;
			}
			while(i--)
				n+=a[i];
		}
		printf("%I64d\n",n);
}}

Guess you like

Origin blog.csdn.net/cwindyc/article/details/107009732