一.函数题
6-1 有序数组的插入
作者 陈越
单位 浙江大学
本题要求将任一给定元素插入从大到小排好序的数组中合适的位置,以保持结果依然有序。
函数接口定义:
bool Insert( List L, ElementType X );
其中List
结构定义如下:
typedef int Position; typedef struct LNode *List; struct LNode { ElementType Data[MAXSIZE]; Position Last; /* 保存线性表中最后一个元素的位置 */ };
L
是用户传入的一个线性表,其中ElementType
元素可以通过>、==、<进行比较,并且题目保证传入的数据是递减有序的。函数Insert
要将X
插入Data[]
中合适的位置,以保持结果依然有序(注意:元素从下标0开始存储)。但如果X
已经在Data[]
中了,就不要插入,返回失败的标记false
;如果插入成功,则返回true
。另外,因为Data[]
中最多只能存MAXSIZE
个元素,所以如果插入新元素之前已经满了,也不要插入,而是返回失败的标记false
。
裁判测试程序样例:
#include <stdio.h> #include <stdlib.h> #define MAXSIZE 10 typedef enum {false, true} bool; typedef int ElementType; typedef int Position; typedef struct LNode *List; struct LNode { ElementType Data[MAXSIZE]; Position Last; /* 保存线性表中最后一个元素的位置 */ }; List ReadInput(); /* 裁判实现,细节不表。元素从下标0开始存储 */ void PrintList( List L ); /* 裁判实现,细节不表 */ bool Insert( List L, ElementType X ); int main() { List L; ElementType X; L = ReadInput(); scanf("%d", &X); if ( Insert( L, X ) == false ) printf("Insertion failed.\n"); PrintList( L ); return 0; } /* 你的代码将被嵌在这里 */
输入样例1:
5
35 12 8 7 3
10
输出样例1:
35 12 10 8 7 3
Last = 5
输入样例2:
6
35 12 10 8 7 3
8
输出样例2:
Insertion failed.
35 12 10 8 7 3
Last = 5
代码长度限制
16 KB
时间限制
1000 ms
内存限制
64 MB
bool Insert(List L,ElementType X) { //首先要解决一个问题是,能否插入的首先要解决一个问题 if(L->Last==MAXSIZE-1)return false; //先前之前的数已经已经输入数组当中了 //所以我们需要拿这个数去和数组中的数比较一下,如果有相同的数就不用 //插入了,直接返回false; //Point:数组是从大到小排序的,所以遍历的时候找到比X大的数后一位停下来即可 //便可得到可以插入的位置 int i;//用于记录可以插入的坐标 for(i=0;i<=L->Last;i++) { if(X==L->Data[i])return false; if(X>L->Data[i])break; } //将记录位置往后一位往后移即可 for(int j=L->Last;j>=i;j--) L->Data[j+1]=L->Data[j]; //插入即可 L->Data[i]=X; L->Last++; return true; }
6-2 冒泡排序
编程实现冒泡排序函数。void bubbleSort(int arr[], int n);。其中arr存放待排序的数据,n为数组长度(1≤n≤1000)。
函数接口定义如下:
/* 对长度为n的数组arr执行冒泡排序 */ void bubbleSort(int arr[], int n);
请实现bubbleSort函数,使排序后的数据从小到大排列。
裁判测试程序样例:
#include <stdio.h> #define N 1000 int arr[N]; /* 对长度为n的数组arr执行冒泡排序 */ void bubbleSort(int arr[], int n); /* 打印长度为n的数组arr */ void printArray(int arr[], int n); void swap(int *xp, int *yp) { int temp = *xp; *xp = *yp; *yp = temp; } int main() { int n, i; scanf("%d", &n); for (i = 0; i < n; ++i) { scanf("%d", &arr[i]); } bubbleSort(arr, n); printArray(arr, n); return 0; } /* 打印长度为n的数组arr */ void printArray(int arr[], int n) { int i; for (i = 0; i < n; i++) { printf("%d", arr[i]); if (i < n - 1) /* 下标0..n-2每个元素后面有个空格 */ printf(" "); /*下标n-1,也就是最后一个元素后面没有空格*/ } printf("\n"); /* 一行打印完后换行 */ } /* 你的代码将嵌在这里 */
输入样例:
10
1 19 9 11 4 3 5 8 10 6
输出样例:
1 3 4 5 6 8 9 10 11 19
void bubbleSort(int *arr,int n) { for(int i=n-1;i>0;i--) { int flag=0; for(int j=0;j<i;j++) { if(arr[j]>arr[j+1]) { swap(&arr[j+1],&arr[j]); flag=1; } } if(flag==0)break;//若没有进行交换则退出 } }
二.编程题
7-1 英文单词排序
本题要求编写程序,输入若干英文单词,对这些单词按长度从小到大排序后输出。如果长度相同,按照输入的顺序不变。
输入格式:
输入为若干英文单词,每行一个,以#
作为输入结束标志。其中英文单词总数不超过20个,英文单词为长度小于10的仅由小写英文字母组成的字符串。
输出格式:
输出为排序后的结果,每个单词后面都额外输出一个空格。
输入样例:
blue
red
yellow
green
purple
#
输出样例:
red blue green yellow purple
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
#include<bits/stdc++.h> #include<string.h> using namespace std; bool cmp(string a,string b) { return a.length() < b.length(); } int main() { string ss[21];//存储字符串 int n=0;//记录有效字符串的数量 while(1) { string s; cin>>s; if(s=="#") break; ss[n]=s; n++; } //stable为稳定性 //sort为c++标准库的函数 //cmp函数是用来规定排序规则的,短的字符串会排在前面,长度相等的顺序不变 stable_sort(ss,ss+n,cmp);//具有稳定性的排序函数 for(int i=0;i<n;i++) { cout<<ss[i]<<" "; } return 0; }
7-2 寻找大富翁
胡润研究院的调查显示,截至2017年底,中国个人资产超过1亿元的高净值人群达15万人。假设给出N个人的个人资产值,请快速找出资产排前M位的大富翁。
输入格式:
输入首先给出两个正整数N(≤106)和M(≤10),其中N为总人数,M为需要找出的大富翁数;接下来一行给出N个人的个人资产值,以百万元为单位,为不超过长整型范围的整数。数字间以空格分隔。
输出格式:
在一行内按非递增顺序输出资产排前M位的大富翁的个人资产值。数字间以空格分隔,但结尾不得有多余空格。
输入样例:
8 3
8 12 7 3 20 9 5 18
输出样例:
20 18 12
代码长度限制
16 KB
时间限制
500 ms
内存限制
64 MB
#include<bits/stdc++.h> using namespace std; int main() { int n,m; cin>>n>>m; int zc[n]; for(int i=0;i<n;i++) { cin>>zc[i]; } //若删去greater<int>()则为从小到大排序 //此处为从大到小排序 sort(zc,zc+n,greater<int>()); if(n>m) { for(int i=0;i<m;i++) { if(i==m-1) { cout<<zc[i]; break; } cout<<zc[i]<<" "; } cout<<endl; }else{ for(int i=0;i<n;i++) { if(i==n-1) { cout<<zc[i]; break; } cout<<zc[i]<<" "; } cout<<endl; } return 0; }
7-3 排序
请编写程序对不超过50000个整数递增排序。
输入格式:
输入第一行一个正整数n,表示待排序的元素个数。第二行为n个整数,表示待排序的元素。n不超过50000。
输出格式:
输出为一行,表示排序结果,每个整数后一个空格。
输入样例:
5
5 4 3 2 1
输出样例:
1 2 3 4 5
代码长度限制
16 KB
时间限制
1000 ms
内存限制
64 MB
#include<bits/stdc++.h> using namespace std; bool cmp(int x,int y) { return x<y; } int main() { int n; cin>>n; int a[n]; int cnt=0; for(int i=0;i<n;i++) { cin>>a[i]; cnt++; } //稳定性排序 //cmp函数,从小到大排序,如果大小相等的话,不会改变相对顺序 stable_sort(a,a+n,cmp); for(int i=0;i<n;i++) { cout<<a[i]<<" "; } return 0; }