资源描述
C 语言程序设计
上机实验指导与习题(第二版)
参考答案
(仅供教师内部参考)
实验 1 C 语言程序初步
一、实验目的
(1)了解所用的计算机系统的基本操作方法,学会独立使用该系统。
(2)了解在该系统上如何编辑、编译、连接和运行一个C 程序。
(3)通过运行简单的C 程序,初步了解C 程序的特点。
(4)在教师的指导下,学会使用JudgeOnline 实验系统。
二、实验内容
1. 运行第一个C 程序
略
2. 在JudgeOnline 系统中提交实现了计算a+b 功能的程序
略
2
实验2 基本数据类型、运算和表达式
一、实验目的
(1)掌握C 语言数据类型,熟悉如何定义一个整型和实型的变量,以及对它们赋值的方法。
(2)掌握不同的类型数据之间赋值的规律。
(3)学会使用C 的有关算术运算符,以及包含这些运算符的表达式,特别是自加(++)和自减(--)运
算符的使用。
(4)进一步熟悉C 程序的编辑、编译、连接和运行的过程。
二、实验内容
[题目1117:变量定义,按要求完成程序]
下面给出一个可以运行的程序,但是缺少部分语句,请按右边的提示补充完整缺少的语句。
#include "stdio.h"
main()
{
int a, b; /*定义整型变量a 和b*/
int i, j; /*定义实型变量i 和j*/
a=5;
b=6;
i=3.14; j=i*a*b;
printf("a=%d,b=%d,i=%f,j=%f\n", a, b, i, j);
}
[具体操作步骤]
(1)将代码补充完整;
(2)在TC 或VC++上运行通过;
(3)在JudgeOnline 实验系统上提交;
[题目6568:在显示屏上显示指定字符]
要求编写一个程序,在显示屏上显示如下内容(全为半角字符,且不包含空格):
C:\ABC.TXT
[提示] 注意转义字符在程序中的使用。
参考程序:
#include "stdio.h"
main()
{ printf("C:\\ABC.TXT"); }
3
[题目1118:赋值表达式与赋值语句,写出程序运行结果]
阅读下面程序,写出运行结果:
#include "stdio.h"
main()
{ float a;
int b, c;
char d, e;
a=3.5;
b=a;
c=330;
d=c;
e='\141';
printf("%f,%d,%d,%c,%c", a,b,c,d,e);
}
运行结果为:
3.500000,3,330,J,a
[提示]赋值语句具有类型转换的功能,但可能会改变数据。
[题目1119:基本运算,写出程序运行结果]
阅读下面程序,写出运行结果:
#include "stdio.h"
main()
{ int a, b, c;
float d=15, e, f;
a=35%7;
b=15/10;
c=b++;
e=15/10;
f=d/10;
printf("%d,%d,%d,%f,%f,%f", a,b,c,d,e,f);
}
运行结果为:
0,2,1,15.000000,1.000000,1.500000
[提示]除法分整除与普通除法之分。
4
实验3 基本输入与输出
一、实验目的
(1)熟练掌握putchar、getchar、printf、scanf 函数的使用方法。
(2)掌握各种类型数据的输入输出的方法,能正确使用各种格式转换符。
二、实验内容
[题目1126:字符的输入与输出]
编程实现由键盘输入一个字符后,在屏幕上输出该字符。
[第一组自测数据]
[键盘输入]
a↙
[正确输出]
a
[第二组自测数据]
[键盘输入]
+↙
[正确输出]
+
参考程序:
#include "stdio.h"
main()
{ char ch;
ch = getchar();
putchar(ch);
}
[题目1127:计算加法]
编程实现由键盘输入一个加法式,输出正确的结果。(两个加数均为整数)
[第一组自测数据]
[键盘输入]
10+20↙
[正确输出]
30
[第二组自测数据]
[键盘输入]
-15+60↙
[正确输出]
45
参考程序:
#include "stdio.h"
main()
{ int a, b;
scanf("%d%*c%d", &a,&b);
printf("%d", a+b);
}
[题目1014:求圆面积]
由键盘输入圆半径r,请计算该圆的面积。(注:π 取3.14159,结果保留两位小数位;另外,程序只
要能对r 在0 到10000 范围的情况输出正确答案即可)
[第一组自测数据] [键盘输入]
2
65.2↙
[正确输出]
13355.02
[第二组自测数据]
[键盘输入]
11.2↙
[正确输出]
394.08
[提示]结果保留两位小数可采用printf 函数的格式控制字符来实现。
参考程序:
#include "stdio.h"
main()
{ float area,r;
scanf("%f",&r);
area=3.14159*r*r;
printf("%0.2f",area);
}
[题目1015:计算摄氏温度值]
从键盘输入一个华氏温度值,要求按格式输出其对应的摄氏温度值,精确到小数点后两位。
数学公式描述为:
( 32)
9
C = 5 F ?
[第一组自测数据]
[键盘输入]
100↙
[正确输出]
37.78
[第二组自测数据]
[键盘输入]
100↙
[正确输出]
37.78
[提示]注意公式中的除为普通除法。
参考程序:
#include<stdio.h>
main()
{ float f,c;
scanf("%f",&f);
c=5.0/9*(f-32);
printf("%.2f",c);
}
3
实验4 选择结构程序设计
一、实验目的
(1)了解C 语言表示逻辑的方法(以0 代表“假”,以非0 代表“真”)。
(2)学会正确使用逻辑运算符和逻辑表达式。
(3)熟练掌握if 语句和switch 语句。
(4)结合程序掌握一些简单的算法。
二、实验内容
[题目1120:判断点是否在圆上]
由键盘输入一个点的坐标, 要求编程判断这个点是否在单位圆上,点在圆上输出Y, 不在圆上输出
N。使用小数点后3 位精度进行判断。
[第一组自测数据]
[键盘输入]
0.707,0.707↙
[正确输出]
Y
[第二组自测数据]
[键盘输入]
0.5,0.5↙
[正确输出]
N
[提示](1)平面上的点与圆的关系分为在圆内、在圆上、在圆外三种,本题要求判断是否在圆上;(2)
判断两实数相等采用判断这两实数的差的绝对值小于规定误差精度(本题为0.001)的方法实现。
参考程序:
#include "stdio.h"
#include "math.h"
main()
{ float a,b;
scanf("%f,%f",&a,&b);
if(fabs(a*a+b*b-1)<1e-3)
printf("Y\n");
else printf("N\n");
}
[题目1017:求数的位数]
由键盘输入一个不多于9 位的正整数,要求输出它是几位数。
[第一组自测数据]
[键盘输入]
349213↙
[正确输出]
6
[第二组自测数据]
[键盘输入]
10000↙
[正确输出]
5
[提示]划定一位数、二位数、…、九位数的范围,然后用if … else if … else 语句实现判断。
4
参考程序:
#include "stdio.h"
main()
{ int n,place;
scanf("%ld",&n);
if(n>99999999) place=9;
else if(n>9999999) place=8;
else if(n>999999) place=7;
else if(n>99999) place=6;
else if(n>9999) place=5;
else if(n>999) place=4;
else if(n>99) place=3;
else if(n>9) place=2;
else place=1;
printf("%ld\n",place);
}
[题目1018:数的排序]
由键盘输入三个整数a、b、c,按从小到大的顺序输出这三个数。
[第一组自测数据]
[键盘输入]
65,45,90↙
[正确输出]
45,65,90
[第二组自测数据]
[键盘输入]
9,6,3↙
[正确输出]
3,6,9
参考程序:
#include<stdio.h>
main()
{ int a,b,c,t;
scanf("%d,%d,%d",&a,&b,&c);
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("%d,%d,%d",a,b,c);
}
[题目1016:字符变换]
由键盘输入5 个字符,将其中的大写字母变成小写,其它类型的字符不变,并按输入顺序逐个输出。
[第一组自测数据]
[键盘输入]
ApPLe↙
[正确输出]
apple
[第二组自测数据]
[键盘输入]
a+B=5↙
[正确输出]
a+b=5
[提示]下面代码实现由键盘读入一个字符,并按题意处理后输出
char a;
a=getchar();
if(a>='A' && a<='Z') a=a+32;
5
putchar(a);
现在,题目要求处理5 个字符,怎么做呢?请自己思考……
参考程序:
#include <stdio.h>
main()
{ char a,b,c,d,e;
scanf("%c%c%c%c%c",&a,&b,&c,&d,&e);
if(a<='Z'&&a>='A') a=a+32;
if(b<='Z'&&b>='A') b=b+32;
if(c<='Z'&&c>='A') c=c+32;
if(d<='Z'&&d>='A') d=d+32;
if(e<='Z'&&e>='A') e=e+32;
printf("%c%c%c%c%c",a,b,c,d,e);
}
[题目1019:数的整除]
由键盘输入5 个整数,逐个判断它们能否被27 整除,能的输出“YES”,不能的输出“NO”(注意,
输出时,一个判断结果占一行,5 个数的判断共占5 行)。
[第一组自测数据]
[键盘输入]
8 27 17577 325 54↙
[正确输出]
NO
YES
YES
NO
YES
[第二组自测数据]
[键盘输入]
8 27 17577 325 54↙
[正确输出]
NO
YES
YES
NO
YES
[提示]整除即除得余数为0
参考程序:
#include "stdio.h"
main()
{ int a,b,c,d,e;
scanf("%d %d %d %d %d",&a,&b,&c,&d,&e);
if(a%27==0) printf("YES\n");else printf("NO\n");
if(b%27==0) printf("YES\n");else printf("NO\n");
if(c%27==0) printf("YES\n");else printf("NO\n");
if(d%27==0) printf("YES\n");else printf("NO\n");
if(e%27==0) printf("YES\n");else printf("NO\n");
}
[题目1020:正负奇偶判断]
由键盘输入非零整数x,判断该数正负,正数输出positive,负数输出negative,接着判断该数的奇
偶性,奇数输出odd,偶数输出even。
[第一组自测数据]
[键盘输入]
-43↙
[正确输出]
negative
odd
6
[第二组自测数据]
[键盘输入]
98↙
[正确输出]
positive
even
参考程序:
#include "stdio.h"
main()
{ int n;
scanf("%d",&n);
if(n<0)printf("negative\n");
else printf("positive\n");
if(n%2==0)printf("even\n");
else printf("odd\n");
}
[题目1023:简单计算器]
下面程序是实现一个简单的运算器(保留两位小数点),如果由键盘输入10+50,计算机可以输出结
果60.00;如果输入8*6,计算机输出48.00;如果输入20/4,计算机输出5.00;如果输入8-6,计算机
输出2.00,请在空处填上适当的代码,运行通过后并提交。
#include "stdio.h"
main()
{ float a,b,c;
char op;
scanf("%f%c%f", );
switch (op)
{ case '+': ;
case '-': _;
case '*': _;
case '/': __ ;
default: printf("error");
return ;
}
printf("result= ", c);
}
[第一组自测数据]
[键盘输入]
45*2↙
[正确输出]
90
[第二组自测数据]
[键盘输入]
50.1-23↙
7
[正确输出] 27.10
参考程序:
#include <stdio.h>
int main()
{ float a,b,c;
char op;
scanf("%f%c%f",&a,&op,&b);
switch(op)
{ case '+':c=a+b;break;
case '-':c=a-b;break;
case '*':c=a*b;break;
case '/':c=a/b;break;
default:printf("error");
break;
}
printf("result=%.2f",c);
}
8
实验5 循环结构程序设计(一)
一、实验目的
(1)熟悉掌握用while 语句、do-while 语句和for 语句实现循环的方法。
(2)掌握在程序设计中用循环实现一些常用算法(如穷举、迭代、递推等)。
二、实验内容
[题目1024:计算阶乘]
输入正整数n,计算n!,结果用长整型数表示(注n!=1*2*3*...*n)
[第一组自测数据]
[键盘输入]
5↙
[正确输出]
120
[第二组自测数据]
[键盘输入]
8↙
[正确输出]
40320
参考程序:
#include<stdio.h>
main()
{ long i,n=1,a;
scanf("%ld",&a);
for(i=1;i<=a;i++) n=n*i;
printf("%ld\n",n);
}
[题目1025:计算数列和]
有数列1,3,5,7,9,11,……
现要求由键盘输入n,计算输出该数列的前n 项和。
[第一组自测数据]
[键盘输入]
2↙
[正确输出]
4
[第二组自测数据]
[键盘输入]
5↙
[正确输出]
25
9
参考程序:
#include<stdio.h>
main()
{ long n,sum=0,i,t=1;
scanf("%ld",&n);
for(i=1;i<=n;i++)
{ sum=sum+t;
t=t+2;
}
printf("%ld\n",sum);
}
或
#include <stdio.h>
main()
{ long n,sum;
scanf("%ld",&n);
sum=n*n;
printf("%ld",sum);
}
注:评判系统不对程序实现细节进行分析,只对运行结果进行评测。
[题目1026:累加一行字符中的数字]
由键盘输入一行字符(总字符个数从1 个至80 个均有可能,以回车符表示结束),将其中每个数字
字符所代表的数值累加起来,输出结果。
[第一组自测数据]
[键盘输入]
abc123↙
[正确输出]
6
[第二组自测数据]
[键盘输入]
A6H7T+65↙
[正确输出]
24
[提示](1)可以使用下面程序段逐个读入键盘输入的一行字符
char ch;
while((ch=getchar())!='\n')
{……}
(2)数字字符转为对应的数值可用a=ch-'0'
参考程序:
#include<stdio.h>
main()
{ char c;
int s=0,a;
while((c=getchar())!='\n')
{ if(c>='0'&&c<='9')
{ a=c-48;
s=s+a;
}
}
printf("%d",s);
}
10
[题目1029:求最大公约数]
由键盘输入两个正整数m、n(m、n 用长整数表示),计算它们的最大公约数。
[第一组自测数据]
[键盘输入]
16,24↙
[正确输出]
8
[第二组自测数据]
[键盘输入]
17,25↙
[正确输出]
1
[提示]公约数是既能整除m 又能整除n 的数,题目要求满足这一条件的最大的一个。
参考程序:
#include<stdio.h>
main()
{ long r,m,n,temp;
scanf("%ld,%ld",&m,&n);
while(m!=0)
{ r=n%m;
n=m;
m=r;
}
printf("%ld\n",n);
}
或
#include<stdio.h>
main()
{ long m,n,t,i,s;
scanf("%ld,%ld",&m,&n);
t=m>n?n:m;
for (i=t;i>=1;i--)
{ if (m%i==0&&n%i==0)
{ s=i; break; }
}
printf("%d",s);
}
[题目1030:字符变换]
由键盘输入一个句子(总字符个数从1 个至80 个均有可能,以回车符表示结束),将其中的大写字
符变成小写(其它类型的字符不变),最后输出变换后的句子。
[第一组自测数据]
[键盘输入]
How Are You?↙
[正确输出]
how are you?
[第二组自测数据]
[键盘输入]
ThiS IS My fIrSt C ProgrAm!↙
[正确输出]
this is my first c program!
11
参考程序:
#include <stdio.h>
main()
{ char c;
while((c=getchar())!='\n')
{ if(c>='A'&&c<='Z')
c=c+32;
putchar(c);
}
}
[题目1037:计算数列和]
有数列:
编程实现,由键盘输入n,计算输出数列前n 项和。(结果保留四位小数)
[第一组自测数据]
[键盘输入]
20↙
[正确输出]
32.6603
[第二组自测数据]
[键盘输入]
30↙
[正确输出]
88.0403
参考程序:
#include<stdio.h>
main()
{ int i,t,n;
float a=2,b=1,s=0;
scanf("%d",&n);
for(i=1;i<=n;i++)
{ s=s+a/b;
t=a;a=a+b;b=t;
}
printf("%.4f\n",s);
}
[题目1044:输出最小值]
从键盘输入十个整数,输出最小值
[自测数据]
[键盘输入]
12 45 76 87 5 87 43 55 99 21↙
[正确输出]
5
12
参考程序:
#include "stdio.h"
main()
{ int i,t,min;
scanf("%d", &min);
for(i=1;i<10;i++)
{
scanf("%d", &t);
if(t<min) min=t;
}
printf("%d\n",min);
}
*[题目1031:统计单词个数]
由键盘输入一个句子(总字符个数从1 个至80 个均有可能,以回车符表示结束),以空格分割单词,
要求输出单词的个数。
[第一组自测数据]
[键盘输入]
How Are You?↙
[正确输出]
3
[第二组自测数据]
[键盘输入]
There are many students and many
trees!↙
[正确输出]
7
参考程序:
#include<stdio.h>
main()
{ int i,num=0,word=0;
char c;
for(i=0;(c=getchar())!='\n';i++)
if(c==' ')word=0;
else if(word==0)
{ word=1;
num++;
}
printf("%d",num);
}
*[题目1042:百万富翁]
一个百万富翁遇到一个陌生人,陌生人找他谈了一个换钱的计划。该计划如下:我每天给你m 元,
而你第一天只需给我一分钱。第二天我仍给你m 元,你给我2 分钱。第三天,我仍给你m 元,你给我4
分钱。依次类推,你每天给我的钱是前一天的两倍,直到一个月(38)天。百万富翁很高兴,欣然接受这
个契约。现要求,编写一个程序,由键盘输入m,计算多少天后,百万富翁开始亏钱。
[第一组自测数据]
[键盘输入]
100↙
[正确输出]
18
[第二组自测数据]
[键盘输入]
10000↙
13
[正确输出] 25
参考程序:
#include <stdio.h>
#include <math.h>
main()
{ int n,m,i;
scanf ("%d",&m);
for (i=1;i<=38;i++)
if (0.01*(pow(2,i-1)-1)-i*m>=0) break;
printf("%d",i-1);
}
14
实验6 循环结构程序设计(二)
一、实验目的
(1)进一步熟悉掌握用while 语句、do-while 语句和for 语句实现循环的方法。
(2)掌握在程序设计中使用多重循环。
二、实验内容
[题目1028:求素数]
输出2 到200 之间(包括2、200)的所有素数(注:要求1 行1 个素数,按由小到大的顺序输出)。
[提示]采用双重循环,外层循环产生2 到200 之间的数,内层循环对数进行判断是否为素数。
参考程序:
# include<stdio.h>
# include<math.h>
main()
{ int m,k,i;
for(m=2;m<=200;m++)
{ k=sqrt(m);
for(i=2;i<=k;i++)
if(m%i==0) break;
if(i>k) printf("%d\n",m);
}
}
[题目1035:打印菱形]
由键盘输入正数n,要求输出2*n+1 行的菱形图案。要求菱形左边紧靠屏幕左边。
[第一组自测数据]
[键盘输入]
3↙
[正确输出]
*
***
*****
*******
*****
***
*
[第二组自测数据]
[键盘输入]
2↙
[正确输出]
*
***
*****
***
*
15
参考程序:
#include "stdio.h"
#include "math.h"
main()
{ int n,i,j,k;
scanf("%d",&n);
for(i=1;i<=2*n+1;i++)
{ k=abs(n+1-i);
for(j=1;j<=k;j++) printf(" ");
for(j=1;j<=2*n+1-2*k;j++) printf("*");
printf("\n");
}
}
[题目1137:找满足要求的数字]
输出1 到9999 中能被7 整除,而且至少有一位数字是5 的所有数字。输出时一行一个数字,且按由
小到大的顺序输出。
[提示]判断一个数中是否有数字5,首先要掌握拆数字的方法,一种可行算法如下:
一个整数a,使用a%10,可以得到a 的个位数,然后使用a=a/10,可以将a 中的个位数字除去,
上述过程重复则可以得到原数a 中其它位上的数字。
参考程序:
#include "stdio.h"
main()
{ int i, j;
for(i=7; i<=9999; i=i+7)
{ j=i;
while(j!=0)
{ if(j%10==5) break;
j=j/10;
}
if(j!=0) printf("%d\n", i);
}
}
[题目1038:打印图案]
由键盘输入正数n,要求输出中间数字为n 的菱形图案。要求菱形左边紧靠屏幕左边。
[第一组自测数据]
[键盘输入]
4↙
[正确输出]
1
121
12321
1234321
12321
121
1
[第二组自测数据]
[键盘输入]
3↙
[正确输出]
1
121
12321
121
1
16
参考程序:
# include<stdio.h>
# include<math.h>
main()
{ int n,i,j,k,h;
scanf("%d",&n);
for(i=-n+1;i<=n-1;i++)
{ for(j=0;j<abs(i);j++)
printf(" ");
for(k=1;k<=n-abs(i);k++) printf("%d",k);
for(h=n-abs(i)-1;h>=1;h--) printf("%d",h);
printf("\n");
}
}
17
实验7 数组
一、实验目的
(1)掌握一维数组和二维数组的定义、赋值和输入输出方法。
(2)掌握与数组有关的算法。
二、实验内容
[题目1039:倒序]
由键盘输入10 个整数,倒序输出。(数字间由一个空格分隔)
[自测数据]
[键盘输入]
70 5 14 20 19 2 99 67 13 66↙
[正确输出]
66 13 67 99 2 19 20 14 5 70
参考程序:
#include<math.h>
main()
{ int a[10];
int i;
for(i=0;i<10;i++) scanf("%d",&a[i]);
for(i=9;i>=0;i--) printf("%d\n",a[i]);
}
[题目1062:打印矩阵]
由键盘输入一个3×4 的矩阵,要求输出它的转置矩阵。
[自测数据]
[键盘输入]
1 6 9 3
1 1 0 2
1 9 8 9
参考程序:
[正确输出]
1 1 1
6 1 9
9 0 8
3 2 9
#include<stdio.h>
main()
{ int a[3][4],b[4][3],i,j;
for(i=0;i<3;i++)
for(j=0;j<4;j++)
{ scanf("%d",&a[i][j]);
b[j][i]=a[i][j];
}
for(i=0;i<4;i++)
{ for(j=0;j<3;j++)
18
printf("%d ",b[i][j]);
printf("\n");
}
}
*[题目1047:冒泡排序]
由键盘输入10 个数,用“冒泡法”对10 个数从小到大排序,并按格式要求输出。代码如下,请填充完
整。 数字间由一个空格分隔。
#incude "stdio.h"
main()
{ int a[10], i, j, t;
for(i=0;i<10;i++)
scanf("%d",___________) ;
for( ___________)
{ for(j=0;j<____;j++)
if (___________)
{___________}
}
for(i=0;i<10;i++)
printf("%d ",a[i]);
}
[自测数据]
[键盘输入]
70 5 14 20 19 2 99 67 13 66↙
[正确输出]
2 5 13 14 19 20 66 67 70 99
参考程序:
#include<stdio.h>
main()
{ int a[10];
int i,j,t;
for(i=0;i<10;i++)
scanf("%d",&a[i]);
for(i=0;i<9;i++)
{ for(j=0;j<9-i;j++)
if(a[j]>a[j+1])
{ t=a[j]; a[j]=a[j+1]; a[j+1]=t; }
}
for(i=0;i<10;i++)
printf("%d ",a[i]);
}
19
[题目1040:统计不同数字个数]
由键盘输入20 个整数,统计不同数字的个数。
[自测数据]
[键盘输入]
70 5 14 22 19 2 99 67 13 66 5 93 44 38 22 11 39 22 33 11↙
[正确输出]
16
[提示]上述答案中,因为5 有1 个重复数字,11 有1 个重复数字,22 有2 个重复数字,故不同数字有16
个,分别是70 5 14 22 19 2 99 67 13 66 93 44 38 11 39 33
参考程序:
#include"stdio.h"
main()
{ int a[20];
int i,t,p=0;
for(i=0;i<20;i++)
{ scanf("%d",&a[i]);
for(t=0;t<i;t++)
if(a[t]==a[i])break;
if(t==i)
p++;
}
printf("%d",p);
}
*[题目1046:计算高精度加法]
由键盘输入两个位数很长的整数(一行一个数,每个数最长不超过80 位),试计算并输出这两个数
的和。
[自测数据]
[键盘输入]
1234567890123456789↙
987654321098765↙
[正确输出]
1235555544444555554
参考程序:
#include "stdio.h"
#include "string.h"
main()
{ int a[100]={0},b[100]={0},c[100]={0};
char s[101];
int i=0,n1=0,n2=0,max=0,e=0;
gets(s);
n1=strlen(s);
for(i=n1-1;i>=0;i--) a[n1-1-i]=s[i]-'0';
gets(s);
n2=strlen(s);
20
for(i=n2-1;i>=0;i--) b[n2-1-i]=s[i]-'0';
if(n1>n2) max=n1;
else max=n2;
for(i=0;i<=max;i++)
{ c[i]=(a[i]+b[i]+e)%10;
e=(a[i]+b[i]+e)/10;
}
if(c[max]>0) printf("%d",c[max]);
for(i=max-1;i>=0;i--)
printf("%d",c[i]);
}
*[题目1051:找矩阵中的鞍点]
由键盘输入一个3×4(3 行4 列)的矩阵,输出矩阵中的鞍点(即在矩阵行中最大,列中最小的数)。
若没有鞍点,输出“NO”字样。
[自测数据]
[键盘输入]
87 90 110 98↙
70 97 210 65↙
98 45 120 30↙
[正确输出]
110
参考程序:
#include <stdio.h>
main()
{ int i,j,k,a[3][4],max,maxj,flag;
for(i=0;i<3;i++)
for(j=0;j<4;j++)
scanf("%d",&a[i][j]);
for(i=0;i<3;i++)
{ max=a[i][0];
maxj=0;
for(j=0;j<4;j++)
if(a[i][j]>max)
{max=a[i][j]; maxj=j; }
flag=1;
for(k=0;k<3;k++)
if(max>a[k][maxj])
{ flag=0; break; }
if(flag)
{printf("%d",max);break;}
}
if(!flag)
printf("NO");
}
21
实验8 字符数组的应用
一、实验目的
(1)掌握字符数组和字符串函数的使用。
(2)掌握与字符串处理有关的算法。
二、实验内容
[题目1121:定义存贮字符串的字符数组]
在下面程序中填充定义字符数组的语句,使程序完整。
#include "string.h"
void main()
{ char s[80]; /*定义字符数组s*/
strcpy(s, "abcdefghijklmn");
printf("%s", s);
}
参考程序:
#include "string.h"
#include <stdio.h>
void main()
{ char s[80]; /*定义字符数组s*/
strcpy(s, "abcdefghijklmn");
printf("%s", s);
}
[题目1123:字符串的输入与输出]
下面程序实现从键盘读入字符串,然后输出到屏幕,请填充必要的语句。
#include "stdio.h"
void main()
{
char s[50];
printf("What's your name?");
gets(s); /*由键盘读入字符串*/
printf("Your name is ", );
printf("%s", s); /*打印字符串*/
}
参考程序:
[题目1122:字符串的合并]
从键盘输入3 个字符串(每个字符串以回车符做为结束标志),将3 个字符串以输入先后顺序合并到
字符串s 中,请填空使用程序完整。
#include "stdio.h"
#include "string.h"
main()
{
char s[100]="";
char a[30];
gets(a); strcat(s, a);
22
gets(a); strcat(s, a);
gets(a); strcat(s, a);
/*可以写多行代码*/
printf("%s", s);
}
[自测数据]
[键盘输入]
123
abc
456
[正确输出]
1
展开阅读全文