第一题:答案:
#include void sort(int a[], int m)//将数组a的前m个元素(从小到大)排序 {int i = 0;for (i = 0; i < m - 1; i++){int j = 0;int flag = 1;for (j = 0; j a[j + 1]){int t = 0;t = a[j];a[j] = a[j + 1];a[j + 1] = t;flag = 0;}}if (flag == 1){break;}}}void input(int a[], int m) //依次从键盘输入数组a的前m个元素 {int i = 0;for (i = 0; i < m; i++){scanf("%d", &a[i]);}}void print(int a[], int m) //依次输出数组a的前m个元素 {int i = 0;for (i = 0; i =0;i--){if (a[i] < a[i-1]){int t = a[i-1];a[i-1] = a[i];a[i] = t;}else{break;}}}void main(){int m, k;scanf("%d", &m);int alist[1000];input(alist, m);sort(alist, m); print(alist, m);scanf("%d", &k);insertSortedList(alist, m, k);print(alist, m + 1);}
第二题:
答案:
#include int gcd(int a, int b); //计算并返回整数a,整数b的最大公约数. void add(int op1[], int op2[], int rst[]); //计算分数op1和op2的和,保存结果到rst. void sub(int op1[], int op2[], int rst[]); //计算分数op1和op2的差,保存结果到rst.void mul(int op1[], int op2[], int rst[]); //计算分数op1和op2的积,保存结果到rst.void div(int op1[], int op2[], int rst[]); //计算分数op1和op2的商,保存结果到rst.void main(){int a, b, c, d;int op1[2], op2[2], rst[2];scanf("%d%d", &op1[1], &op1[0]);scanf("%d%d", &op2[1], &op2[0]);add(op1, op2, rst);printf("%d/%d + %d/%d = %d/%d\n", op1[1], op1[0], op2[1], op2[0], rst[1], rst[0]);sub(op1, op2, rst);printf("%d/%d - %d/%d = %d/%d\n", op1[1], op1[0], op2[1], op2[0], rst[1], rst[0]);mul(op1, op2, rst);printf("%d/%d * %d/%d = %d/%d\n", op1[1], op1[0], op2[1], op2[0], rst[1], rst[0]);div(op1, op2, rst);printf("%d/%d / %d/%d = %d/%d\n", op1[1], op1[0], op2[1], op2[0], rst[1], rst[0]);}int gcd(int a, int b)//返回参数a,b的最大公约数{int r = a % b;while (r != 0){a = b;b = r;r = a % b;}return b;}void add(int op1[], int op2[], int rst[]) //计算分数op1和op2的和,保存结果到rst. {int fm = op1[0] * op2[0]; //和的分母int fz = op1[1] * op2[0] + op2[1] * op1[0]; //和的分子int g = gcd(fm, fz);rst[0] = fm / g; //分子分母同时除以它们的最大公约数,实现约分rst[1] = fz / g;if (rst[0] < 0)//确保分母为正数{rst[0] = -rst[0];rst[1] = -rst[1];}}void sub(int op1[], int op2[], int rst[])//计算分数op1和op2的差,保存结果到rst.{int fm = op1[0] * op2[0];int fz = op1[1] * op2[0] - op2[1] * op1[0];int g = gcd(fm, fz);rst[0] = fm / g;rst[1] = fz / g;if (rst[0] < 0)//确保分母为正数{rst[0] = -rst[0];rst[1] = -rst[1];}}void mul(int op1[], int op2[], int rst[]) //计算分数op1和op2的积,保存结果到rst.{int fm = op1[0] * op2[0];int fz = op1[1] * op2[1];int g = gcd(fm, fz);rst[0] = fm / g;rst[1] = fz / g;if (rst[0] < 0)//确保分母为正数{rst[0] = -rst[0];rst[1] = -rst[1];}}void div(int op1[], int op2[], int rst[])//计算分数op1和op2的商,保存结果到rst.{int fm = op1[0] * op2[1];int fz = op1[1] * op2[0];int g = gcd(fm, fz);rst[0] = fm / g;rst[1] = fz / g;if (rst[0] < 0)//确保分母为正数{rst[0] = -rst[0];rst[1] = -rst[1];}}
第三题:
答案:
#include int myisdigital(char ch);//数字字符判断函数,如果ch是数字字符,则返回1,否则返回0; int myisalpha(char ch);//字母字符判断函数,如果ch是大小写字母字符, 则返回1,否则返回0; int mystrlen(char str[]);// 返回指定字符串str的长度 (不计\0字符) void mystrcat(char str1[], char str2[]);// 连接的两个字符串, 将str2连接到str1后面 int mystrcmp(char str1[], char str2[]);//比较两个字符串,如果str1大,则返回1;如果str2则返回-1;如果相等,返回0; void mystrcpy(char str1[], char str2[]);//复制字符串str2到字符串str1中 void main(){char str1[1000], str2[100], tmp[100]; int m;int num_alpha = 0; //str1中的字母字符的个数 int num = 0; //str2中的数组字符构成的整数 int i;scanf("%s%s%d", str1, str2, &m);printf("%d,", mystrlen(str1) + mystrlen(str2));for (i = 0; i < mystrlen(str1); i++)if (myisalpha(str1[i]))num_alpha++;for (i = 0; i 0)// str1比较大,则交换 {mystrcpy(tmp, str1);mystrcpy(str1, str2);mystrcpy(str2, tmp);}for (i = 1; i = '0' && ch = 'A' && ch = 'a' && ch <= 'z'))return 1;elsereturn 0;}int mystrlen(char str[])// 返回指定字符串str的长度 (不计\0字符){int i = 0;while (str[i] != '\0'){i++;}return i;}int mystrcmp(char str1[], char str2[])//比较两个字符串,如果str1大,则返回1;如果str2则返回-1;如果相等,返回0;{int i = 0, j = 0;if (mystrlen(str1) < mystrlen(str2)){for (i = 0; i str2[i])return 1;if ((str1[i] mystrlen(str2)){for (i = 0; i str2[i] || ((str1[i] == str2[i]) && i == mystrlen(str2)))return 1;if (str1[i] < str2[i])return -1;}}if (mystrlen(str1) == mystrlen(str2)){for (i = 0; i str2[i])return 1;if (str1[i] < str2[i])return -1;if ((str1[i] == str2[i]) && i == mystrlen(str2))return 0;}}}void mystrcpy(char str1[], char str2[])//复制字符串str2到字符串str1中 {int i = 0;for (i = 0; i < mystrlen(str2); i++){str1[i] = str2[i];}str1[i] = '\0';}