妈祖文化的课本:C语言高手快来啊

来源:百度文库 编辑:高考问答 时间:2024/05/05 15:08:37
1、将[1,1000000]中个位数字是6的数,把个位数字调到第一位,其它的各位均向后推一位,
得到的数是原数的4 倍。
(1) 求所有满足要求的数的数目。 (2) 求所有满足要求的数的和。2.编写函数fun,它的功能是:计算并输出下列级数和:
s=1/(1*2)+1/(2*3)+ …+1/(n*(n+1))
例如,当n=10时,函数值为:0.909091。

第一题:
#include <math.h>

main()
{
long oldnum,newnum;
int bit;
long save;
long sum = 0;
int num = 0;

for(oldnum=1; oldnum<=1000000; oldnum++)
{
if(oldnum%10 == 6)
{
save = oldnum;
bit = 0;
while(save)
{
bit++;
save /= 10;
}
newnum = oldnum/10 + 6*pow(10, bit-1);

if(newnum == oldnum*4)
{
num++;
sum += oldnum;
}
}
}
printf("The number of the data which satisfy the condition is: %d\n", num);
printf("The summary of the data is: %ld", sum);
getch();
}

第二题:
float fun(int n)
{
float s = 0;
int i;

for(i=1; i<=n; i++)
{
s += (float)1.0/(i*(i+1));
}
return s;
}

main()
{
int n;
printf("enter n: ");
scanf("%d", &n);
printf("The result is: %f\n", fun(n));
getch();
}