admin 管理员组文章数量: 887021
2024年2月24日发(作者:active learning课件)
编程题
实验1-1 Hello World! (5 分)
#include
int main()
{
printf("Hello World!");
return 0;
}
实验1-2 Welcome to You! (5 分)
#include
int main()
{
printf("Welcome to You!");
return 0;
}
实验1-3 Programming in C is fun! (5 分)
#include
int main()
{
printf("Programming in C is fun!");
return 0;
}
实验1-4 输出三角形 (5 分)
#include
int main()
{
int i,n=4;
printf("****n");
printf("***n");
printf("**n");
printf("*n");
return 0;
}
实验1-5 输出菱形图案 (5 分)
#include
int main()
{
printf(" An");
printf("A An");
printf(" An");
return 0;
}
实验1-6 输出带框文字 (5 分)
#include
int main()
{
printf("************n");
printf(" Welcomen");
printf("************n");
return 0;
}
实验1-7 What is a computer? (5 分)
#include
int main()
{
printf("What is a computer?");
return 0;
}
实验1-8 输出倒三角图案 (5 分)
#include
int main()
{
printf("* * * *n");
printf(" * * *n");
printf(" * *n");
printf(" *n");
return 0;
}
实验2-1-1 计算摄氏温度 (5 分)
#include
int main()
{
printf("fahr = 100, celsius = %d",5*(100-32)/9);
return 0;
}
实验2-1-2 温度转换 (5 分)
#include
int main()
{
printf("fahr = 150, celsius = %d",5*(150-32)/9);
return 0;
}
实验2-1-3 计算物体自由下落的距离 (5 分)
#include
int main()
{
printf("height = 45.00");
return 0;
}
实验2-1-4 计算平均分 (5 分)
#include
int main(void)
{
printf("math = 87, eng = 72, comp = 93, average = %d", (87 + 72 + 93) / 3);
return 0;
}
实验2-1-5 将x的平方赋值给y (5 分)
#include
int main()
{
printf("9 = 3 * 3n");
printf("3 * 3 = 9");
return 0;
}
实验2-1-6 计算华氏温度 (5 分)
#include
int main()
{
printf("celsius = 26, fahr = %d",9*26/5+32);
return 0;
}
实验2-1-7 整数152的各位数字 (10 分)
#include
int main()
{
printf("152 = 2 + 5*10 + 1*100");
return 0;
}
实验2-2-1 计算分段函数[1] (10 分)
#include
int main()
{
float x,y;
scanf("%f",&x);
if(x!=0)
y=1/x;
else
y=0.0;
printf("f(%.1f) = %.1f",x,y);
return 0;
}
实验2-2-2 计算摄氏温度 (10 分)
#include
int main()
{
int f;
scanf("%d",&f);
printf("Celsius = %d",5*(f-32)/9);
return 0;
}
实验2-2-3 计算存款利息 (10 分)
#include
#include
int main()
{
float money, year, rate;
scanf("%f %f %f",&money, &year, &rate);
printf("interest = %.2f",money*pow((1+rate),year)-money);
return 0;
}
实验2-2-4 计算分段函数[2] (10 分)
#include
#include
int main()
{
float x,result;
scanf("%f",&x);
if(x>=0)
result=sqrt(x);
else
result=pow(x+1,2)+2*x+1/x;
printf("f(%.2f) = %.2f",x,result);
return 0;
}
实验2-2-5 求整数均值 (10 分)
#include
int main()
{
int sum = 0, a[4];
for(int i = 0; i < 4;i++)
{
scanf("%d",&a[i]);
sum += a[i];
}
printf("Sum = %d; Average = %.1f",sum,sum/4.0);
return 0;
}
实验2-2-6 计算分段函数[3] (10 分)
#include
int main()
{
float x, y;
scanf("%f",&x);
if(x != 10)
y = x;
else
y = 1.0/x;
printf("f(%.1f) = %.1f",x, y);
return 0;
}
实验2-2-7 整数四则运算 (10 分)
#include
int main()
{
int a, b;
scanf("%d %d",&a,&b);
printf("%d + %d = %dn",a,b,a+b);
printf("%d - %d = %dn",a,b,a-b);
printf("%d * %d = %dn",a,b,a*b);
printf("%d / %d = %dn",a,b,a/b);
return 0;
}
实验2-2-8 阶梯电价 (15 分)
#include
int main()
{
int ele;
float cost;
scanf("%d",&ele);
if(ele <= 50)
cost = ele*0.53;
else
cost = 50*0.53+(ele - 50)*(0.53 + 0.05);
if(ele >= 0)
printf("cost = %.2f",cost);
else
printf("Invalid Value!");
return 0;
}
实验2-2-9 计算火车运行时间 (15 分)
#define _CRT_SECURE_NO_DEPRECATE
#include
int main()
{
int str, end, str_h, str_m, end_h, end_m;
scanf("%d %d", &str, &end);
str_h = str / 100; str_m = str % 100;
end_h = end / 100; end_m = end % 100;
if (end_m - str_m < 0)
printf("%02d:%02d", end_h - str_h - 1, 60 - str_m + end_m);
else
printf("%02d:%02d", end_h - str_h, end_m - str_m);
return 0;
}
实验2-3-1 求1到100的和 (10 分)
#include
int main()
{
int sum = 0;
for(int i = 1;i <= 100;i++)
sum += i;
printf("sum = %d",sum);
return 0;
}
实验2-3-2 求N分之一序列前N项和 (15 分)
#define _CRT_SECURE_NO_DEPRECATE
#include
int main()
{
int n;
double s = 0;
scanf("%d", &n);
for (int i = 1; i <= n; i++)
s += 1.0 / i;
printf("sum = %.6lf", s);
}
return 0;
实验2-3-3 求奇数分之一序列前N项和 (15 分)
#define _CRT_SECURE_NO_DEPRECATE
#include
int main()
{
int n;
double sum = 0, t = 1;
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
sum += 1.0 / t;
t += 2;
}
printf("sum = %.6lf", sum);
return 0;
}
实验2-3-4 求简单交错序列前N项和 (15 分)
#define _CRT_SECURE_NO_DEPRECATE
#include
int main()
{
int n;
double sum = 0, x = 1;
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
if (i % 2 == 0)
sum += 1.0 / x;
else
sum -= 1.0 / x;
x += 3;
}
printf("sum = %.3lf", sum);
return 0;
}
实验2-3-5 输出华氏-摄氏温度转换表 (15 分)
#define _CRT_SECURE_NO_DEPRECATE
#include
#include
int main()
{
int lower, upper;
scanf("%d %d", &lower, &upper);
if (lower > upper || upper > 100)
{
printf("Invalid.");
exit(0);
}
printf("fahr celsiusn");
do
{
printf("%d%6.1fn", lower, 5.0 * (lower - 32) / 9);
lower += 2;
} while (lower <= upper);
return 0;
}
实验2-3-6 求交错序列前N项和 (15 分)
#include
int main()
{
int n;
float fenz, fenm, sum = 0;
fenz = fenm = 1;
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
if (i % 2 == 0)
sum += fenz / fenm;
else
sum -= fenz / fenm;
fenz++;
fenm += 2;
}
printf("%.3f", sum);
return 0;
}
实验2-3-7 求平方与倒数序列的部分和 (15 分)
#define _CRT_SECURE_NO_DEPRECATE
#include
int main()
{
int m, n, i = 0;
double sum = 0;
scanf("%d %d", &m, &n);
while (m <= n)
{
if (i % 2 == 0)
sum += m * m;
else
{
sum += 1.0 / m;
m++;
}
i++;
}
printf("sum = %.6lf", sum);
return 0;
}
实验2-4-2 生成3的乘方表 (15 分)
#define _CRT_SECURE_NO_DEPRECATE
#include
#include
int main()
{
int n;
scanf("%d", &n);
for (int i = 0; i <= n; i++)
printf("pow(3,%d) = %.0fn", i, pow(3, i));
return 0;
}
实验2-4-3 求平方根序列前N项和 (15 分)
#define _CRT_SECURE_NO_DEPRECATE
#include
#include
int main()
{
int n;
double sum = 0;
scanf("%d", &n);
for (int i = 1; i <= n; i++)
sum += sqrt(i);
printf("sum = %.2f", sum);
return 0;
}
实验2-4-4 求阶乘序列前N项和 (15 分)
#define _CRT_SECURE_NO_DEPRECATE
#include
int fac(int n);
int main()
{
int n, sum = 0;
scanf("%d", &n);
for (int i = 1; i <= n; i++)
sum += fac(i);
printf("%d", sum);
return 0;
}
int fac(int n)
{
int s = 1;
for (int i = 1; i <= n; i++)
s *= i;
return s;
}
实验2-4-6 求幂之和 (15 分)
#define _CRT_SECURE_NO_DEPRECATE
#include
#include
int main(void)
{
int n, sum = 0;
scanf("%d", &n);
for (int i = 1; i <= n; i++)
sum += pow(2, i);
printf("result = %d", sum);
return 0;
}
实验2-4-7 求组合数 (15 分)
#include
double fact(int n);
int main()
{
int m,n;
double sum;
scanf("%d %d",&m,&n);
sum=fact(n)/(fact(m)*fact(n-m));
printf("result = %.0lf",sum);
return 0;
}
double fact(int x)
{
double s=1;
int i;
for( i=1;i<=x;i++)
s*=i;
return s;
}
实验3-1 求一元二次方程的根 (20 分)
#define _CRT_SECURE_NO_DEPRECATE
#include
#include
int main(void)
{
float a, b, c, de;
scanf("%f %f %f", &a, &b, &c);
de = b * b - 4 * a * c;
if (a == 0 && b == 0)
{
if (c != 0)
printf("Not An Equation");
else
printf("Zero Equation");
}
else if (a == 0 && b != 0)
{
if (c != 0)
printf("%.2f", -c / b);
else
printf("%.2f", 0);
}
else if (de > 0)
printf("%.2fn%.2f", (-b + sqrt(de)) / (2 * a), (-b - sqrt(de)) / (2 * a));
else if (de == 0)
printf("%.2f", -b / (2 * a));
else
{
if (-b / (2 * a) != 0)
{
printf("%.2f+%.2fin", -b / (2 * a), sqrt(-de) / (2 * a));
printf("%.2f%.2fin", -b / (2 * a), -sqrt(-de) / (2 * a));
}
else
{
printf("0.00+%.2fin", sqrt(-de) / (2 * a));
printf("0.00%.2fin", -sqrt(-de) / (2 * a));
}
}
return 0;
}
实验3-2 计算符号函数的值 (10 分)
#include
#include
int main()
{
int n, y;
scanf("%d",&n);
if(n < 0)
y = -1;
else if(n == 0)
y = 0;
else
y = 1;
printf("sign(%d) = %d", n, y);
return 0;
}
实验3-3 比较大小 (10 分)
#include
int main()
{
int a[3], temp;
for(int i = 0; i < 3; i++)
scanf("%d", &a[i]);
for(int i = 0; i < 3; i++)
{
for(int j = i; j < 3; j++)
{
if(a[i] > a[j])
{
temp = a[j];
a[j] = a[i];
a[i] = temp;
}
}
}
printf("%d->%d->%d",a[0], a[1], a[2]);
return 0;
}
实验3-4 统计字符 (15 分)
#define _CRT_SECURE_NO_DEPRECATE
#include
int main()
{
int letter, blank, digit, other;
char a[10];
letter = blank = digit = other = 0;
for (int i = 0; i < 10; i++)
scanf("%c", &a[i]);
for (int i = 0; i < 10; i++)
{
if (a[i] >= 'a' && a[i] <= 'z' || a[i] >= 'A' && a[i] <='Z')
letter++;
else if (a[i] == ' ' || a[i] == 'n')
blank++;
else if (a[i] >= '0' && a[i] <= '9')
digit++;
else
other++;
}
printf("letter = %d, blank = %d, digit = %d, other = %d", letter, blank, digit, other);
return 0;
}
实验3-5 查询水果价格 (15 分)
#include
#include
int main()
{
int i,choice;
printf("[1] applen[2] pearn[3] orangen[4] grapen[0] exitn");
for(i = 0;i < 5;i++)
{
scanf("%d",&choice);
switch(choice)
{
case 1:printf("price = 3.00n");
break;
case 2:printf("price = 2.50n");
break;
case 3:printf("price = 4.10n");
break;
case 4:printf("price = 10.20n");
break;
case 0:
exit(0);
default:printf("price = 0.00n");
break;
}
}
return 0;
}
实验3-6 计算个人所得税 (10 分)
#include
int main()
{
float s, rate, salary;
scanf("%f",&salary);
if(salary <= 1600)
s = 0;
else if(salary <= 2500)
s = 0.05*(salary - 1600);
else if(salary <= 3500)
s = 0.1*(salary - 1600);
else if(salary <= 4500)
s = 0.15*(salary - 1600);
else
s = 0.2*(salary - 1600);
printf("%.2f",s);
return 0;
}
实验3-7 统计学生成绩 (15 分)
#define _CRT_SECURE_NO_DEPRECATE
#include
int main()
{
int n, a, b, c, d, e, score;
a = b = c = d = e = 0;
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
scanf("%d", &score);
if (score >= 90)
a++;
else if (score >= 80)
b++;
else if (score >= 70)
c++;
else if (score >= 60)
d++;
else
e++;
}
printf("%d %d %d %d %d", a, b, c, d, e);
return 0;
}
实验3-8 输出三角形面积和周长 (15 分)
#define _CRT_SECURE_NO_DEPRECATE
#include
#include
int main()
{
float a, b, c, s;
scanf("%f %f %f", &a, &b, &c);
s = (a + b + c) / 2;
if (a + b > c && a + c > b && b + c > a)
printf("area = %.2f; perimeter = %.2f", sqrt(s * (s - a) * (s - b) * (s - c)), 2 * s);
else
printf("These sides do not correspond to a valid triangle");
return 0;
}
实验3-9 三天打鱼两天晒网 (15 分)
#include
int main()
{
int n,x;
scanf("%d",&n);
if(n <= 5){
if(x - 3 <= 0)
printf("Fishing in day %d",n);
else
printf("Drying in day %d",n);
}
else{
x = n%5;
if(x == 0)
printf("Drying in day %d",n);
else if(x - 3 <= 0)
printf("Fishing in day %d",n);
else
printf("Drying in day %d",n);
}
return 0;
}
实验3-10 高速公路超速处罚 (15 分)
#define _CRT_SECURE_NO_DEPRECATE
#include
int main()
{
int v, limv;
scanf("%d %d", &v, &limv);
if ((float)v / limv - 1 < 0.1)
printf("OK");
else if ((float)v / limv - 1 < 0.5)
printf("Exceed %.0f%%. Ticket 200", ((float)v / limv - 1) * 100);
else
printf("Exceed %.0f%%. License Revoked", ((float)v / limv - 1) * 100);
return 0;
}
实验3-11 计算油费 (15 分)
#define _CRT_SECURE_NO_DEPRECATE
#include
int main()
{
int a, b;
char c;
scanf("%d %d %c", &a, &b, &c);
if (b == 90)
{
if (c == 'm')
printf("%.2f", a * 6.95 * 0.95);
if(c=='e')
printf("%.2f", a * 6.95 * 0.97);
}
if (b == 93)
{
if (c == 'm')
printf("%.2f", a * 7.44 * 0.95);
if (c == 'e')
printf("%.2f", a * 7.44 * 0.97);
}
if (b == 97)
{
if (c == 'm')
printf("%.2f", a * 7.93 * 0.95);
if (c == 'e')
printf("%.2f", a * 7.93 * 0.97);
}
return 0;
}
实验4-1-1 最大公约数和最小公倍数 (15 分)
#include
int main()
{
int i,a,b,r,d;
scanf("%d %d",&a,&b);
r=a*b;
if(a
i=a;
a=b;
b=i;
}
do
{
i=a%b;
a=b;
b=i;
}while(i!=0);
d=r/a;
printf("%d %d",a,d);
return 0;
}
实验4-1-2 求奇数和 (15 分)
#include
int main()
{
int n,sum;
scanf("%d",&n);
while(n>0){
if(n%2!=0)
sum += n;
scanf("%d",&n);
}
printf("%d",sum);
return 0;
}
实验4-1-3 找出最小值 (20 分)
#include
int main()
{
int i,m,n,min;
scanf("%d",&n);
scanf("%d",&m);
min=m;
for(i=1;i scanf("%d",&m); if(m min=m; } printf("min = %d",min); return 0; } 实验4-1-4 求整数的位数及各位数字之和 (15 分) #include int main() { int n,w=1,sum=0; scanf("%d",&n); while(n/10>0){ sum += n%10; w++; n=n/10; } printf("%d %d",w,sum+n); return 0; } 实验4-1-5 韩信点兵 (10 分) #include int main() { int i; for(i=1; ;i++){ if((i-1)%5==0) if((i-5)%6==0) if((i-4)%7==0) if((i-10)%11==0) break; } printf("%d",i); return 0; } 实验4-1-6 求分数序列前N项和 (15 分) #include int main() { int n,i; double a = 2, b = 1,r,sum = 0; r = b; scanf("%d", &n); for (i = 1; i <= n; i++){ sum += a / b; b = a; a = a + r; r = b; } printf("%.2f",sum); return 0; } 实验4-1-7 特殊a串数列求和 (20 分) #include int main() { int i,a,r,n,s = 0; scanf("%d %d",&a,&n); r = a; for(i = 1;i <= n;i++){ s += a; a = (a*10)+r; } printf("s = %d",s); return 0; } 实验4-1-8 求给定精度的简单交错序列部分和 (15 分) #include #include int main() { int i = 1, flag = 1; double tmp, eps, sum = 0; scanf( "%lf", &eps ); do { tmp = flag * 1.0 / ( 3 * i - 2 ); i++; sum+= tmp; flag = -flag; }while( fabs( tmp ) > eps ); printf( "sum = %fn", sum ); return 0; } 实验4-1-9 猜数字游戏 (15 分) #define _CRT_SECURE_NO_DEPRECATE #include #include int main() { int s, n, t = 0, a; //s为随机数,n为猜测最大次数 ,t为猜的次数 ,a为用户每次输入的数 scanf("%d %d", &s, &n); for (int i = 1; ; i++) { scanf("%d", &a); t++; if (t > n) { printf("Game Overn"); exit(0); } if (a < 0) { printf("Game Overn"); exit(0); } if (a > s) printf("Too bign"); else if (a < s) printf("Too smalln"); } else break; } if (t == 1) printf("Bingo!n"); else if (t > 1 && t <= 3) printf("Lucky You!n"); else if (t > 3 && t <= n) printf("Good Guess!n"); return 0; 实验4-1-10 兔子繁衍问题 (15 分) #include int main() { int n,a = 1,b = 1,c,month = 2,i; scanf("%d",&n); if(n==1) printf("1"); else{ for(i = 1; ;i++){ c = b; b = a; a = a+c; month++; if(a >= n) break; } printf("%d",month); } return 0; } 实验4-1-11 高空坠球 (20 分) #include #include #include int main() { double a,h,sum; int n,i; } scanf("%lf %d",&h,&n); sum = h; if(n == 0){ printf("0.0 0.0"); exit(0); } for(i = 1;i < n;i++){ a = h/pow(2,i - 1); sum += a; } for(i = 1;i <= n;i++) h = h/2; printf("%.1lf %.1lf",sum,h); return 0; 实验4-1-12 黑洞数 (20 分) #include int max(int a,int b,int c); int min(int a,int b,int c); int main() { int i,j,n,t = 0; int a[4]; scanf("%d",&n); for(i = 1; ;i++){ t++; a[1] = n%10;a[2] = n/10%10;a[3] = n/100; n = max(a[1],a[2],a[3]) - min(a[1],a[2],a[3]); if(n != 495) printf("%d: %d - %d = %dn",t,max(a[1],a[2],a[3]),min(a[1],a[2],a[3]),n); else{ printf("%d: %d - %d = %d",t,max(a[1],a[2],a[3]),min(a[1],a[2],a[3]),n); break; } } return 0; } int max(int a,int b,int c) { int t,i,j; int m[4]={0}; m[1]=a;m[2]=b;m[3]=c; for(i = 1;i <= 3;i++){ for(j = 1;j <= 4-j;j++) if(m[j] t=m[j];m[j]=m[j+1];m[j+1]=t; } } return m[1]*100+m[2]*10+m[3]; } int min(int a,int b,int c) { int t,i,j; int m[4]={0}; m[1]=a;m[2]=b;m[3]=c; for(i = 1;i <= 3;i++){ for(j = 1;j <= 4-j;j++) if(m[j] t=m[j];m[j]=m[j+1];m[j+1]=t; } } return m[3]*100+m[2]*10+m[1]; } 实验4-2-2 求e的近似值 (15 分) #include double jie(int n); int main() { int i,n; double e = 1; scanf("%d",&n); for(i = 1;i <= n;i++){ e += 1/jie(i); } printf("%.8lf",e); return 0; } double jie(int n) { int i; double s = 1; for(i = 1;i <= n;i++) s *= i; return s; } 实验4-2-3 验证“哥德巴赫猜想” (20 分) #include #include int isPrime(int n); int main() { int n,i,p,q; scanf("%d",&n); for(i = 2;i <= n/2;i++){ if(isPrime(i)&&isPrime(n-i)){ printf("%d = %d + %d",n,i,n-i); break; } } return 0; } int isPrime(int n) { int div; for(div = 2;div <= sqrt(n);div++){ if(n%div == 0) //break; return 0; } return 1; } 实验4-2-4 换硬币 (20 分) #include int main() { int x,total = 0,count = 0; //x为零钱数额,total为硬币总数量,count为换法个数 int a,b,c; scanf("%d",&x); for(a = x/5;a > 0;a--) for(b = x/2;b > 0;b--) for(c = x;c > 0;c--) if(a*5+b*2+c == x){ count++; printf("fen5:%d, fen2:%d, fen1:%d, total:%dn",a,b,c,a+b+c); } printf("count = %d",count); return 0; } 实验4-2-5 水仙花数 (20 分) #include #include void fun(int ar[8],int m); int main() { int n,j,k,s; int sum,min,max,m; int a[8]={0}; scanf("%d",&n); min = pow(10,n - 1); max = pow(10,n)-1; for(j = min;j <= max;j++){ if(j > 5000000&&j< 9000000||j>2000000&&j<4000000) 决n为7时的运行超时问题 continue; m = j; fun(a,m); sum = 0; for(k = 1;k <= n;k++) sum += pow(a[k],n); if(sum == j) printf("%dn",j); } return 0; } void fun(int ar[8],int m) { int i = 1; while(m){ ar[i] = m%10; m = m/10; if(m < 10) ar[i+1] = m; i++; //我这一步只是为了解 } } 实验4-2-6 输出三角形字符阵列 (15 分) #include int main() { int n,i; char ch = 'A'; scanf("%d",&n); do{ for(i = 1;i <= n;i++){ printf("%c ",ch); ch++; } putchar('n'); n--; }while(n); return 0; } 实验4-2-7 找完数 (20 分) #include void wanshu(int n); int x = 0; //定义一个全局变量 int main() { int m,n,i,count = 0; scanf("%d %d",&m,&n); for(i = m;i <= n;i++){ wanshu(i); } if(x==0) printf("None"); return 0; } void wanshu(int n) { int i,sum = 0,a[100] = {0},t = 0,s; for(i = 1;i <= n/2;i++){ if(n%i == 0){ t++; //n最后会有t个因数 } a[t] = i; } } for(i = 1;i <= t;i++) sum += a[i]; if(sum == n){ printf("%d = %d",n,a[1]); for(s = 2;s <= t;s++) printf(" + %d",a[s]); putchar('n'); x++; } 实验4-2-8 输出整数各位数字 (15 分) #include int main() { int n,j,i = 0,a[11] = {0}; scanf("%d",&n); do { i++; a[i] = n%10; n = n/10; }while(n); for(j = i;j > 0;j--) printf("%d ",a[j]); return 0; } 实验4-2-9 梅森数 (20 分) #include #include int isPrime(int n); //素数判断函数 int main() { int n,i = 2,t; scanf("%d",&n); if(n < 2) printf("None"); else{ do{ t = pow(2,i)-1; if(isPrime(t)){ //如果i为素数 printf("%dn",t); } i++; }while(i <= n); } return 0; } int isPrime(int n) { int div; for(div = 2;div <= sqrt(n);div++){ if(n%div == 0) return 0; } return 1; } 实验6-1 近似求PI (15 分) #include double fac(int n); //定义一个阶乘函数 int main() { int i = 0; double eps,n = 0,s = 1,PI; scanf("%le", &eps); for(i = 1; ;i += 2) { s *= i; PI += fac(n)/s; if(fac(n)/s < eps) break; n++; } printf("PI = %.5lf",2.0*PI); return 0; } double fac(int n) { int i; double s = 1; if(n == 0) s = 1; else for(i = 1;i <= n;i++) s *= i; return s; } 实验6-8 简单计算器 (20 分) #include int main() { int a,result; int i,flag = 1; char op; scanf("%d",&a); result = a; for(i = 1; ;i++){ op = getchar(); scanf("%d",&a); switch(op){ case '+':result += a; break; 跳出循环。 case '-':result -= a; break; case '*':result *= a; break; case '/': if(a==0){ printf("ERROR"); flag = 0; break; } else result /= a; break; case '=': printf("%dn",result); flag = 0; break; default: //switch中的break只能跳出switch,不能 } printf("ERROR"); flag = 0; break; } if(flag == 0) break; } return 0; 实验6-9 统计一行文本的单词个数 (15 分) #include int main() { char ch[1000]; char c; int i,word = 0,num = 0; gets(ch); for(i = 0;(c = ch[i]) != '0';i++){ if(c == ' ') word = 0; else if(word == 0){ word = 1; num++; } } printf("%d",num); return 0; } 实验7-1-1 简化的插入排序 (15 分) #include int main() { int n,i,j,b,temp; int a[11]; scanf("%d",&n); for(i = 0;i < n;i++) scanf("%d",&a[i]); scanf("%d",&b); for(i = 0;i <= n;i++){ if(b <= a[i]){ } for(j = n;j > i;j--){ a[j] = a[j-1]; } a[i] = b; break; } if(b == a[n-1]) a[n] = b; if(b>a[n-1]) a[n] = b; } for(i = 0;i <= n;i++) printf("%d ",a[i]); return 0; 实验7-1-2 求最大值及其下标 (20 分) #include int main() { int i,n,max,j = 0,a[10]; scanf("%d",&n); for(i = 0;i < n;i++) scanf("%d",&a[i]); max = a[0]; for(i = 1;i < n;i++){
版权声明:本文标题:PTA 浙大版《C语言程序设计实验与习题指导(第3版)》题目集(参考 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.freenas.com.cn/free/1708776087h531151.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论