809-成都信息工程大学研究生入学考试-程序设计代码
2016
31题(解法1)
#include <stdio.h>
#define N 5
void max_min(int array[],int n)
{
int min,x,max,y,i,temp;
min = max = array[0];
x = y = 0;
for(i=0;i<n;i++)
{
if(min>array[i])
{
min=array[i];
x=i;
}
if(max<array[i])
{
max=array[i];
y=i;
}
}
if(x == 0 && y == n-1){
temp = array[x];
array[x] = array[n-1];
array[n-1] = temp;
}else{
temp = array[x];
array[x] = array[n-1];
array[n-1] = temp;
temp = array[y];
array[y] = array[0];
array[0] = temp;
}
for(i=0;i<n;i++) printf("%3d",array[i]);
}
int main()
{
int i,array[N];
for(i=0;i<N;i++) scanf("%d",&array[i]);
max_min(array,N);
return 0;
}
31题(解法2-更优解)
#include <stdio.h>
#define N 5
void max_min(int *array){
int *p,*max,*min,temp;
max = min = array;
for(p=array+1;p<array+N;p++){
if(*p > *max) max = p;
if(*p < *min) min = p;
}
if((*max==array[0]) && (*min==array[N-1])){
temp = *max;
*max = array[N-1];
array[N-1] = temp;
}else{
temp = *max;
*max = array[N-1];
array[N-1] = temp;
temp = *min;
*min = array[0];
array[0] = temp;
}
for(p=array;p<array+N;p++){
printf("%3d",*p);
}
}
int main(){
int i,array[N];
printf("请输入一个%d维数组:\n",N);
for(i=0;i<N;i++) scanf("%d",&array[i]);
max_min(array);
return 0;
}
32题
#include <stdio.h>
#include <stdlib.h>
#define N 3
struct student{
int id;
char name[10];
float scores[3];
float ave_socre;
} s[N];
void Input(struct student s[],int n)
{
int i;
printf("请输入学生信息,如:2018 张三 95 97 99\n");
for(i=0;i<n;i++)
{
scanf("%d %s %f %f %f",&s[i].id,s[i].name,&s[i].scores[0],&s[i].scores[1],&s[i].scores[2]);
s[i].ave_socre = (s[i].scores[0]+s[i].scores[1]+s[i].scores[2]) / 3;
}
}
void Output(struct student s[],int n)
{
int i;
printf("%s\t%s\t%s\t%s\t%s\t%s\t\n","学号","姓名","科目一","科目二","科目三","平均分");
for(i=0;i<n;i++)
{
printf("%d\t%s\t%4.2f\t%4.2f\t%4.2f\t%4.2f\n",s[i].id,s[i].name,s[i].scores[0],s[i].scores[1],s[i].scores[2],s[i].ave_socre);
}
}
int main()
{
Input(s,N);
Output(s,N);
return 0;
}
2017
28题
#include <stdio.h>
int main()
{
int i,j;
for(i=1;i<6;i++)
{
for(j=0;j<i;j++)
{
printf("%d",i);
}
printf("\n");
}
return 0;
}
29题
#include <stdio.h>
int main()
{
void swap(int *,int *);
int i,j,k;
printf("请输入三个待排序的数:\n");
scanf("%d%d%d",&i,&j,&k);
if(i>j){
swap(&i,&j);
}
if(i>k){
swap(&i,&k);
}
if(j>k){
swap(&j,&k);
}
printf("%3d%3d%3d",i,j,k);
return 0;
}
void swap(int *a,int *b){
int temp;
temp = *a;
*a = *b;
*b = temp;
}
30题
#include <stdio.h>
int main()
{
int a[12] = {1,4,6,9,13,16,19,28,40,100},i,j,k;
printf("请输入一个数:\n");
scanf("%d",&k);
printf("插入之前为:");
for(i=0;i<10;i++){
printf("%d ",a[i]);
}
for(i=0;i<10;i++){
if(k<a[i]){
for(j=10;j>=i;j--){
a[j+1] = a[j];
}
a[i] = k;
break;
}
}
printf("插入之后为:");
for(i=0;i<11;i++){
printf("%d ",a[i]);
}
return 0;
}
2018
28题
#include <stdio.h>
void Feb_days(){
int year;
scanf("%d",&year);
if( (year % 4 == 0) && (year % 100 != 0)){
printf("days:29");
}else{
printf("days:28");
}
}
int main()
{
Feb_days();
return 0;
}
29题
#include <stdio.h>
int main()
{
int i,j,k;
for(i=0;i<=100;i++){
for(j=0;j<=30;j++){
for(k=0;k<=200;k++){
if((i+j+k)==100
&& (0.4*i+4*j+0.2*k) == 40
&& i * j * k != 0
){
printf("买苹果%d个,西瓜%d个,梨%d个\n",i,j,k);
}
}
}
}
return 0;
}
30题
#include <stdio.h>
#define N 3
void swap_Line(int a[][4],int j){
int t;
t = a[j][0];a[j][0]=a[j+1][0];a[j+1][0] = t;
t = a[j][1];a[j][1]=a[j+1][1];a[j+1][1] = t;
t = a[j][2];a[j][2]=a[j+1][2];a[j+1][2] = t;
t = a[j][3];a[j][3]=a[j+1][3];a[j+1][3] = t;
}
void Sort(int s[N][4]){
int i,j;
for(i=0;i<N;i++){
for(j=0;j<N-1-i;j++){
if(s[j][3] < s[j+1][3]) swap_Line(s,j);
}
}
}
void PrintArray(int s[N][4]){
int i;
printf("NO\tscore1\tscore2\taverage\t\n");
for(i=0;i<N;i++){
printf("%2d\t%2d\t%2d\t%2d\t\n",s[i][0],s[i][1],s[i][2],s[i][3]);
}
}
int main()
{
int i;
int s[N][4];
printf("请输入%d个学生信息:\n",N);
for(i=0;i<N;i++){
scanf("%d%d%d",&s[i][0],&s[i][1],&s[i][2]);
s[i][3] = (s[i][1] + s[i][2])/2;
}
Sort(s);
PrintArray(s);
return 0;
}
2019
30
#include <stdio.h>
int main()
{
int i,k=2,s[20],j=0;
printf("请输入一个整数:\n");
scanf("%d",&i);
printf("%d = ",i);
while (i > 1)
{
if(i%k == 0){
s[j++]=k;
i = i / k;
}else{
k++;
}
}
for(i=0;i<j - 1;i++){
printf("%d * ",s[i]);
}
printf("%d",s[i]);
return 0;
}