1.求最大公因数
int gcd(int x,int y){
return (x>=y)?(x%y==0?y:gcd(y,x%y)):(y%x==0?x:gcd(x,y%x));
}
2.求各位数字之和
int sum(int x){
int res;
while(x/10!=0){
res+=x%10;
x=x/10;
if(x/10==0)
res+=x;
}
return res;
}
3.数字反转
int reverse(int x){
int res=0;
int dig=0;
int y=x;
while(x/10!=0){
dig++;
x=x/10;
}
while(y/10!=0){
int temp=y%10;
res+=temp*pow(10,dig);
y=y/10;
dig--;
if(y/10==0)
res+=y;
}
return res;
}
本文介绍了三种基本算法:求最大公因数、求各位数字之和及数字反转。通过递归方式实现了最大公因数的计算,并提供了求解数字各位之和及数字反转的具体步骤。
726

被折叠的 条评论
为什么被折叠?



