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++){

if(max

max = a[i];

j = i;

}

}

printf("%d %d",max,j);

return 0;

}

实验7-1-3 将数组中的数逆序存放 (20 分)

#include

int main()

{

int i,n,a[10],b[10];

scanf("%d",&n);

for(i = 0;i < n;i++)

}

scanf("%d",&a[i]);

for(i = 0;i < n;i++){

b[n-i-1]=a[i];

}

printf("%d",b[0]);

for(i = 1;i < n;i++)

printf(" %d",b[i]);

return 0;

实验7-1-4 找出不是两个数组共有的元素 (20 分)

#include

int main()

{

int a[20], b[20], c[20];

int m, n, i, j, k=0;

scanf("%d", &m);

for(i=0; i

scanf("%d", &a[i]);

scanf("%d", &n);

for(i=0; i

scanf("%d", &b[i]);

for(i=0; i

for(j=0; j

if(a[i]==b[j])

break;

}

if(j>=n){

c[k]=a[i];

k++;

}

}

for(i=0; i

for(j=0; j

if(b[i]==a[j])

break;

}

if(j>=m){

c[k]=b[i];

k++;

}

}

printf("%d", c[0]);

}

for(i=1; i

for(j=0; j

if(c[i]==c[j])

break;

}

if(j>=i)

printf(" %d", c[i]);

}

return 0;

实验7-1-5 选择法排序 (20 分)

#include

int main()

{

int n,i,j,temp;

int a[10];

scanf("%d",&n);

for(i = 0;i < n;i++)

scanf("%d",&a[i]);

for(i = 0;i < n;i++)

{

for(j = i;j < n;j++)

{

if(a[i]

{

temp=a[i];

a[i]=a[j];

a[j]=temp;

}

}

}

printf("%d",a[0]);

for(i = 1;i < n;i++)

printf(" %d",a[i]);

//printf("%c",8);

//printf("xxx");

return 0;

}

实验7-1-6 求一批整数中出现最多的个位数字 (20 分)

#define _CRT_SECURE_NO_DEPRECATE

#include

int fmax(int a[]);

int main()

{

int n, i, a[1000], fqy[10] = { 0 };

int dight,max_fqy;

scanf("%d",&n);

for (i = 0; i < n; i++)

{

scanf("%d",&a[i]);

}

for (i = 0; i < n; i++)

{

while (a[i] > 0)

{

dight = a[i] % 10;

fqy[dight]++;

a[i] /= 10;

}

}

max_fqy = fmax(fqy);

printf("%d:",max_fqy);

for (i = 0; i < 10; i++)

{

if (fqy[i] == max_fqy)

printf(" %d",i);

}

return 0;

}

int fmax(int a[])

{

int i,max;

max = a[0];

for (i = 0; i < 10; i++)

if (a[i] > max)

max = a[i];

return max;

}

实验7-1-7 查找整数 (10 分)

#define _CRT_SECURE_NO_DEPRECATE

#include

int fmax(int a[]);

int main()

{

int n, x, i, a[20], count = 0;

scanf("%d %d",&n,&x);

for (i = 0; i < n; i++)

scanf("%d",&a[i]);

for (i = 0; i < n; i++)

{

if (a[i] == x)

{

printf("%d",i);

count++;

}

}

if (count == 0)

printf("Not Found");

return 0;

}

实验7-1-8 输出数组元素 (15 分)

#define _CRT_SECURE_NO_DEPRECATE

#include

int main()

{

int i, n, a[10] = { 0 }, count = 1;

scanf("%d",&n);

for (i = 0; i < n; i++)

scanf("%d",&a[i]);

printf("%d",a[1]-a[0]);

for (i = 1; i < n-1; i++)

{

printf(" %d", a[i + 1] - a[i]);

count++;

if (count == 3)

{

putchar('n');

if(i + 1 != n-1)

{

printf("%d", a[i + 2] - a[i + 1]);

count=1;

}

i++;

}

}

}

return 0;

实验7-1-9 数字加密 (15 分)

#define _CRT_SECURE_NO_DEPRECATE

#include

int main()

{

int i, x, a[4];

scanf("%d", &x);

a[0] = (x / 1000 + 9) % 10; //千位

a[1] = (x / 100 % 10 + 9) % 10; //百位

a[2] = (x / 10 % 10 + 9) % 10; //十位

a[3] = (x % 10 + 9) % 10; //个位

printf("The encrypted number is %d%d%d%d", a[2], a[3], a[0], a[1]);

return 0;

}

实验7-1-10 交换最小值和最大值 (15 分)

#define _CRT_SECURE_NO_DEPRECATE

#include

#include

int main()

{

int i, n, a[10], max, min, x, y, temp;

scanf("%d", &n);

for (i = 0; i < n; i++)

scanf("%d", &a[i]);

if(n == 1)

{

printf("%d ", a[0]);

exit(0);

}

min = a[0];

for (i = 0; i < n; i++)

if (min > a[i])

{

min = a[i];

y = i;

}

}

a[y] = a[0];

a[0] = min;

max = a[0];

for(i =0; i < n; i++)

if(max < a[i])

{

max = a[i];

x = i;

}

a[x] = a[n - 1];

a[n - 1] = max;

for (i = 0; i < n; i++)

printf("%d ", a[i]);

return 0;

实验7-1-11 求整数序列中出现次数最多的数 (15 分)

#define _CRT_SECURE_NO_DEPRECATE

#include

#include

int main()

{

int n, a[1000], b[1000], count = 0;

int temp, max;

scanf("%d", &n);

for (int i = 0; i < n; i++)

{

scanf("%d", &a[i]);

b[i] = a[i];

}

for (int i = 0; i < n; i++)

{

for (int j = 0; j < n; j++)

{

if (a[i] == b[j])

count++;

}

if (i == 0)

}

{

temp = count;

max = a[i];

}

else if(temp < count)

{

temp = count;

max = a[i];

}

count = 0;

}

printf("%d %d",max,temp);

return 0;

实验7-1-12 组个最小数 (20 分)

#define _CRT_SECURE_NO_DEPRECATE

#include

int main()

{

int i, a[10];

for (i = 0; i < 10; i++)

scanf("%d", &a[i]);

for (i = 1; i < 10; i++)

{

if (a[i] != 0)

{

printf("%d", i);

a[i]--;

break;

}

}

for (i = 0; i < 10; i++)

{

while (a[i] != 0)

{

printf("%d", i);

a[i]--;

}

}

return 0;

}

实验7-1-13 装箱问题 (20 分)

#define _CRT_SECURE_NO_DEPRECATE

#include

#include

int main()

{

int n, s[1001], a[1001], max = 1; //定义物品个数n个

scanf("%d", &n);

for (int i = 1; i <= n; i++)

scanf("%d", &s[i]); //将每个物品大小存入数组中

for (int i = 1; i <= n; i++)

a[i] = 100; //将数组a赋值为100

for (int i = 1; i <= n; i++)

{

for (int j = 1; j <= n; j++)

{

if (s[i] <= a[j])

{

if (j > max)

max = j; //记录最大箱子号

printf("%d %dn", s[i], j);

a[j] = a[j] - s[i]; //更新数组a

break;

}

}

}

printf("%dn", max);

return 0;

}

实验7-2-1 求矩阵各行元素之和 (15 分)

#define _CRT_SECURE_NO_DEPRECATE

#include

int main()

{

int m, n, a[6], s = 0;

scanf("%d %d", &m, &n);

for (int i = 0; i < m; i++)

{

for (int j = 0; j < n; j++)

scanf("%d", &a[j]);

for (int j = 0; j < n; j++)

}

}

s += a[j];

printf("%dn", s);

s = 0;

return 0;

实验7-2-2 矩阵运算 (20 分)

#define _CRT_SECURE_NO_DEPRECATE

#include

int main()

{

int n, sum = 0, a[10][10];

scanf("%d", &n);

for (int i = 0; i < n; i++) //按格式输入矩阵

for (int j = 0; j < n; j++)

scanf("%d", &a[i][j]);

for (int i = 0; i < n; i++)

for (int j = 0; j < n; j++)

{

if (!(i + j == n - 1 || j == n - 1 || i == n - 1))

//a[i][j] = 0; //如果符合题意,就将其赋值为0

sum += a[i][j]; //求和

}

printf("%d", sum);

return 0;

}

实验7-2-3 求矩阵的局部极大值 (15 分)

#define _CRT_SECURE_NO_DEPRECATE

#include

int main()

{

int m, n, count = 0, a[100][21];

scanf("%d %d", &m, &n);

for (int i = 1; i <= m; i++) //按照格式输入矩阵

for (int j = 1; j <= n; j++)

scanf("%d", &a[i][j]);

for (int i = 2; i < m; i++) //从第2行开始,到倒数第一行结束

}

{

for (int j = 2; j < n; j++) //从第2列开始,到倒数第一列结束

{

if (a[i][j] > a[i + 1][j] && a[i][j] > a[i - 1][j] &&

a[i][j] > a[i][j + 1] && a[i][j] > a[i][j - 1])//判断是否为矩阵的局部极大值

{

printf("%d %d %dn", a[i][j], i, j);

count++;

}

}

}

if (count == 0)

printf("None %d %d", m, n);

return 0;

实验7-2-4 计算天数 (15 分)

#define _CRT_SECURE_NO_DEPRECATE

#include

int main()

{

int year, month, days;

scanf("%d/%d/%d", &year, &month, &days);

for (int i = 1; i <= month - 1; i++) //利用循环,每次遍历一个月份,就加上该月份的天数

{

switch (i)

{

case 1:

case 3:

case 5:

case 7:

case 8:

case 10:

case 12:days += 31;

break;

case 2:days += 28;

break;

case 4:

case 6:

case 9:

case 11:days += 30;

break;

}

}

}

if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) //判断是否为闰年

{

if (month > 2) //月份大于2,就加上闰年多出的一天

printf("%d", days+1);

else

printf("%d", days);

}

else

printf("%d", days);

return 0;

实验7-2-5 判断上三角矩阵 (15 分)

#define _CRT_SECURE_NO_DEPRECATE

#include

int main()

{

int t, n, count = 0, a[10][10];

scanf("%d", &t);

for (int k = 0; k < t; k++)

{

scanf("%d", &n);

for (int i = 0; i < n; i++)

for (int j = 0; j < n; j++)

scanf("%d", &a[i][j]);

for (int i = 1; i < n; i++)

for (int j = i - 1; j >= 0; j--)

if (a[i][j] != 0)

count = 1;

if (count == 0)

printf("YESn");

else

printf("NOn");

count = 0;

}

return 0;

}

实验7-2-6 打印杨辉三角 (20 分)

#define _CRT_SECURE_NO_DEPRECATE

#include

int main()

{

int t, n, a[10][10];

scanf("%d", &n);

t = n - 1;

for (int i = 0; i < n; i++) //先给每行第一列数和最后一个数赋值1

a[i][0] = a[i][i] = 1;

for (int i = 2; i < n; i++) //利用循环给其他数赋值

for (int j = 1; j < i; j++)

a[i][j] = a[i - 1][j] + a[i - 1][j - 1];

for (int i = 0; i < n; i++)

{

for (int k = 0; k < t; k++) //先输出空格

printf(" ");

t--;

for (int j = 0; j < i + 1; j++) //输出数字+空格

printf("%4d", a[i][j]);

printf("n");

}

return 0;

}

实验7-2-7 方阵循环右移 (20 分)

#define _CRT_SECURE_NO_DEPRECATE

#include

int main()

{

int m, n, a[6][6];

scanf("%d %d", &m, &n);

m = m % n; //考虑到m可能大于n,这里取模

for (int i = 0; i < n; i++)

for (int j = 0; j < n; j++)

scanf("%d", &a[i][j]);

for (int i = 0; i < n; i++)

{

for (int j = n-m; j < n; j++) //先输出右移后超出最后一列的数

printf("%d ", a[i][j]);

for (int j = 0; j < n - m; j++) //在输出右移后没有超过最后一列的数

}

}

printf("%d ", a[i][j]);

printf("n");

return 0;

实验7-2-8 找鞍点 (20 分)

#define _CRT_SECURE_NO_DEPRECATE

#include

int is_line_max(int n, int x, int i, int a[6][6]);//判断是否为行最大值

int is_rank_min(int n, int x, int j, int a[6][6]);//判断是否为列最小值

int main()

{

int n, flag = 0, a[6][6];

scanf("%d", &n);

for (int i = 0; i < n; i++) //按格式输入

for (int j = 0; j < n; j++)

scanf("%d", &a[i][j]);

for (int i = 0; i < n; i++)

for (int j = 0; j < n; j++)

if (is_line_max(n, a[i][j], i, a))

if (is_rank_min(n, a[i][j], j, a))

{

printf("%d %dn", i, j);

flag = 1; //如果是鞍点,就做标记

}

if (flag == 0)

printf("NONE");

return 0;

}

int is_line_max(int n, int x, int i, int a[6][6])

{

for (int k = 0; k < n; k++)

if (x < a[i][k])

return 0;

return 1;

}

int is_rank_min(int n, int x, int j, int a[6][6])

{

for (int k = 0; k < n; k++)

if (x > a[k][j])

return 0;

return 1;

}

实验7-2-9 螺旋方阵 (20 分)

#define _CRT_SECURE_NO_DEPRECATE

//参考刘汝佳"蛇形填数"的算法

#include

#include //内含memset()函数

int main()

{

int n, x, y, tot = 0, a[10][10];

scanf("%d", &n);

memset(a, 0, sizeof(a)); //将数组元素全部初始化为0,以便之后判断

tot = a[x = 0][y = 0] = 1;

while (tot < n * n)

{ //先进性预判,碰壁就不往下走,!a[x][y+1]等价于a[x][y+1]==0

while (y + 1 < n && !a[x][y + 1]) a[x][++y] = ++tot;

while (x + 1 < n && !a[x + 1][y]) a[++x][y] = ++tot;

while (y - 1 >= 0 && !a[x][y - 1]) a[x][--y] = ++tot;

while (x - 1 >= 0 && !a[x - 1][y]) a[--x][y] = ++tot;

}

for (int i = 0; i < n; i++)

{

for (int j = 0; j < n; j++)

printf("%3d", a[i][j]);

printf("n");

}

return 0;

}

实验7-2-10 简易连连看 (20 分)

#define _CRT_SECURE_NO_DEPRECATE

#include

#include //内含exit()函数

void print(int n, char a[10][10]); //输出字符串数组函数

int main()

{

int n, k, count = 0, fail = 0;

char a[10][10];

scanf("%d", &n);

for (int i = 1; i <= 2 * n; i++)

for (int j = 1; j <= 2 * n; j++)

{

scanf("%c", &a[i][j]);

if (a[i][j] == ' ' || a[i][j] == 'n') //过滤无效字符

j--;

}

scanf("%d", &k);

for (int i = 1; i <= k; i++)

{

int x1, x2, y1, y2;

scanf("%d %d %d %d", &x1, &y1, &x2, &y2);

if (a[x1][y1] == a[x2][y2] && a[x1][y1] != '*')

{

a[x1][y1] = a[x2][y2] = '*';

count += 2;

if (count != 2 * n * 2 * n)

print(n, a);

}

else if (a[x1][y1] != a[x2][y2] || a[x1][y1] == '*' && a[x2][y2] == '*')

{

fail++;

printf("Uh-ohn");

}

if (count == 2 * n * 2 * n)

{

printf("Congratulations!");

exit(0); //直接退出程序

}

if (fail == 3)

{

printf("Game Over");

exit(0);

}

}

return 0;

}

void print(int n, char a[10][10])

{

for (int i = 1; i <= 2 * n; i++)

{

for (int j = 1; j <= 2 * n; j++)

{

if (j == 1)

printf("%c", a[i][j]);

else

printf(" %c", a[i][j]);

}

printf("n");

}

}

实验7-3-1 字符串逆序 (15 分)

#define _CRT_SECURE_NO_DEPRECATE

#include

#include

int main()

{

char a[81];

gets(a);

for (int i = strlen(a) - 1; i >= 0; i--)

printf("%c", a[i]);

return 0;

}

实验7-3-2 查找指定字符 (15 分)

#define _CRT_SECURE_NO_DEPRECATE

#include

#include

int main()

{

char ch, a[81];

int flag = 0;

ch = getchar();

getchar();

gets(a);


本文标签: 输出 计算 函数 数组 序列