c语言求输入的3个数中的最大值.docVIP

  • 32
  • 0
  • 约 3页
  • 2017-06-07 发布于重庆
  • 举报
c语言求输入的3个数中的最大值

求输入的3个数中的最大值。 main() { int num1,num2,num3,max; printf(“please input three numbers:”); scanf(“%d,%d,%d”,num1,num2,num3); if(num1num2) {if(num1num3) max=num1; else max=num3;} else {if(num2num3) max=num2; else max=num3;} printf(“The three numbers are:%d,%d,%d\n”,num1,num2,num3); printf(“max=%d\n”,max); } 用π/4≈1-1/ 3+1/5-1/7+1/9… 求π近似的值。 直到最后一项的绝对值小于10-6为止。 #include math.h main() { int s; float n , t , sum, pi; t=1; sum=0 ; n=1 ; s=1; while(fabs(t)=.000001) { sum=sum+t; n=n+2; s= -s; t=s/n; } pi=sum*4; printf(pi=%f\n,pi); } 求出100~200以内的所有素数。 main() { int m , k,i ,n=0; for(m=101;m=200;m=m+2) { k=sqrt(m); for(i=2;i=k ;i++) if(m % i= =0) break; if(i=k+1) { printf( %d,m); n=n+1;} if( n%10= =0) printf(\n); } printf(“\n”); } 用冒泡法对10个数排序(由小到大)。 main() { int a[11], i , j , t ; printf(“Input 10 numbers : \n”); for(i=1 ; i11 ; i++) scanf(“%d”,a[i]); printf(“\n”); for(j=1 ; j=9 ; j++) /* 控制9轮排序 */ for(i=1 ; i=10-j ; i++) /* 每轮排序比较次数*/ if(a[i]a[i+1]) { t=a[i]; a[i]=a[i+1]; a[i+1]=t; } for(i=1 ; i=10 ; i++) printf(%d,a[i]); } 使用数组保存“九九表”,然后输出。\ main() { int a99[10][10], i, j ; for(i=1; i 10; i++) for(j=1; j=i; j++) a99[i][j]=i*j; for(i=1; i10; i++) { for(j=1;j=i; j++) printf(“%6d,a99[i][j]); printf(“\n”); } } 统计字符串中的单词数 #include stdio.h main() { char string[81],c; int i,num=0,word=0; gets(string); for(i=0;string[i]!=\0;i++) {if(string[i]==‘ ’) word=0; else if(word= =0){word=1;num++;}} printf(“\nThere are %d words.”, num); } 把输入字符中的小写字符转换成大写并输出 #include stdio.h main() { char ch; scanf(%c,ch); ch=(ch=a ch=z) ? ch-32: ch; printf(“%c”,ch); } 已知基本运费、货重,以及路程(s)和折扣的关系标准如下 s250km 没有折扣 250≤s500 2 %折扣 500≤s1000

文档评论(0)

1亿VIP精品文档

相关文档