循环应用
#include <stdio.h>
int main()
{
int n;
int i;
double sum = 0.0;
scanf("%d", &n);
for ( i = 1; i<n; i++ )
{
sum += 1.0/i;
}
printf("f(%d)=%f\n", n, sum);
return 0;
}
#include <stdio.h>
int main()
{
int n;
int i;
double sum = 0.0;
int sign = 1;
scanf("%d", &n);
for ( i = 1; i<n; i++ )
{
sum += sign*1.0/i;
sign = -sign;
}
printf("f(%d)=%f\n", n, sum);
return 0;
}
最大公约数
//辗转相除法
#include <stdio.h>
int main()
{
int a,b;
int t;
scanf("%d %d", &a, &b);
while ( b!=0 )
{
t = a%b;
a=b;
b=t;
printf("a=%d,b=%d,t=%d\n", a, b, t);
}
printf("acd=%d\n", a);
return 0;
}
正序整数分解
#include <stdio.h>
int main()
{
int x;
scanf("%d", &x);
int t = 0;
do
{
int d = x%10;
t = t*10+d;
x /= 10
}
while ( x>0 );
x = t;
do
{
int d = x % 10;
printf("%d", d);
if ( x > 9 )
{
printf(" ");
}
x /= 10;
}
while ( x>0 );
printf("\n");
}
//另一个
int mask = 1;
int t = x;
while ( t>9 )
{
t /= 10;
mask *= 10;
}
printf("mask=%d\n", mask);
//pow(底数, 次数) 指数函数
do
{
int d = x/mask;
printf("%d", d);
if ( mask > 9 )
{
printf(" ");
}
x %= mask;
mask /= 10;
}
while ( mask > 0 );
printf("\n");