A.三数论大小(引用)
题目描述
输入三个整数,然后按照从大到小的顺序输出数值。
要求:定义一个函数,无返回值,函数参数是三个整数参数的引用,例如int &a, int &b, int &c。在函数内对三个参数进行排序。主函数调用这个函数进行排序。
要求:不能直接对三个整数进行排序,必须通过函数而且是引用的方法。
要求:输出必须在主函数进行。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 #include <iostream> using namespace std;void fun (int & ra, int & rb, int & rc) ;int main () { int t, a, b, c; cin >> t; for (int i = 0 ; i < t; i++) { cin >> a >> b >> c; fun (a, b, c); cout << a << " " << b << " " << c << endl; } return 0 ; } void fun (int & ra, int & rb, int & rc) { int tmp; if (ra < rb) { tmp = ra; ra = rb; rb = tmp; } if (ra < rc) { tmp = ra; ra = rc; rc = tmp; } if (rb < rc) { tmp = rb; rb = rc; rc = tmp; } }
B.求最大值最小值(引用)
题目描述
编写函数void find(int *num,int n,int &minIndex,int &maxIndex),求数组num(元素为num[0],num[1],…,num[n-1])中取最小值、最大值的元素下标minIndex,maxIndex(若有相同最值,取第一个出现的下标。)
输入n,动态分配n个整数空间,输入n个整数,调用该函数求数组的最小值、最大值下标。
改变函数find功能不计分。
要求:在main函数中按样例格式输出结果,不能直接在find函数中输出。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 #include <iostream> using namespace std;void find (int * num, int n, int & minIndex, int & maxIndex) { for (int i = 0 ; i < n; i++) { if (num[i] > num[maxIndex]) { maxIndex = i; } if (num[i] < num[minIndex]) { minIndex = i; } } } int main () { int t, i, n, maxIndex, minIndex; cin >> t; while (t--) { cin >> n; maxIndex = minIndex = 0 ; int * array = new int [n]; for (i = 0 ; i < n; i++) { cin >> array[i]; } find (array, n, minIndex, maxIndex); cout << "min=" << array[minIndex] << " " << "minindex=" << minIndex << endl; cout << "max=" << array[maxIndex] << " " << "maxindex=" << maxIndex << endl; cout << endl; delete []array; } return 0 ; }
C.职工信息 (结构体)
题目描述
编写程序,定义一个职工信息结构包括职工姓名、工作年限、工资总额。定义一个存放职工信息的结构数组
程序实现初始化5名职工的信息,并对工作年限大于等于30年的职工每人加100元工资,分别输出工资变化之前和之后的所有职工的信息。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 #include <iostream> using namespace std;struct Person { char name[10 ]; int year; int wages; }; int main () { struct Person array[6 ]; int i; for (i = 0 ; i < 5 ; i++) { cin >> array[i].name >> array[i].year >> array[i].wages; } cout << "原始工资" << endl; cout << "姓名" << " " << "年限" << " " << "工资" << endl; for (i = 0 ; i < 5 ; i++) { cout << array[i].name << " " << array[i].year << " " << array[i].wages << endl; } cout << "加薪后工资" << endl; cout << "姓名" << " " << "年限" << " " << "工资" << endl; for (i = 0 ; i < 5 ; i++) { cout << array[i].name << " " << array[i].year << " " ; if (array[i].year >= 30 ) { array[i].wages += 100 ; cout << array[i].wages << endl; } else { cout << array[i].wages << endl; } } return 0 ; }
D.谁是老二(结构体)
题目描述
定义一个结构体,包含年月日,表示一个学生的出生日期。然后在一群学生的出生日期中找出谁的出生日期排行第二
要求:出生日期的存储必须使用结构体,不能使用其他类型的数据结构。
要求程序全过程对出生日期的输入、访问、输出都必须使用结构。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 #include <iostream> using namespace std;struct birth { int year; int month; int day; }; void swap (birth& a, birth& b) { birth tmp = a; a = b; b = tmp; } bool compare (birth& a, birth& b) { if (a.year > b.year) return 1 ; else if (a.year < b.year) return 0 ; else { if (a.month > b.month) return 1 ; else if (a.month < b.month) return 0 ; else { return a.day > b.day; } } } int main () { int i, j, k, t; birth member[100 ] = { 0 }; cin >> t; for (i = 0 ; i < t; i++) { cin >> member[i].year >> member[i].month >> member[i].day; } for (i = 0 ; i < t; i++) { for (j = 0 ; j < t - i - 1 ; j++) { if (compare (member[j], member[j + 1 ])) { swap (member[j], member[j + 1 ]); } } } cout << member[1 ].year << '-' << member[1 ].month << '-' << member[1 ].day << endl; }
E.抄袭查找(结构体+指针+函数)
题目描述
已知一群学生的考试试卷,要求对试卷内容进行对比,查找是否有抄袭。
每张试卷包含:学号(整数类型)、题目1答案(字符串类型)、题目2答案(字符串类型)、题目3答案(字符串类型)
要求:使用结构体来存储试卷的信息。定义一个函数,返回值为一个整数,参数是两个结构体指针,函数操作是比较两张试卷的每道题目的答案,如果相同题号的答案相似度超过90%,那么就认为有抄袭,函数返回抄袭题号,否则返回0。相似度是指在同一题目中,两个答案的逐个位置上的字符两两比较,相同的数量大于等于任一个答案的长度的90%,就认为抄袭。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 #include <iostream> #include <string.h> using namespace std;struct exampaper { int id; string answer[4 ]; }; int compare (string a, string b) { int i = 0 ; double len = 0 , sum = 0 ; while (a[i] != '\0' && b[i] != '\0' ) { if (a[i] == b[i]) { sum++; } i++, len++; } if (len * 0.9 <= sum) { return 1 ; } else { return 0 ; } } int main () { int t; cin >> t; exampaper* student = new exampaper[t]; for (int i = 0 ; i < t; i++) { cin >> student[i].id; for (int j = 0 ; j < 3 ; j++) { cin >> student[i].answer[j]; } } for (int i = 0 ; i < 3 ; i++) { for (int j = 0 ; j < t; j++) { for (int k = j + 1 ; k < t; k++) { if (compare (student[j].answer[i], student[k].answer[i])== 1 ) { cout << student[j].id << " " << student[k].id << " " << i + 1 << endl; } } } } return 0 ; }