C语言入门到中等难度学习资料

  • 格式:pdf
  • 大小:147.51 KB
  • 文档页数:20
case 7:printf("C\n");break; case 6:printf("D\n");break; default:("E\n"); }
} 17.大写字母转换成小写字母 #include<stdio.h>
3
main() {
char ch; printf("请输入一个字母:\n"); scanf("%c",&ch); ch=(ch>='A'&&ch<='Z')?(ch+32):ch; printf("%c",ch); } 18.两数判断大小 #include <stdio.h> main() { int x,y; printf("请输入 x 和 y:\n"); scanf("%d%d",&x,&y); if(x!=y)
int a,b,j,k; scanf("%d%d",&a,&b); j=a/b; k=a%b; printf("商 j=%1.0d,余数 k=%1.0d\n",j,k); } 9.三个数求平均值 #include <stdio.h> main() { double a,b,c,d; scanf("%lf%lf%lf",&a,&b,&c); d=(a+b+c)/3; printf("a=%lf,b=%lf,c=%lf,d=%2.1lf\n ",a,b,c,d); } 10.三个数互相交换,然后输出 #include <stdio.h> main() { int a,b,c,t; scanf ("%d%d%d",&a,&b,&c); printf("a=%db=%dc=%d\n",a,b,c); t=c; c=b; b=a; a=t; printf("a=%d,b=%d,c=%d\n",a,b,c); } 11.四舍五入 保留两位小数 #include <stdio.h>
printf("max=x=%d\n",x); else
printf("max=y=%d\n",y); printf("**end**\n"); } 16.给学生成绩划分等级 #include <stdio.h> main() { int g; printf("请输入学生成绩:\n"); scanf("%d",&g); printf("g=%d:",g);
#include <stdio.h> main() {
int i,k,m,n;
1
i=560; k=60; m=i/k; n=i-m*k; printf("m=%1.0d 小时,n=%2.0d 分 钟\n",m,n); } 8.输入两个整数:1500 和 350,求出它 们的商和余数并进行输出
#include <stdio.h> main() {
main() {
double a; scanf("%lf",&a); printf("a=%lf\n",a); a=a*100; a=a+0.5; a=(int)a; a=a/100; printf("a=%lf\n",a); } 12.a>b 交换 #include <stdio.h> main() { float a,b,t; scanf("%f%f",&a,&b); if(a>b) {t=a;a=b;b=t;} printf("%5.2f,%5.2f\n",a,b); } 13.把三位数拆开 #include <stdio.h> main() { int x,y,z,f,d; printf("请输入一个三位数:\n"); scanf("%d",&x); printf("%d\n",x); y=x/100; z=x/10; f=z%10; d=x%10; printf("%d\n",y); printf("%d\n",f); printf("%d\n",d); } 14.三个数从小到大排列 #include<stdio.h> main() { float a,b,c,t; printf("请输入三个数:\n"); scanf("%f,%f,%f",&a,&b,&c);
2
if(a>b) {t=a,a=b,b=t;} if(a>c) {t=a,a=c,c=t;} if(b>c) {t=b,b=c,c=t;} printf("%5.2f,%5.2f,%5.2f\n",a,b,c); } 15.两个数,输出较大的 #include <stdio.h> main() { int x,y; printf("请输入两个整数 x、y:\n"); scanf("%d%d",&x,&y); printf("x,y:%d%d\n",x,y); if(x>y)
#include <stdio.h> #include <math.h> int main() {double a,b,c,disc,x1,x2,p,q; scanf("%lf%lf%lf",&a,&b,&c), disc=b*b-4*a*c; p=-b/(2.0*a);
q=sqrt(disc)/(2.0*a); x1=p+q,x2=p-q; printf("x1=%7.2f\nx2=%7.2f\n",x1,x2); return 0; } 5.交换 x 和 y #include <stdio.h> main() { int x,y,t; scanf ("%d%d",&x,&y); printf("x=%dy=%d\n",x,y); t=x; x=y; y=t; printf("x=%dy=%d\n",x,y); } 6.作业 #include <stdio.h> main() { int x1,x2,x3;float a1,a2,a3;double b1,b2,b3;char y1,y2,y3; printf ("学号:20140722,姓名:舒磊,班 级:通信一班");
printf("x=%d\n",x); } 21.1——100 的累加 #de<stdio.h> main() {
int i,n; i=1; n=0; while(i<=100) { n=n+i; i++; } printf("1+2+...+100=%d\n",n); } 22. 300 以内能被 5 同时被 7 整除的 do-while 型 #include<stdio.h> main() { int x; x=1; do {if(x%5==0&&x%7==0)
} #include <stdio.h> main() {
int g; printf("请输入学生成绩:\n"); scanf("%d",&g); printf("g=%d:",g); switch(g/10) {case 10: case 9:printf("A\n");break; case 8:printf("B\n");break;
#include<stdio.h> #include<math.h> main() {int s; float n,t,pi; t=1.0; pi=0; n=1.0; s=1; while(fabs(t)>=1e-6) { pi=pi+t; n+=2.0; s=-s; t=s/n; } pi=pi*4; printf("pi=%f\n",pi); } #include<stdio.h> #include<math.h> main() {int s; float n,t,pi; t=1.0; pi=0; n=1.0; s=1; do {
printf("%d ",x); x++; } while(x<=300);
} 23.求 1^2+2^2+……n^2,直到累加和 大于等于 10000 为止 #include<stdio.h> main() {int i,sum; i=0; sum=0; while(sum<10000) { sum+=i*i; i++;} printf("n=%d sum=%d\n",i-1,sum); } #include<stdio.h>
scanf ("%d%d%d",&x1,&x2,&x3); printf("x1=%d,x2=%d,x3=%d\n",x1,x2,x3) ; scanf ("%f,%f,%f",&a1,&a2,&a3); printf("a1=%2.0f,a2=%2.0f,a3=%2.0f\n", a1,a2,a3); scanf ("%lf,%lf,%lf",&b1,&b2,&b3); printf("b1=%2.0f,b2=%2.0f,b3=%2.0f\n", b1,b2,b3); scanf ("%c%c%c",&y1,&y2,&y3); printf("y1=%c,y2=%c,y3=%c\n",y1,y2,y3) ; } 7.把 560 分钟换算成用小时和分钟表 示,然后进行输出