2018华南农业大学2018年秋高级语言程序设计期末公开模拟考试
1 求数的位数
描述
由键盘输入一个不多于9位的正整数,要求输出它是几位数。
输入格式
一个整数
输出格式
输出该数为几位数
输入样例
349213
输出样例
6
#include<stdio.h>
#include<string.h>
int main()
{
long num;
char str[20];
scanf("%ld",&num);
sprintf(str,"%d",num);
printf("%d",strlen(str));
return 0;
}
2 冒泡排序
Time Limit:1000MS Memory Limit:65536K
题型: 填空题 语言: 无限制
描述
由键盘输入10个数,用“冒泡法”对10个数从小到大排序,并按格式要求输出。代码如下,请填充完整。
#include "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>
int main()
{ int a[10],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]);
return 0;
}
3 迭代法求平方根
Time Limit:1000MS Memory Limit:65535K
题型: 编程题 语言: 无限制
描述
使用迭代法求a的平方根。求平方根的迭代公式如下,要求计算到相邻两次求出的x的差的绝对值小于1E-5时停止,结果显示4位小数
输入格式
输入一个非负实数a
输出格式
计算并输出平方根
输入样例
16
输出样例
4.0000
#include<stdio.h>
#include<math.h>
int main(){
float a,b,c;
scanf("%f",&a);
b=a;
do
{
c=(b+a/b)/2;
if(fabs(b-c)<10.e-5)break;
b=c;
}
while(1);
{
printf("%.4f",c);
}
return 0;
}
5 一年的第几天
Time Limit:1000MS Memory Limit:65535K
题型: 填空题 语言: 无限制
描述
定义一个结构体类型表示日期类型(包括年、月、日)。程序中定义一个日期类型的变量,输入该日期的年、月、日,
计算并输出该日期是一年的第几天。
#include <stdio.h>
struct DATE
{
_______________________
};
int days(struct DATE date)
{
_______________________
}
int main()
{
struct DATE d;
scanf("%d-%d-%d", &d.year, &d.month, &d.day);
printf("%d", days(d));
}
输入格式
年月日,格式如样例
输出格式
该年的第几天
输入样例
2015-1-1
输出样例
1
#include <stdio.h>
struct DATE
{
int year;
int month;
int day;
};
int days(struct DATE date)
{
int sum=date.day;
int a[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int i;
for(i=1;i<date.month;i++){
sum+=a[i];
}
if(date.month>2&&((date.year%4==0&&date.year%100!=0)||date.year%400==0))
sum=sum+1;
return sum;
}
int main()
{
struct DATE d;
scanf("%d-%d-%d", &d.year, &d.month, &d.day);
printf("%d", days(d));
}
6 打印菱形图案
Time Limit:1000MS Memory Limit:65536K
题型: 编程题 语言: 无限制
描述
由键盘输入正数n(n<30),要求输出如下2*n+1行的菱形图案。
输出格式
菱形右边不留多余空格
输入样例
2
输出样例
*
***
*****
***
*
#include<stdio.h>
#include<stdlib.h>
int main()
{
int l,n,i,j,k;
scanf("%d",&l);
n=l+1;
for(i=1;i<=n;i++)
{
for(j=1;j<=n-i;j++)
printf(" ");
for(k=1;k<=2*i-1;k++)
printf("*");
printf("\n");
}
for(i=n-1; i>=1; i--)
{
for(j=1; j<=n-i; j++)
printf(" ");
for(k=1; k<=2*i-1; k++)
printf("*");
printf("\n");
}
return 0;
}
7 学生信息统计
Time Limit:1000MS Memory Limit:65535K
题型: 填空题 语言: 无限制
描述
输入10个学生5门课的考试成绩,分别用函数实现以下功能:
(1) 计算一个学生的平均分。
(2) 计算每门课程的平均分。
(3) 找出每门课程的最高分。
显示结果,显示两位小数。
#include <stdio.h>
void average(double a[][5], int n)
{
_______________________
}
void average2(double a[][5], int n)
{
_______________________
}
void top(double a[][5], int n)
{
_______________________
}
int main()
{
double a[10][5];
int i, j;
for(i=0; i<10; i++)
for(j=0; j<5; j++)
scanf("%lf", &a[i][j]);
average(a,10);
average2(a,10);
top(a,10);
return 0;
}
答案:
#include <stdio.h>
void average(double a[][5], int n)
{
double m;
int i;
for(i=0;i<=9;i++)
{
m=(a[i][0]+a[i][1]+a[i][2]+a[i][3]+a[i][4])/5;
printf("%.2f ",m);
}
printf("\n");
}
void average2(double a[][5], int n)
{
double m;
int i;
for(i=0;i<=4;i++)
{
m=(a[0][i]+a[1][i]+a[2][i]+a[3][i]+a[4][i]+a[5][i]+a[6][i]+a[7][i]+a[8][i]+a[9][i])/10;
printf("%.2f ",m);
}
printf("\n");
}
void top(double a[][5], int n)
{
int i,j,k,g;
double max;
max=a[0][0];
for(j=0;j<=4;j++)
{
for(i=0;i<=9;i++)
{
if(a[i][j]>max)
{
max=a[i][j];
k=i;
g=j;
}
}
printf("%.2f ",max);
}
}
int main()
{
double a[10][5];
int i, j;
for(i=0; i<10; i++)
for(j=0; j<5; j++)
scanf("%lf", &a[i][j]);
average(a,10);
average2(a,10);
top(a,10);
return 0;
}
8 链表创建与插入结点(填空)
Time Limit:1000MS Memory Limit:65535K
题型: 填空题 语言: 无限制
描述
代码实现先创新一个链表,然后显示该链表,之后插入一个结点,再显示插入结点的链表。
请填空,完成该代码
输入样例
3 (3 students)
1 (code of no.1 student)
98 (score of no.1 student)
3 (code of no.2 student)
99 (score of no.2 student)
5 (code of no.3 student)
87 (score of no.3 student)
4 (code of no.3 student needs be inserted)
77 (score of no.3 student needs be inserted)
输出样例
1 98
3 99
5 87
1 98
3 99
4 77
5 87
#include "stdio.h"
#include "malloc.h"
#define LEN sizeof(struct student)
struct student
{
long num;
int score;
struct student *next;
};
struct student *create(int n)
{
struct student *head=NULL,*p1=NULL,*p2=NULL;
int i;
for(i=1;i<=n;i++)
{ p1=(struct student *)malloc(LEN);
scanf("%ld",&p1->num);
scanf("%d",&p1->score);
p1->next=NULL;
if(i==1) head=p1;
else p2->next=p1;
p2=p1;
}
return(head);
}
void print(struct student *head)
{
struct student *p;
_______________________;
while(p!=NULL)
{
printf("%ld\t%d",p->num,p->score);
p=p->next;
printf("\n");
}
}
struct student *insert(struct student *head, struct student *stud)
{
struct student *p0,*p1,*p2;
_______________________; p0=stud;
if(head==NULL)
{head=p0;}
else
{
while( (p0->num > p1->num) && (p1->next!=NULL) )
{
p2=p1;
p1=p1->next;
}
if( p0->num < p1->num )
{
if( head==p1 ) head=p0;
else p2->next=p0;
p0->next=p1;
}
else { _______________________;}
}
return(head);
}
int main()
{
struct student *head,*stu;
int n;
scanf("%d",&n);
head=create(n);
print(head);
stu=_______________________;
scanf("%ld",&stu->num);
scanf("%d",&stu->score);
stu->next = NULL;
head=insert(head,stu);
print(head);
return 0;
}
答案
#include "stdio.h"
#include "malloc.h"
#define LEN sizeof(struct student)
struct student
{
long num;
int score;
struct student *next;
};
struct student *create(int n)
{
struct student *head=NULL,*p1=NULL,*p2=NULL;
int i;
for(i=1;i<=n;i++)
{ p1=(struct student *)malloc(LEN);
scanf("%ld",&p1->num);
scanf("%d",&p1->score);
p1->next=NULL;
if(i==1) head=p1;
else p2->next=p1;
p2=p1;
}
return(head);
}
void print(struct student *head)
{
struct student *p;
p=head;
while(p!=NULL)
{
printf("%ld\t%d",p->num,p->score);
p=p->next;
printf("\n");
}
}
struct student *insert(struct student *head, struct student *stud)
{
struct student *p0,*p1,*p2;
p1=head; p0=stud;
if(head==NULL)
{head=p0;}
else
{
while( (p0->num > p1->num) && (p1->next!=NULL) )
{
p2=p1;
p1=p1->next;
}
if( p0->num < p1->num )
{
if( head==p1 ) head=p0;
else p2->next=p0;
p0->next=p1;
}
else {p1->next=p0;p0->next=NULL;}
}
return(head);
}
int main()
{
struct student *head,*stu;
int n;
scanf("%d",&n);
head=create(n);
print(head);
stu=(struct student *)malloc(LEN);
scanf("%ld",&stu->num);
scanf("%d",&stu->score);
stu->next = NULL;
head=insert(head,stu);
print(head);
return 0;
}
9 文本文件操作_单词的排序(填空)
Time Limit:1000MS Memory Limit:65535K
题型: 填空题 语言: 无限制
描述
在当前目录有文件“case1.in”,文件里存放有多个(总个数不超过10000个)英文单词(每个英文单词不会超过10个字文字符),
每行一个,单词未排序。现要求,将文件中的所有单词按字典顺序排序,然后将排序好的单词写入新建的文件answer.txt中(注:文件存放于当前目录)。
请完成程序,实现该功能,(注意,填空题,请不要使用return 0结束,否则会影响评判而判错)
(如case1.in文件中原内容如下)
hello
bye
yes
(程序执行后,在文件answer.txt中内容如下)
bye
hello
yes
#include "stdio.h"
#include "string.h"
int main()
{
char a[10000][11],t[11];
int i,j,n=0;
FILE *fp;
fp=fopen("case1.in","r");
if(fp==NULL)exit(0);
while((fgets(a[n],11,fp))!=NULL)
{
n++;
}
fclose(fp);
for(i=0; i<n-1; i++)
{
for(j=0; j<n-1-i; j++)
{
if(strcmp(a[j],a[j+1])>0)
{
strcpy(t,a[j]);
strcpy(a[j],a[j+1]);
strcpy(a[j+1],t);
}
}
}
fp=fopen("answer.txt","w");
if(fp==NULL)exit(0);
for(i=0; i<n; i++)
{
fputs(a[i],fp);
}
fclose(fp);
}
10 数组的逆序输出【填空】
Time Limit:1000MS Memory Limit:65535K
题型: 填空题 语言: 无限制
描述
写一个程序,首先从键盘输入一个正整数n表示个数,然后输入n个整数,存放到数组中,最后把这些整数逆序输出。
输出时,两个整数之间用一个空格隔开。注:类名必须是Main(大写M)
#include <stdio.h>
int main()
{
int i,t, n;
_______________________
scanf("%d", &n);
for(i=0;i<n;i++)
scanf("%d",_______________________);
for(i=0;i<_______________________;i++)
{
_______________________
}
for(i=0;i<n;i++)
printf("%d ",a[i]);
return 0;
}
输入格式
第一行是n, n<=20
第二行是n个整数
输出格式
输出逆序的数列
输入样例
10
12 45 76 87 5 87 43 55 99 21
输出样例
21 99 55 43 87 5 87 76 45 12
答案
#include <stdio.h>
int main()
{
int i,t, n;
int a[80];
scanf("%d", &n);
for(i=0;i<n;i++)
scanf("%d", &a[i]);
for(i=0;i< (n+1)/2;i++)
{
t=a[i];
a[i]=a[n-1-i];
a[n-1-i]=t;
}
for(i=0;i<n;i++)
printf("%d ",a[i]);
return 0;
}
12 数字三角形2
Time Limit:1000MS Memory Limit:65535K
题型: 编程题 语言: 无限制
描述
由键盘输入正奇数N,输出如下图所示的(N+1)/2 行的数字三角。
例如:N=5
输入格式
输入一个正奇数N,1<=N<=49
输出格式
输出如题所述数字三角,为了输出美观,每个数字占5个字符位,右对齐
输入样例
5
输出样例
7
8 9 6
1 2 3 4 5
#include<stdio.h>
int main()
{
int x=0,y,count=1,max,n,m=0,word=0;
int a[49][49]={0};
scanf("%d",&n);
y=(n+1)/2-1;//表示这个三角形的行数
max=(n+1)*(n+1)/4;//表示这个三角形的最大值
while(count<=max)
{
a[y][x]=count++;
if(m==0)//m=0表示从左到右开始存数
{
x++;
if(x==n||a[y][x]!=0)
{
x--;
x--;
y--;
m=1;
}
}
else if(m==1)//m=1表示从右下向左上存数
{
x--;
y--;
if(word==0&&y<0||a[y][x]!=0)
{
y++;
y++;
m=2;
word=1;
}
else if(word==1&&y==0||a[y][x]!=0)
{
y++;
y++;
m=2;
}
}
else//这里是m=2,表示从右上到左下存数
{
y++;
x--;
if(a[y][x]!=0)
{
x++;
x++;
y--;
m=0;
}
}
}
for(y=0;y<(n+1)/2;y++)
{
for(x=0;x<n;x++)
{
if(a[y][x]!=0)
printf("%5d",a[y][x]);
else
printf(" ");
}
printf("\n");
}
return 0;
}
oj亲自调试成功,第11题没有做,仅供参考。