摘要:
/* 从输入的n个成绩中选出最高分 */#includeint main(void){ int i,mark,max,n; printf("Enter n:"); scanf_s("%d",&n); printf("Enter %d marks:",n); scanf_s("%d",&mark); max=mark; for(i=1;i<n;i++){ scanf_s("%d",&mark); if(max<mark) max=mark; } printf(& 阅读全文
摘要:
/* 使用嵌套循环计算1!+2!+3!+…+100! */#includeint main(void){ int i,j; double item,sum; sum=0; for(i=1;i<=100;i++){ item=1; for(j=1;j<=i;j++) item=item*j; sum=sum+item; } printf("1!+2!+3!+…+100!=%e\n",sum); return 0;} 阅读全文
摘要:
/* 使用函数计算1!+2!+3!+…+100!*/#includedouble fact(int n);int main(void){ int i; double sum; sum=0; for(i=1;i<=100;i++) sum=sum+fact(i); printf("1!+2!+3!+…+100!=%e\n",sum); return 0;}double fact(int n){ int i; double result; result=1; for(i=1;i<=n;i++) result=r... 阅读全文
摘要:
/* 简单的猜数游戏 */#includeint main(void){ int mynumber=38; int count=0,yournumber; for(count=1;countmynumber) printf("Sorry!your number is bigger than my number!\n"); else printf("Sorry!your number is smaller than my number!\n"); } printf("Game is o... 阅读全文
摘要:
/* 判断正整数m是否为素数 */#includeint main(void){ int i,m; printf("Enter a number:"); scanf_s("%d",&m); for(i=2;im/2&&m!=1) printf("%d is a prime number!\n",m); else printf("No!\n"); return 0;} 阅读全文
摘要:
/* 统计一个整数的位数 */#includeint main(void){ int count,number; count=0; printf("Enter a number:"); scanf_s("%d",&number); if(number<0)number=-number; do{ number=number/10; count++; }while(number!=0); printf("It contains %d digits.\n",count); return 0;} 阅读全文
摘要:
/* 输入一批学生的成绩,以负数作为结束标志,计算平均成绩,并统计不及格人数 */#includeint main(void){ int count,num; double grade,total; num=0; total=0; count=0; printf("Enter grades:"); scanf_s("%lf",&grade); while(grade>=0){ total=total+grade; num++; if(grade<60) count++; ... 阅读全文
摘要:
/* 用格雷戈里公式计算π的近似值 */#include#includeint main(void){ int denominator,flag; double item,pi; flag=1; denominator=1; item=1.0; pi=0; while(fabs(item)>=0.0001){ item=flag*1.0/denominator; pi=pi+item; flag=-flag; denominator=denominator+2; } pi=pi*4; ... 阅读全文
摘要:
/* 求解简单的四则运算表达式 */#includeint main(void){ double value1,value2; char op; printf("Type in an expression:"); scanf_s("%lf%c%lf",&value1,&op,&value2); if(op=='+') printf("=%.2f\n",value1+value2); else if(op=='-') printf("=%.2f\n",value 阅读全文
摘要:
/* 统计字符,包括空格和回车、数字字符和其他字符 */#includeint main(void){ int blank,digit,other; char ch; int i; blank=digit=other=0; printf("Enter 10 characters:"); for(i=1;i<=10;i++){ ch=getchar(); switch (ch){ case' ': case'\n': blank++; break; ... 阅读全文