精品文档
第六章习题 1.
(1)#include
main() {
int i,j,k;
char space=' '; for(i=1;i<=4;i++) {
for(j=1;j<=i;j++) { printf(\ } for(k=1;k<=6;k++) { printf(\ } printf(\ } }
(2)#include
main() { int k = 4, n; for (n = 0; n < k; n++) { if (n % 2 == 0) continue; k--; } printf(\}
(3)#include
main() { int k = 4, n;
精品文档
精品文档
}
for (n = 0; n < k; n++) { if (n % 2 ==0) break; k--; }
printf(\
2.
(1)计算1+3+5+……99+101=
#include
int i,sum=0;
for(i=1;i<=101;i=i+2) {
sum=sum+i; }
printf(\ }
(2)计算1*2*3+3*4*5+...+99*100*101=
#include
long i;
long term,sum=0; for(i=1;i<=99;i=i+2) {
term=i*(i+1)*(i+2); sum=sum+term; }
printf(\}
精品文档
精品文档
(3)计算a+aa+aaa+...+aa...a(n个a)的值
#include
long term=0,sum=0; int a,i,n;
printf(\ scanf(\ for(i=1;i<=n;i=i++) {
term=term*10+a; sum=sum+term; }
printf(\}
(4 )计算1—1/2+1/3-...+1/99-1/100+...,直到最后一项的绝对值小于10#include
int n=1;
float term=1.0,sign=1,sum=0; while(fabs(term)>=1e-4) {
term=sign*1.0/n; sum=sum+term; sign=sign*(-1); n++; }
printf(\}
-4 。
(5) #include
#include
精品文档
精品文档
int n=1,count=1; float x;
double sum,term; printf(\ scanf(\ sum=x; term=x; do{
term=(-1)*term*x*x/((n+1)*(n+2)); sum=sum+term; n=n+2; count++;
}while(fabs(term)>=1e-5);
printf(\}
3. #include
main() { int x = 0,find=0;
while(!find) /*当没找到时继续执行循环*/ { x++; if(x%2==1&&x%3==2&&x%5==4&&x%6==5&&x%7==0) { printf(\ find=1; } } }
4. #include
main() {
int i,n;
long m=1,p=1; printf(\
精品文档
精品文档
scanf(\ for(i=1;i<=n;i++) {
m=i*i; p=i*i*i;
printf(\ } }
5. #include
main() { float c,f; for(c=-40;c<=110;c=c+10) { f=9/5*c+32; printf(\ } }
6. #include
#include
精品文档