求
解(1):用while循环
#include<stdio.h>
#include<math.h>
int main()
{
double k = 1, sum = 0, sum1 = 0, sum2 = 0, sum3 = 0;
while (1)
{
if (k > 100)
{
k = 1;
break;
}
sum1 += k;
k++;}
while (1)
{
if (k > 50)
{
k = 1;
break;
}
sum2 = sum2 + (k * k);
k++;
}
while (1)
{
if (k > 10)
break;
sum3 = sum3 + (1 / k);
k++;
}
sum = sum1 + sum2 + sum3;
printf("%15.6f\n",sum);
return 0;
}
解:(2)用for循环
#include<stdio.h>
#include<math.h>
int main()
{
double k, sum = 0, sum1 = 0, sum2 = 0, sum3 = 0;
for ( k = 1; k <=100; k++)
{
sum1 += k;
}
for ( k = 1; k <=50; k++)
{
sum2 = sum2 + (k * k);
}
for ( k = 1; k <=10; k++)
{
sum3 = sum3 + (1 / k);
}
sum = sum1 + sum2 + sum3;
printf("%15.6f\n", sum);
return 0;
}