文章目录
- 第一章
- 1. 求两个整数之和(p7)
- 第二章
- 2. 求三个数中的较大值(用函数)(p14、p107)
- 3.求1×2×3...×n(求n的阶乘,用for循环与while循环)(P17)
- 1.循环求n的阶乘
- 2.递归求n的阶乘(n< 10)
- 4.有M个学生,输出成绩在80分以上的学生的学号和成绩,并统计人数(p18)
- 5.判断200-2500的每一年是否是闰年,并将结果输出。非闰年如何求呢?(p18)
- 6.求1-1/2+1/3-1/4.....+1/99-1/100(有坑!p19)
- 6.1变形:1-1/22+1/333-1/4444.....+1/nnnnn...(n不大于9)
- 7.判断素数(p20)
- 1.用2-(i-1)去试除
- 2.用2-根号i去试除
- 8.输入10个数,从中找最大(p35)
- 9.有三个数a,b,c,按大小顺序输出(使用函数p35)
- 10.判断一个数能否被3和5整除(p35)
- 11.求两个数m和n的最大公约数,以及最小公倍数(p35、p137)
- 1.暴力求解法
- 2.辗转相除法
- 3.递归
- 12.求方程ax^2+bx+c=0的根(p35)
- 第三章
- 13.温度转换(p37)
- 14.大小写字母转换(p54)
- 15.给出三角形边长,求三角形的面积(p58)
- 16.译码p82、p135
- 17.计算面积p82
- 第四章
- 18.输入一个小于1000的正数,要求输出它的平方根(如平方根不是整数,则输出其整数部分)
- 19.有一个函数,输入x,输出y相应的值(p108)
- 20.给出100制成绩,要求输出等级(P168)
- 21.给一个不多于5位的正整数!!!!!(P109)
- 22.输入4个整数,要求按从小到大的顺序输出p109
- 23.输出乘法口诀表
- 左上
- 左下
- 右上
- 右下
- 第五章
- 24.求1-100的和
- 25.学校1000名学生捐款,总数到达10万元结束,统计捐款人数及平均捐款数目(P122)
- 26.输出100-200不能被3整除的数
- 27.输出下列矩阵
- 28.用公式 π/4≈1-1/3+1/5-1/7+......求π的近似值,直达发现某一项的绝对值小于10^-6^为止。
- 29.求斐波那契数列得前40 个数(p129)
- 方法1:循环
- 方法2:递归
- 方法3:一次求两个数
- 30.统计输入的一行中字母、空格、数字和其它字符的个数(p129)
- 31求a+aa+aaa+aaaa+aaaaa....nn...nn的值,n表示a的位数,a是一个数字,二者都由键盘输入(p129)
- 32. 1!+2!+3!+4!+5!...+n!(p129)
- 方法1:双层for
- 方法2:单层循环
- 33.求1000以内的水仙花数,例:153 = 1^3^ + 5^3^ + 3^3^(p129)
- 34.求1000以内的完美数(一个数恰好等于其真因子之和)例:6 = 1+2+3
- 35.求一个分数序列的前20项和(p138)
- 36.自由落体(p138)
- 37.猴子吃桃
- 38.智能打印菱形
- 第六章
- 39.冒泡排序(p144)
- 普通冒泡排序
- 双向冒泡排序
- 40.选择排序
- 41.将二维数组行和列的元素互换(p149)
- 42.输出一个矩阵中的最大值,以及它的下标(p150)
- 43.统计单词的个数(p163)
- 44.三个字符串,找出最大者(p164)
- 45.筛选法求素数(p165)
- 46.求矩阵对角线之和(p165)
- 正对角线
- 反对角线
- 47.向一个有序数组中插入一个数,按顺序输出(p165)
- 48.逆序数组(p165)
- 49.杨辉三角(p165)
- 方法一数组循环
- 方法二递归
- 50.找出一个二维数组中的鞍点(p165)
- 51.折半查找(p165)
- 52.统计输入的三行中,大小写字母、数字、空格的数量
- 53.模拟实现strcat
- 54.模拟实现strcmp
- 55.模拟实现strcpy
- 第七章
- 56.函数嵌套求4个数的最大值(p180)
- 57.汉诺塔问题(p188)
- 58.写一个函数,调用该函数可求出最大、最小、平均值(p198)
- 59. 有一个字符串,输入一个字符,删除该字符串中的该字符(p213)
- 1.使用数组
- 2.使用指针
- 60,反转字符串(p216课后习题)
- 61. 写一个函数,输入一个4位数字,要求输出这4个数字字符,两个数字之间空一个空格(p216)
- 62.写一个函数,输入一行字符,将字符串中最长的单词输出(p216)
- 63. 输入10个学生5门课程的成绩,分别用函数实现以下功能(p216)
- 64.进制转换,函数实现(p216)
- 65.用递归法将一个整数n转换为字符串(p216)
- 66.给出年月日,计算该日是该年的第几天(p216)
- 第八章(均使用指针)
- 67.按由大到小顺序输出两数,指针实现(p227)
- 68.使用指针将n个整数按相反顺序存放(p242)
- 69. 输入三个字符串,由小到大输出(p291)
- 70.输入10个数,将最小数与第一个交换,最大数与最后一个交换(p291)
- 71.n个整数,使前面各数向后移动m个位置,最后m个数变成最前面m个数(p291)
- 72.n个人围一圈报数(p291)
- 73.写一个函数,求字符串的长度(p291)
- 74.从该字符串的第m的字符开始,全部复制到另一个字符串中(p291)
- 75.输入一行字符串,统计大小写字母、数字、空格和其它字符的个数(p291)
- 76.写一个函数,转置矩阵(p291)
- 77. 5×5数组,将最大值放在中间位置,4个最小值放在四个角上(p291)
- 78.10个字符串,对他们进行排序(p291)
- 79.逆序10个数(p291)
- 80.写三个函数,实现以下功能(数组实现)
- 81输入一个字符串,统计其中连续数字的个数,并将数字放在一个数组中
- 82.输入月份,输出对应的英文单词,使用指针数组处理
第一章
1. 求两个整数之和(p7)
#include<stdio.h>
int main()
{int a = 0;int b = 0;int sum = 0;scanf("%d %d", &a, &b);sum = a + b;printf("sum=%d", sum);return 0;
}
第二章
2. 求三个数中的较大值(用函数)(p14、p107)
写法一:
//求三个数中的较大值(用函数)
#include<stdio.h>
int Max(int a, int b, int c)
{int max = 0;max = a > b ? a : b;max = max > c ? max : c;return max;
}
int main()
{int a = 0;int b = 0;int c = 0;int max = 0;scanf("%d %d %d", &a, &b, &c);max = Max(a, b, c);printf("max=%d", max);return 0;
}
写法二:
#include<stdio.j>
int main()
{int a = 0;int b = 0;int c = 0;int max = 0;scanf("%d %d %d", &a, &b, &c);if(a > b){max = a; }else{max = b;}if(c > max){max = c;}printf("max=%d", max);return 0;
}
3.求1×2×3…×n(求n的阶乘,用for循环与while循环)(P17)
1.循环求n的阶乘
#include<stdio.h>
int main()
{int i = 1;int n = 0;int sum = 1;scanf("%d", &n);for (i = 1; i <= n; i++){sum = sum * i;}/*while (n){sum = sum * i;i++;n--;}*/printf("%d\n", sum);return 0;
}
2.递归求n的阶乘(n< 10)
n! = (n-1)! * n
#include<stdio.h>
int func(int n)
{if( n == 0){return 1;}else{return n * func(n - 1);}
}
int main()
{int n = 0;scanf("%d", &n);int ret = func(n);printf("%d\n", ret);return 0;
}
4.有M个学生,输出成绩在80分以上的学生的学号和成绩,并统计人数(p18)
#include<stdio.h>
#define M 4
int main()
{int arr[M][2] = { 0 };int i = 0;int number = 0;int score = 0;int count = 0;for (i = 0; i < M; i++){scanf("%d %d", &arr[i][0], &arr[i][1]);}for (i = 0; i < M; i++){if (arr[i][1] > 80){printf("学号:%d 分数:%d\n", arr[i][0], arr[i][1]);count++;}}printf("80分以上的共%d人\n", count);return 0;
}
5.判断200-2500的每一年是否是闰年,并将结果输出。非闰年如何求呢?(p18)
#include<stdio.h>
int main()
{int i = 0;for (i = 2000; i <= 2500; i++){if ((i % 4 == 0 && i % 100 != 0) || (i % 400 == 0)){printf("%d年是闰年\n", i);}else{printf("%d年不是闰年\n", i);}}return 0;
}
如何求非闰年?
#include<stdio.h>
int main()
{int i = 0;for (i = 2000; i <= 2500; i++){//方法一:对闰年的要求逐个取反//if(i % 4 !=0 || i % 100 ==0 && i % 400 != 0)//方法二:直接对闰年整个取反 !(闰年要求)if (!((i % 4 == 0 && i % 100 != 0) || (i % 400 == 0))){printf("%d年不是闰年\n", i);}}return 0;
}
6.求1-1/2+1/3-1/4…+1/99-1/100(有坑!p19)
#include<stdio.h>
int main()
{int i = 0;//特别注意 一定要有变量是浮点型!!float flag = 1;float sum = 0;for (i = 1; i <= 100; i++){sum += flag / i;flag = -flag;}printf("%f\n", sum);return 0;
}
6.1变形:1-1/22+1/333-1/4444…+1/nnnnn…(n不大于9)
#include<stdio.h>
int main()
{int i = 0;//特别注意 一定要有变量是浮点型!!float flag = 1;float sum = 0;int n = 0;scanf("%d", &n);for (i = 1; i <= n; i++){int m = i;//记录当前数是几for (int j = 1; j < i; j++){m = i + m * 10;//算分母}sum += flag / m;flag = -flag;}printf("%f\n", sum);return 0;
}
7.判断素数(p20)
1.用2-(i-1)去试除
#include<stdio.h>
#include<math.h>
int main()
{int i = 0;int j = 0;int sum = 0;for (i = 100; i < 1000; i++){for (j = 2; j < i; j++){if (i % j == 0){break;}}if (j == i){printf("%d ", i);sum++;}}printf("\n%d\n", sum);return 0;
}
#include<stdio.h>
#include<math.h>
int main()
{int i = 0;int j = 0;int sum = 0;for (i = 100; i <1000; i++){int flag = 1;//每次假设i是素数for (j = 2; j < i; j++){if (i % j == 0){flag = 0;//i不是素数break;}}if (flag == 1){printf("%d ", i);sum++;}}printf("\n%d\n", sum);return 0;
}
2.用2-根号i去试除
#include<math.h>
int main()
{int i = 0;int j = 0;int sum = 0;for (i = 100; i < 1000; i++) //优化3:for (i = 101; i < 1000; i += 2){ //因为偶数一定不是素数for (j = 2; j <= sqrt(i); j++){if (i % j == 0){break;}}if (j > sqrt(i)){printf("%d ", i);sum++;}}printf("\n%d\n", sum);return 0;
}
8.输入10个数,从中找最大(p35)
#include<stdio.h>
int main()
{int arr[10] = { 0 };int i = 0;for (i = 0; i < 10; i++){scanf("%d", &arr[i]);}int max = arr[0];//只需从1下标开始for (i = 1; i < 10; i++) {if (arr[i] > max){max = arr[i];}}printf("max=%d", max);return 0;
}
9.有三个数a,b,c,按大小顺序输出(使用函数p35)
#include<stdio.h>void swap(int* n, int* m)
{int tmp = *n;*n = *m;*m = tmp;
}int main()
{int a = 0;int b = 0;int c = 0;scanf("%d %d %d", &a, &b, &c);if (a < b)swap(&a, &b);if (a < c)swap(&a, &c);if (b < c)swap(&b, &c);//a最大,c最小printf("%d %d %d\n", a, b, c);return 0;
}
10.判断一个数能否被3和5整除(p35)
#include<stdio.h>
int main()
{int a = 0;scanf("%d", &a);if (0 == a % 3 && 0 == a % 5){printf("%d能被3和5整除\n", a);}elseprintf("%d不能被3和5整除\n", a);return 0;
}
11.求两个数m和n的最大公约数,以及最小公倍数(p35、p137)
最小公倍数=两数的积÷最大公约数
1.暴力求解法
#include <stdio.h>
int main()
{int n = 0;int m = 0;int total = 0;scanf("%d %d", &n, &m);total = n * m;//两数之积int min = n < m ? n : m;while (1) {if (n % min == 0 && m % min == 0){break;}min--;}//此时min为最大公约数//最小公倍数就等于:两个数的乘积/最大公约数printf("%d\n", min);printf("%d\n", total / min);
}
2.辗转相除法
#include <stdio.h>
int main()
{int n = 0;int m = 0;int total = 0;scanf("%d %d", &n, &m);total = n * m;//两数之积while (n % m){int tmp = n % m;n = m;m = tmp;}//此时m为最大公约数//最小公倍数就等于:两个数的乘积/最大公约数printf("%d\n", m);printf("%d\n", total / m);
3.递归
- 更相减损法
以较大的数减较小的数,接着把所得的差与较小的数比较,并以大数减小数。继续这个操作,直到它们两个数相等为止。则相等的两个数就是所求的最大公约数。
#include<stdio.h>
int Fun(int n, int m)
{if (n > m){return Fun(m, n - m);}else if (n < m){return Fun(n, m - n);}//两数相等elsereturn n;
}int main()
{int n = 0;int m = 0;int total = 0;scanf("%d %d", &n, &m);total = n * m;//两数之积int ret = Fun(n, m);printf("%d\n", ret);printf("%d\n", total /ret);return 0;
}
- 辗转相除法
此递归和辗转相除法相似
#include<stdio.h>
int Fun(int n, int m)
{ // while (n % m)if (m == 0) //{return n; // int tmp = n % m;else // n = m;return Fun(m, n % m); // m = tmp;//} //printf("%d\n", m);
}
int main()
{int n = 0;int m = 0;int total = 0;scanf("%d %d", &n, &m);total = n * m;//两数之积int ret = Fun(n, m);printf("%d\n", ret);printf("%d\n", total / ret);return 0;
}
12.求方程ax^2+bx+c=0的根(p35)
#include<stdio.h>
#include<math.h>
int main()
{double a, b, c;double flag = 0;double x1, x2;scanf("%lf %lf %lf", &a, &b, &c);flag = b * b - 4 * a * c;if (flag > 0){x1 = (-b + sqrt(flag)) / (2 * a);x2 = (-b - sqrt(flag)) / (2 * a);printf("方程有两个不相等的实根:x1=%f,x2=%f\n", x1, x2);}else if (flag < 0){printf("方程无解\n");}else{x1 = x2 = -b / (2 * a);printf("方程有两个不相等的实根:x1=x2=%f\n", x1);}return 0;
}
第三章
13.温度转换(p37)
#include<stdio.h>
int main()
{float temperature = 0;float Ht = 0;scanf("%f", &temperature);Ht = ( 5.0 / 9) * (temperature - 32);printf("%f\n", Ht);return 0;
}
14.大小写字母转换(p54)
#include<stdio.h>
int main()
{char ch1 = '0';char ch2 = '0';scanf("%c", &ch1);//大写转小写if (ch1 >= 97){ch2 = ch1 - 32;}//小写转大写else{ch2 = ch1 + 32;}printf("%c\n", ch2);return 0;
}
15.给出三角形边长,求三角形的面积(p58)
#include<stdio.h>
#include<math.h>
int main()
{double a = 0;double b = 0;double c = 0;double area = 0;double s = 0;scanf("%lf %lf %lf", & a, & b, & c);s = (a + b + c) / 2;//能构成三角形 (任意两边之和大于第三边)if (a + b > c || a + c > b || c + b > a){area = sqrt(s * (s - a) * (s - b) * (s - c));printf("area= %lf\n", area);}else{printf("不是三角形!\n");}return 0;
}
16.译码p82、p135
int main()
{char ch = 0;while ((ch = getchar()) != '\n'){//如果是字母if (ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z'){//是最后四个字母,就减22if (ch >= 'w' && ch <= 'z' || ch >= 'W' && ch <= 'Z'){ch = ch - 22;}//加4else{ch = ch + 4;}printf("%c", ch);}//是数字elseprintf("%c", ch);}return 0;
}
17.计算面积p82
#include<stdio.h>
int main()
{double r, heigh, circle, area, superarea, v1, v2;double pi = 3.14;scanf("%lf %lf", &r, &heigh);printf("圆的周长:%lf\n", 2 * pi * r);printf("圆的面积:%lf\n", pi * r * r);printf("圆球的表面积:%lf\n", 4 * pi * r * r);printf("圆球的体积:%lf\n", 3.0 / 4 * pi * r * r * r);printf("圆柱的体积:%lf\n", pi * r * r * heigh);return 0;
}
第四章
18.输入一个小于1000的正数,要求输出它的平方根(如平方根不是整数,则输出其整数部分)
#include<stdio.h>
#include<math.h>
int main()
{int num = 0;int sq = 0;do{printf("请输入一个小于1000的正整数:");scanf("%d", &num);} while (num >1000 || num < 0);sq = sqrt(num);printf("%开平方根的结果是:%d\n", sq);return 0;
}
19.有一个函数,输入x,输出y相应的值(p108)
注意:乘号
#include<stdio.h>
int main()
{int x = 0;int y = 0;scanf("%d", &x);if (x < 1){y = x;printf("x=%d y=x=%d\n",x, y);}if (x >= 1 && x < 10){y = 2 * x - 1;printf("x=%d y=2x-1=%d\n", x, y);}if (x >= 10){y = 3 * x - 11;printf("x=%d y=3x-11=%d\n", x, y);}return 0;
}
20.给出100制成绩,要求输出等级(P168)
90分以上为 :A ,80-89为:B,70-79为:C,60-69为:D,60分以下为:E
#include<stdio.h>
int main()
{int score = 0;scanf("%d", &score);switch (score / 10){case 9: printf("A\n");break;case 8:printf("B\n");break;case 7:printf("C\n");break;case 6:printf("D\n");break;default:printf("E\n");break;}return 0;
}
21.给一个不多于5位的正整数!!!!!(P109)
①求出它是几位数;
②分别输出每一位数字;
③按逆序输出各位数字,例如原数为321,应输出123
#include<stdio.h>//输出每一位
void fun(int n)
{if (n > 9){fun(n / 10);}printf("%d ", n % 10);
}int main()
{int num = 0;int count = 1; //输入的至少是一位数吧printf("输入一个不多于5位的正整数:");scanf("%d", &num);//输出位数int tmp = num;while (tmp / 10){count++;tmp = tmp / 10;}printf("共有%d位数\n", count);//输出每一位,递归printf("每一位数如下:");fun(num);printf("\n");//逆序输出printf("逆序输出:");tmp = num;while (tmp){printf("%d ", tmp % 10);//输出个位tmp = tmp / 10; //去除个位}return 0;
}
22.输入4个整数,要求按从小到大的顺序输出p109
与第9题相似
23.输出乘法口诀表
左上
int main()
{for (int i = 9; i > 0; i--){for (int j = 1; j <= i; j++){printf("%d*%d=%2d ", j, i, j * i);}printf("\n");}return 0;
}
左下
int main()
{for (int i = 1; i <= 9; i++){for (int j = 1; j <= i; j++){printf("%d*%d=%2d ", j, i, j * i);}printf("\n");}
}
右上
int main()
{for (int i = 9; i>0; i-- ){for (int k = 0; k < 9-i; k++){printf("\t");}for (int j = 1; j <= i; j++){printf("%2d*%2d=%2d", j, i, i * j);}printf("\n");}return 0;
}
右下
int main()
{for (int i = 1; i <= 9; i++){for (int k = 0; k < 9 - i; k++){printf("\t");}for (int j = 1; j <= i; j++){printf("%2d*%2d=%2d",i, j, i*j);}printf("\n");}return 0;
}
第五章
24.求1-100的和
#include<stdio.h>
int main()
{int sum = 0;int i = 1;/*for (i = 1; i <= 100; i++){sum += i;}*/while (i <= 100){sum += i;i++;}printf("%d\n", sum);return 0;
}
25.学校1000名学生捐款,总数到达10万元结束,统计捐款人数及平均捐款数目(P122)
#include<stdio.h>
#define M 100000
int main()
{double money = 0;double sum = 0;double ave = 0;int i = 0;for (i = 1; i <= 1000; i++){scanf("%lf", &money);sum += money;if (sum >= M){break;}}ave = sum / i; //注意此处是iprintf("人数:%d,平均每人捐:%lf\n", i, ave);return 0;
}
26.输出100-200不能被3整除的数
#include<stdio.h>
int main()
{int i = 0;//int count = 0;for (i = 100; i <= 200; i++){if (i % 3 == 0){continue;}else{printf("%d ", i);//count++;}//5个数一行/*if (count % 5 == 0){printf("\n");}*/}return 0;
}
27.输出下列矩阵
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
#include<stdio.h>
int main()
{int i = 0;int j = 0;for (i = 1; i <= 4; i++) //几行{for (j = 1; j <= 5; j++) //几列{printf("%2d ", i * j);}printf("\n");}return 0;
}
28.用公式 π/4≈1-1/3+1/5-1/7+…求π的近似值,直达发现某一项的绝对值小于10-6为止。
#include<stdio.h>
#include<math.h>
int main()
{double pi = 0;double den = 1;double term = 1.0;int flag = 1;while (fabs(term) >= 1e-6){pi = pi + term; //累加den = den + 2; //分母flag = -flag;term = flag / den; //某一项}pi = 4 * pi;printf("pi=%lf\n", pi);return 0;
}
29.求斐波那契数列得前40 个数(p129)
方法1:循环
#include<stdio.h>
int main()
{int f1 = 1;int f2 = 1;printf("%12d%12d", f1, f2);//int i = 3;int i = 0;int f3 = 0;for (i = 1; i <= 38; i++){f3 = f1 + f2;printf("%12d", f3);f1 = f2;f2 = f3;}/*while (i <= 40){f3 = f1 + f2;printf("%12d", f3);f1 = f2;f2 = f3;i++;}*/return 0;
}
数组
int main()
{int arr[40] = { 1,1 };int i = 0; for (i = 2; i < 40; i++){arr[i] = arr[i - 1] + arr[i - 2];}for (i = 0; i < 40; i++){printf("%12d", arr[i]);//为了避免0的时候换行if ((i+1) % 5 == 0){printf("\n");}}return 0;
}
方法2:递归
int fib(int n)
{if (n == 1 || n == 2){return 1;}else{return fib(n - 1) + fib(n - 2);}
}#include<stdio.h>
int main()
{int f1 = 1;int f2 = 1;printf("%12d%12d", f1, f2);int i = 0;for (i = 3; i <= 40; i++){int ret = fib(i);printf("%12d", ret);}return 0;
}
方法3:一次求两个数
一次求两个数
#include<stdio.h>int main()
{int f1 = 1;int f2 = 1;int i = 0;for (i = 1; i <= 20; i++){printf("%12d%12d", f1, f2);f1 = f1 + f2;f2 = f2 + f1;}return 0;
}
30.统计输入的一行中字母、空格、数字和其它字符的个数(p129)
#include<stdio.h>
int main()
{char ch = 0;int character = 0;int number = 0;int spacing = 0;int other = 0;while ((ch = getchar()) != '\n'){if (ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z'){character++;}else if (ch >= '0' && ch <= '9'){number++;}else if (ch == ' ') //也可写成ch == 32(空格的ascll码为32){spacing++;}elseother++;}printf("character = %d, number = %d, spacing = %d, other = %d\n", character, number, spacing, other);return 0;
}
31求a+aa+aaa+aaaa+aaaaa…nn…nn的值,n表示a的位数,a是一个数字,二者都由键盘输入(p129)
int main()
{int a = 0;int n = 0;scanf("%d %d", &a, &n);int count = 0;int sum = 0;int tmp = a;while (count < n){printf("%d ", a);sum += a;a = a * 10 + tmp; //2*10+2 22*10+2 222*10+2count++;}printf("\nsum = %d\n", sum);return 0;
}
32. 1!+2!+3!+4!+5!..+n!(p129)
方法1:双层for
int main()
{int n = 0;scanf("%d", &n);int sum = 0;for (int i = 1; i <= n; i++){int ret = 1;for (int j = 1; j <= i; j++){//求一个数的阶乘ret = ret * j;}//把每个数的阶乘相加sum += ret;}printf("%d\n", sum);return 0;
}
方法2:单层循环
#include<stdio.h>
int main()
{int n = 0;scanf("%d", &n);int sum = 0;int ret = 1;for (int i = 1; i <= n; i++){ret = ret * i;sum += ret;}//int a = 1;//while (n)//{// ret = ret * a; //n! = (n-1)! * n// a++;// sum += ret;// n--;//}printf("%d\n", sum);return 0;
}
33.求1000以内的水仙花数,例:153 = 13 + 53 + 33(p129)
#include<stdio.h>int main()
{int i = 0;int a, b, c ,sum;for (i = 100; i < 1000; i++){a = i / 100;b = i / 10 % 10;c = i % 10;sum = a * a * a + b * b * b + c * c * c;if (sum == i){printf("%d ", i);}}return 0;
}
34.求1000以内的完美数(一个数恰好等于其真因子之和)例:6 = 1+2+3
#include<stdio.h>
int main()
{int i = 0;for (i = 1; i <= 1000; i++){int sum = 0;int j = 0;for (j = 1; j < i; j++){if (i % j == 0){sum += j;}}if (sum == i){printf("%d is factors are ", i);for (j = 1; j < i; j++){if (i % j == 0){printf("%d ", j);}}printf("\n");}}return 0;
}
35.求一个分数序列的前20项和(p138)
2/1+3/2+5/3+8/5+13/8…
#include<stdio.h>
int main()
{float a = 2;float b = 1;float sum = 0;float tmp = 0;int i = 0;for (i = 1; i <= 20; i++){sum += a / b;tmp = a + b;b = a;a = tmp;}printf("%f\n", sum);
}
36.自由落体(p138)
#include<stdio.h>
int main()
{float sum = 100;float h = sum / 2;for (int i = 2; i <= 10; i++){sum += h * 2;//第n次落地经过的m数h = h / 2; //接下来反弹多高}printf("10次落地共经历%fm\n", sum);printf("10次反弹%fm\n", h);return 0;
}
37.猴子吃桃
#include<stdio.h>
int main()
{int x1 = 0;int x2 = 1;int day = 9;while (day){x1 = (x2 + 1) * 2; //第一天的桃子是第二天桃子加一后的两倍 例:100 吃51 ,剩49x2 = x1;day--;}printf("共有:%d个\n", x1);return 0;
}
38.智能打印菱形
#include<stdio.h>
int main()
{int n = 0;//输入打印几行scanf("%d", &n);int i = 0;//打印上半部分for (i = 0; i < n; i++){//打印空格int j = 0;for (j = 0; j < n -1- i; j++){printf(" ");}//打印**for (j = 0; j < 2 * i + 1; j++){printf("*");}printf("\n");}//打印下半部分for (i = 0; i < n - 1; i++){//打印空格int j = 0;for (j = 0; j <= i; j++){printf(" ");}//打印*for (j = 0; j < 2 * (n - 1 - i) - 1; j++){printf("*");}printf("\n");}return 0;
}
第六章
39.冒泡排序(p144)
普通冒泡排序
int main()
{int arr[10] = { 1,4,8,3,5,0,2,7,9,10 };int i = 0;//一共比较多少趟for (i = 0; i < 10; i++){int j = 0;//每趟比较几次for (j = 0; j < 10 - 1 - i; j++){//从小到大排列if (arr[j] > arr[j + 1]){int tmp = arr[j];arr[j] = arr[j + 1];arr[j + 1] = tmp;}}}for (i = 0; i < 10; i++){printf("%d ", arr[i]);}return 0;
}
双向冒泡排序
//双向冒泡排序
void d_bubble_sort(int arr[], int num)
{int left = 0;int right = num - 1;//优化,如果数组已经有序,就跳出循环int flag = 0;while (left < right){flag = 1;//从左向右找最大for (int i = left; i < right; i++){//前>后,交换if (arr[i] > arr[i + 1]){flag = 0;int tmp = arr[i];arr[i] = arr[i + 1];arr[i + 1] = tmp;}}//优化,如果遍历了一遍数组,没有发生交换,那就说明数组已经有序了if (flag == 1){break;}right--; //最大值已放在右侧//从右向左找最小for (int j = right; j > left; j--){//前>后,交换if (arr[j] < arr[j - 1]){int tmp = arr[j];arr[j] = arr[j - 1];arr[j - 1] = tmp;}}left++;//最小值放在左侧}
}
int main()
{int arr[10] = { 5,3,7,9,1,2,4,8,6,10 };int arr2[10] = { 1,2,3,4,5,6,7,8,9,0 };d_bubble_sort(arr2,10);int i = 0;for (i = 0; i < 10; i++){printf("%d ", arr2[i]);}return 0;
}
40.选择排序
int main()
{int arr[10] = { 1,4,8,3,5,0,2,7,9,10 };int i = 0;for (i = 0; i < 10; i++){int j = 0;int min = i;//将i后的数进行比较for (j = i + 1; j < 10; j++){if (arr[min] > arr[j]){min = j; //找对最小数的下标}}//将数中的最小值与第一个数交换int tmp = arr[i];arr[i] = arr[min];arr[min] = tmp;}for (i = 0; i < 10; i++){printf("%d ", arr[i]);}
}
41.将二维数组行和列的元素互换(p149)
int main()
{int arr1[2][3] = { 1,2,3,4,5,6 };int arr2[3][2] = { 0 };int i = 0;int j = 0;for (i = 0; i < 2; i++){for (j = 0; j < 3; j++){printf("%d ", arr1[i][j]);arr2[j][i] = arr1[i][j];}printf("\n");}for (i = 0; i < 3; i++){for (j = 0; j < 2; j++){printf("%d ", arr2[i][j]);}printf("\n");}return 0;
}
42.输出一个矩阵中的最大值,以及它的下标(p150)
int main()
{int arr[3][4] = { 1,2,3,4,9,8,7,6,-1,-5,-8,-4 };int i = 0;int j = 0;int row = 0;int col = 0;int max = arr[0][0];for (i = 0; i < 3; i++){for (j = 0; j < 4; j++){if (max < arr[i][j]){max = arr[i][j];row = i;col = j;}}}printf("max=%d 下标位:%d,%d\n", max, row, col);return 0;
}
43.统计单词的个数(p163)
int main()
{char str[100] = { 0 };gets(str);int num = 0;int word = 0;int i = 0;for (i = 0; str[i] != '\0'; i++){char c = str[i];if (c == ' ') //是空格,则说明当前位置,单词还没开始,或者刚结束{word = 0;}else if (word == 0) //当前位置不是空格是字符,并且没有操作过,则是一个单词的开始{word = 1;num += word;}//如果该位置不是空格,并且前面不是空格,则说明它属于一个单词,不需要计算}printf("%d\n", num);return 0;
}
44.三个字符串,找出最大者(p164)
#include<stdio.h>
#include<string.h>
int main()
{char arr[3][20];char str[20];int i = 0;for (i = 0; i < 3; i++){gets(arr[i]);}if (strcmp(arr[0], arr[1]) > 0){strcpy(str, arr[0]);}else{strcpy(str, arr[1]);}if (strcmp(arr[2], str) > 0){strcpy(str, arr[2]);}printf("%s\n", str);return 0;
}
45.筛选法求素数(p165)
#include<stdio.h>
//筛选法求1-100的素数
int main()
{int arr[101] = { 0,0 };//1不是素数,可以直接设置为0int i = 0;for (i = 2; i <= 100; i++){arr[i] = i;}//用2 - 99的数去除for (i = 2; i < 100; i++){int j = 0;//被除数是3-100for (j = i + 1; j <= 100; j++){if (arr[j] % i == 0){arr[j] = 0;}}}int count = 0;for (i = 0; i < 101; i++){if (arr[i] != 0){printf("%d\t", arr[i]);count++;}if (count == 10){printf("\n");count = 0;}}return 0;
}
46.求矩阵对角线之和(p165)
正对角线
int main()
{int arr[3][3] = { 1,2,3,4,5,6,7,8,9 };int sum = 0;for (int i = 0; i < 3; i++){for (int j = 0; j < 3; j++){//横、纵坐标相等则是正对角线if (i == j){sum += arr[i][j];}}}printf("%d\n", sum);return 0;
}
反对角线
int main()
{int arr[3][3] = { 1,2,3,4,5,6,7,8,9 };int sum = 0;for (int i = 0; i < 3; i++){for (int j = 0; j < 3; j++){//横、纵坐标的和等于:行数/列数减一if (i+j == 3-1){sum += arr[i][j];}}}printf("%d\n", sum);return 0;
}
47.向一个有序数组中插入一个数,按顺序输出(p165)
#include<stdio.h>
int main()
{int arr[11] = { 1,2,3,4,5,6,7,8,9,10 };int insert = 0;scanf("%d", &insert);int i = 0;//从后往前遍历for (i = 9; i >= 0; i--){//arr[i] > insert,arr[i]往后移if (arr[i] > insert){arr[i + 1] = arr[i];}else{arr[i + 1] = insert;//插入后,停止遍历break;}}//如果insert是最小的,那么i就减到了-1if (i < 0){arr[0] = insert;}for (i = 0; i <= 10; i++){printf("%d ", arr[i]);}return 0;
}
48.逆序数组(p165)
int main()
{int arr[] = { 1,2,3,4,5,6,7,8,9,10};int left = 0;int right = sizeof(arr) / sizeof(arr[0]) - 1;while (left < right){int tmp = arr[left];arr[left] = arr[right];arr[right] = tmp;left++;right--;}for (int i = 0; i < 10; i++){printf("%d ", arr[i]);}return 0;
}
49.杨辉三角(p165)
方法一数组循环
int main()
{int arr[10][10] = { 0 };int i = 0;int j = 0;for (i = 0; i < 10; i++){for (j = 0; j <= i; j++){if (j == 0 || i == j){arr[i][j] = 1;}else{arr[i][j] = arr[i - 1][j] + arr[i - 1][j - 1];}}}for (i = 0; i < 10; i++){//打印空格for (j = 0; j < 10 - i; j++)printf(" ");for (j = 0; j <= i; j++){printf("%4d", arr[i][j]);}printf("\n");}return 0;
}
方法二递归
int func(int m, int n)
{if (m == n || n == 0){return 1;}elsereturn func(m - 1, n) + func(m - 1, n - 1);
}
int main()
{int line = 0;scanf("%d", &line);int i = 0;int j = 0;for (i = 0; i < line; i++){for (j = 0; j < line - i; j++){printf(" ");}for (j = 0; j <=i; j++){printf("%4d", func(i, j));}printf("\n");}return 0;
}
50.找出一个二维数组中的鞍点(p165)
鞍点:即该位置上的元素在该行上最大,在该列上最小。
一个数组也可能没有鞍点
int main()
{int arr[3][3] = { {13,8,7}, {11,12,5}, {14,6,3} };int i = 0;int j = 0;int flag = 1; //假设该数组有鞍点for (i = 0; i < 3; i++){//先假设每行第一个数最大int max_min = arr[i][0];int col = 0;//记下该数是哪一列for (j = 0; j < 3; j++){//如果max_min不是该行最大的,就换if (arr[i][j] > max_min){max_min = arr[i][j];col = j; }}//再遍历每一行的col列,比较是否是该列最小int k = 0;for (k = 0; k < 3; k++){if (arr[k][col] < max_min){flag = 0;break; //有比该数小的数,则该数不是鞍点}}if (flag == 1){printf("该数组的鞍点是:arr[%d][%d]=%d\n", i, col, max_min);break;}}if (flag == 0){printf("该数组没有鞍点\n");}return 0;
}
51.折半查找(p165)
int main()
{int arr[15] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 };int input = 0;scanf("%d", &input);int left = 0;int right = sizeof(arr) / sizeof(arr[0]) - 1;int flag = 1;while (left <= right){int mid = (left + right) / 2;if (input == arr[mid]){printf("找到了,下标是:%d\n", mid);flag = 0;break;}else if (input < arr[mid]){right = mid - 1;}else{left = mid + 1;}}//if (left > right)if (flag){printf("找不到\n");}return 0;
}
52.统计输入的三行中,大小写字母、数字、空格的数量
int main()
{char arr[3][80];int upc = 0;int lowc = 0;int dig = 0;int spa = 0;int other = 0;int i = 0;for (i = 0; i < 3; i++){printf("输入第%d行:", i+1);gets(arr[i]);int j = 0;for (j = 0; j < 80 && arr[i][j] != '\0'; j++){if (arr[i][j] >= 'A' && arr[i][j] <= 'Z'){upc++;}else if (arr[i][j] >= 'a' && arr[i][j] <= 'z'){lowc++;}else if (arr[i][j] >= '0' && arr[i][j] <= '9'){dig++;}else if (arr[i][j] == ' '){spa++;}else{other++;}}}printf("大写:%d,小写:%d,数字:%d,空格:%d, 其它:%d\n", upc, lowc, dig, spa, other);return 0;
}
53.模拟实现strcat
- 数组
int main()
{char str1[20] = { "hello-" };char str2[20] = { "world" };int i = 0;int j = 0;while (str1[i] != '\0'){i++;}while (str2[j] != '\0'){str1[i] = str2[j];i++;j++;}str2[j] = '\0';printf("%s\n", str1);return 0;
}
- 指针
char* my_strcat(char* dest, char* src)
{//char* ret = dest;while (*dest != '\0'){dest++;}//第一种写法while (*src != '\0'){*dest = *src;dest++;src++;}*dest = '\0';//第二种写法/*while (*dest++ = *src++){;}*/return ret;
}int main()
{char str1[20] = "hello-";char* str2 = "world";char* ret = my_strcat(str1, str2);printf("%s\n", ret);return 0;
}
54.模拟实现strcmp
- 数组
int main()
{char str1[20] = { 0 };char str2[20] = { 0 };gets(str1);gets(str2);int i = 0;int j = 0;int ret = 0;while(str1[i] == str2[i] && str1[i] != '\0'){i++;}if (str1[i] == str2[i] && str1[i] == '\0'){ret = 0;}else{ret = str1[i] - str2[i];}printf("%d\n", ret);return 0;
}
- 指针
int my_strcmp(char* str1, char* str2)
{while (*str1 == *str2 ){if (*str1 == '\0'){return 0;}str1++;str2++;}return *str1 - *str2;
}int main()
{char str1[20] = { 0 };char str2[20] = { 0 };gets(str1);gets(str2);int ret = my_strcmp(str1, str2);printf("%d\n", ret);return 0;
}
55.模拟实现strcpy
1.数组
#include<stdio.h>
#include<string.h>
int main()
{char str1[20] = { 0 };char str2[20] = { 0 };gets(str2);int len = strlen(str2);for (int i = 0; i <= len; i++){str1[i] = str2[i];}printf("%s\n", str1);return 0;
}
- 指针
void my_strcpy(char* dest, char* src)
{while (*dest = *src){dest++;src++;}
}int main()
{char str1[20] = { 0 };char str2[20] = { 0 };gets(str2);my_strcpy(str1, str2);printf("%s\n", str1);return 0;
}
第七章
56.函数嵌套求4个数的最大值(p180)
#include<stdio.h>
int Max2(int x, int y)
{return x > y ? x : y;
}int Max_1(int a, int b, int c, int d)
{int m = 0;m = Max2(a, b);m = Max2(m, c);m = Max2(m, d);return m;
}
int main()
{int a, b, c, d, max;scanf("%d %d %d %d", &a, &b, &c, &d);max = Max_1(a,b,c,d);printf("%d\n",max);return 0;
}
57.汉诺塔问题(p188)
#include<stdio.h>void move(char x, char y)
{printf("%c--->%c\n", x, y);
}
void hanoi(int n, char A, char B, char C)
{//A盘只剩一个,直接移动到C盘if (n == 1){move(A, C);}else{//n-1个,从A盘借助C移到B盘hanoi(n - 1, A, C, B);//第n个从A盘移动到C盘move(A, C);//n-1个,从B盘借助A移到C盘hanoi(n - 1, B, A, C);}
}
int main()
{int n = 0;scanf("%d", &n);hanoi(n, 'A', 'B', 'C');return 0;
}
58.写一个函数,调用该函数可求出最大、最小、平均值(p198)
//全局变量
int Max = 0;
int Min = 0;float average(int arr[], int n)
{int i = 0;float sum = 0;Max = Min = arr[0];for (i = 0; i < n; i++){if (arr[i] > Max){Max = arr[i];}else if (arr[i] < Min){Min = arr[i];}sum += arr[i];}return sum / n;
}int main()
{int arr[10] = { 0 };int i = 0;for (i = 0; i < 10; i++){scanf("%d", &arr[i]);}float ave = average(arr, 10);printf("max=%d min=%d ave=%f\n", Max, Min, ave);return 0;
}
59. 有一个字符串,输入一个字符,删除该字符串中的该字符(p213)
1.使用数组
void Delete(char c[], char ch)
{int i = 0;int j = 0;for (i = 0; c[i] != '\0'; i++){if (c[i] != ch){c[j] = c[i]; // 自己放自己里面j++;}}c[j] = '\0';
}
int main()
{char arr[] = "I am student";char ch = 0;printf("%s\n", arr);scanf("%c", &ch);Delete(arr, ch);printf("%s\n", arr);
}
2.使用指针
void Delete(char* c, char ch)
{char* point = c;while (*point != '\0'){if (*point != ch){//当前字符不是要删除的字符,就放进c中*c = *point;//放完后c++c++; }//1.放进c中后,point也++//2.是要删除的字符,直接跳过point++;}*c = '\0';//最后*c的位置放上\0
}
int main()
{char arr[] = "I am student";char ch = 0;printf("%s\n", arr);scanf("%c", &ch);Delete(arr, ch);printf("%s\n", arr);
}
60,反转字符串(p216课后习题)
#include<stdio.h>
#include<string.h>
void reverse(char c[], int left, int right)
{while (left <= right){char tmp = c[left];c[left] = c[right];c[right] = tmp;left++;right--;}
}
void Print(char arr[])
{int i = 0;for (i = 0; arr[i] != '\0'; i++){printf("%c", arr[i]);}printf("\n");
}
int main()
{char arr[] = "abcdef";int start = 0;int end = strlen(arr)-1;Print(arr);reverse(arr, start, end);Print(arr);return 0;
}
61. 写一个函数,输入一个4位数字,要求输出这4个数字字符,两个数字之间空一个空格(p216)
#include<stdio.h>
#include<string.h>
void func(char str[])
{//0123 4//3689 \0//01234567 8//3 6 8 9 \0 int i = 0;//第一个数字不动//别忘记\0for (i = strlen(str); i > 0; i--){str[2 * i] = str[i];str[2 * i - 1] = ' ';}printf("%s", str);
}int main()
{char str[10];scanf("%s", &str);func(str);return 0;
}
62.写一个函数,输入一行字符,将字符串中最长的单词输出(p216)
int judge_alpha(char ch)
{if (ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z'){return 1;}else{return 0;}
}
int TheLongestString(char string[], int len)
{int i = 0;int length = 0;int end_len = 0;int flag = 1;//假设该位置是一个单词的开始int start_point = 0;int place = 0;for (i = 0; i <= len; i++){//是字母,计数if (judge_alpha(string[i])){//是一个单词的开始,记录开始位置if (flag){start_point = i;flag = 0;}//记录完位置,记录长度//不是一个单词的开始,那就说明属于这个单词,计算该单词的长度length++;}//不是字符:单词结束else{flag = 1;if (length > end_len){end_len = length;length = 0; //置为0,继续记录下一个单词的长度}}}return start_point;
}int main()
{char str[20] = { 0 };gets(str);int len = strlen(str);int ret = TheLongestString(str, len);int i = 0;//此处也可以定义全局变量接最长字符串的长度,就不需要调用judeg_alpha函数了for (i = ret; judge_alpha(str[i]); i++){printf("%c", str[i]);}return 0;
}
63. 输入10个学生5门课程的成绩,分别用函数实现以下功能(p216)
- 计算每个学生的平均分
- 计算每门课程的平均分
- 找出所有50个分数中最高的分数所对应的学生和课程
- 计算每个学生平均分的方差
#include<stdio.h>
#define M 2
#define N 5
//计算每个人的平均分
void student_ave(float arr[M][N], float ave[N])
{int i = 0;for (i = 0; i < M; i++){float sum = 0;int j = 0;for (j = 0; j < N; j++){sum += arr[i][j];}ave[i] = sum / N;printf("Num %d: average score = %.2f\n", i+1, ave[i]);}printf("\n");
}//计算每门课程平均分
void lesson_ave(float arr[M][N])
{int i = 0;int j = 0;for (i = 0; i < N; i++){float sum = 0;for (j = 0; j < M; j++){sum += arr[j][i];}printf("lesson%d average:%.2f\n", i + 1, sum / M);}printf("\n");
}
//找出所有课程中最大的分数及其学生、课程名
void findmax(float arr[M][N])
{float max = arr[0][0];int i = 0;int j = 0;int student = 0;int course = 0;for (i = 0; i < M; i++){for (j = 0; j < N; j++){if (arr[i][j] > max){max = arr[i][j];student = i; //记录名字course = j; //记录课程}}}printf("max = %.2f student = %d course = %d\n", max, student+1, course+1);
}
//求平均分的方差
void s_s(float ave[N])
{float sum_s = 0.0;float sum = 0.0;for (int i = 0; i < N; i++){sum_s += ave[i] * ave[i];sum += ave[i];}printf("方差是:%.2f\n", (sum_s / N) - ((sum / N) * (sum / N)));
}int main()
{float arr[M][N] = { 0 };float ave_score[N] = { 0 };int i = 0;for (i = 0; i < M; i++){int j = 0;for (j = 0; j < N; j++){scanf("%f", &arr[i][j]);}}//计算每个学生平均分student_ave(arr, ave_score);lesson_ave(arr);findmax(arr);s_s(ave_score);return 0;
}
64.进制转换,函数实现(p216)
- 十六进制转十进制
int convert(char* p)
{int sum = 0;while (*p != '\0'){if (*p >= 'A' && *p <= 'f'){sum = sum * 16 + *p - 'A' + 10;}else if (*p >= 'a' && *p <= 'f'){sum = sum * 16 + *p - 'a' + 10;}else if(*p >= '0' && *p <="9"){sum = sum * 16 + (*p - '0') * 16;}p++;}return sum;
}int main()
{char str[10] = {0};gets(str);int sum = convert(str);printf("%d\n", sum);return 0;
}
- 十进制转八进制
void convert(int n)
{if (n){convert(n / 8);printf("%d", n % 8);}
}int main()
{int num = 0;scanf("%d", &num);convert(num);return 0;
}
- 十进制转二进制
int convert(int n)
{int sum = 0;int ret = 0;if (n){ret = convert(n / 2) * 10 ;sum = ret + (n % 2);}return sum;
}int main()
{int num = 0;scanf("%d", &num);int ret = convert(num);printf("%d\n", ret);return 0;
}
65.用递归法将一个整数n转换为字符串(p216)
void convert(int n)
{if (n / 10 != 0){convert(n / 10);}putchar(n % 10 + '0');
}int main()
{int n = 0;scanf("%d", &n);if (n < 0){putchar('-');n = -n;}convert(n);return 0;
}
66.给出年月日,计算该日是该年的第几天(p216)
int sum_day(int year, int month, int day)
{int arr[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };int sum = day;for (int i = 1; i < month; i++){sum += arr[i];}if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0){sum++;}return sum;
}int main()
{int year, month, day;scanf("%d %d %d", &year, &month, &day);int ret = sum_day(year, month, day);printf("%d\n", ret);return 0;
}
第八章(均使用指针)
67.按由大到小顺序输出两数,指针实现(p227)
void swap(int* a, int* b)
{int tmp = *a;*a = *b;*b = tmp;
}
int main()
{int a = 0;int b = 0;scanf("%d %d", &a, &b);if (a < b){swap(&a, &b);}printf("max = %d, min = %d\n", a, b);return 0;
}
68.使用指针将n个整数按相反顺序存放(p242)
void reverse(int* arr, int num)
{int n = num / 2;int* left = arr;int* right = arr + num - 1;for (int i = 0; i < n; i++){int tmp = 0;tmp = *left;*left = *right;*right = tmp;left++;right--;}
}int main()
{int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };reverse(arr, 10);int i = 0;for (i = 0; i < 10; i++){printf("%d ", arr[i]);}return 0;
}
69. 输入三个字符串,由小到大输出(p291)
#include<string.h>
void swap(char* s1, char* s2)
{char str[20] = { 0 };strcpy(str, s1);strcpy(s1, s2);strcpy(s2, str);
}int main()
{char str1[20] = { 0 };char str2[20] = { 0 };char str3[20] = { 0 };gets(str1);gets(str2);gets(str3);if (strcmp(str1, str2) > 0)swap(str1, str2);if (strcmp(str1, str3) > 0)swap(str1, str3);if (strcmp(str2, str3) > 0)swap(str2, str3);printf("%s %s %s\n", str1, str2, str3);return 0;
}
70.输入10个数,将最小数与第一个交换,最大数与最后一个交换(p291)
void exchange(int* arr, int num)
{int* max = arr;int* min = arr;int i = 0;for (i = 0; i < num; i++){if (*(arr + i) >= *max){max = arr + i;}if (*(arr + i) <= *min){min = arr + i;}}//若最大值就是首元素,为了避免最小值与首元素交换后找不到最大值if (max == arr){max = min;}int tmp = *arr;*arr = *min;*min = tmp;tmp = *(arr + num - 1);*(arr + num - 1) = *max;*max = tmp;
}int main()
{int arr[10] = { 0 };int i = 0;for (i = 0; i < 10; i++){scanf("%d", &arr[i]);}exchange(arr, 10);for (i = 0; i < 10; i++){printf("%d ", arr[i]);}return 0;
}
71.n个整数,使前面各数向后移动m个位置,最后m个数变成最前面m个数(p291)
其实就是左旋
void rotate(int* parr, int m, int n)
{for (int j = 0; j < m; j++){int tmp = *(parr + n - 1);//旋转一次for (int i = n - 1; i > 0; i--){*(parr + i) = *(parr + i - 1);}//最后一个数放在前面*parr = tmp;}
}int main()
{int arr[10] = { 0 };int i = 0;int m = 3;for (i = 0; i < 10; i++){scanf("%d", &arr[i]);}rotate(arr, m, 10);for (i = 0; i < 10; i++){printf("%d ", arr[i]);}return 0;
}
72.n个人围一圈报数(p291)
int main()
{int arr[100] = { 0 };//定义一个数组,存放每个人喊得数字int count = 0;//几个人scanf("%d", &count);int digit = 1;//要喊得数字int remain = count; //剩余人数while (remain > 1) //还未找出赢家{for (int i = 1; i <= count; i++){if (*(arr+i) == 3){continue;//当前位置是3,跳过}*(arr + i) = digit;if (digit == 3)//喊完3就要喊1了,又因为下面还有个++,所以赋值为0{digit = 0;remain--;//剩余人数-1}digit++;}}for (int j = 1; j <= count; j++){if (*(arr + j) != 3){printf("%d\n", j);break;}}return 0;
}
73.写一个函数,求字符串的长度(p291)
int my_strlen(char* str)
{int count = 0;while (*str != '\0'){count++;str++;}return count;
}int main()
{char* str = "abcdef";printf("%d\n", my_strlen(str));return 0;
}
74.从该字符串的第m的字符开始,全部复制到另一个字符串中(p291)
void my_m_strcpy(char* str1, char* str2, int m)
{int count = 0;while (count < m - 1){count++;str1++;}while (*str1 != '\0'){*str2 = *str1;str2++;str1++;}*str2 = '\0';
}int main()
{char str1[20] = "hello world";char str2[20] = { 0 };int m = 0;scanf("%d", &m);my_m_strcpy(str1, str2, m);printf("%s\n", str2);return 0;
}
75.输入一行字符串,统计大小写字母、数字、空格和其它字符的个数(p291)
int main()
{int upc = 0;int lowc = 0;int digit = 0;int space = 0;int other = 0;char str[50] = { 0 };gets(str);char* p = str;while (*p != '\0'){if (*p >= 'A' && *p <= 'Z'){upc++;}else if (*p >= 'a' && *p <= 'z'){lowc++;}else if(*p >= '0' && *p <= '9'){digit++;}else if (*p == ' '){space++;}else{other++;}p++;}printf("%d %d %d %d %d\n", upc, lowc, digit, space, other);return 0;
}
76.写一个函数,转置矩阵(p291)
void move1(int* parr)
{int i = 0;for (i = 0; i < 3; i++){int j = 0;for (j = i; j < 3; j++){int tmp = 0;tmp = *(parr + 3 * i + j);*(parr + 3 * i + j) = *(parr + 3 * j + i);*(parr + 3 * j + i) = tmp;}}
}void move2(int (*parr)[3])
{int i = 0;for (i = 0; i < 3; i++){int j = 0;//此处应该是j=i,否则就会交换两次,变回原型了for (j = i; j < 3; j++){int tmp = 0;tmp = *(*(parr + i) + j);*(*(parr + i) + j) = *(*(parr + j) + i);*(*(parr + j) + i) = tmp;}}
}int main()
{int arr[3][3] = { 1,2,3,1,2,3,1,2,3};move1(&arr[0][0]); //普通指针,注意传的是第一个元素的地址//int(*p)[3] = arr; //数组指针//move2(p);int i = 0;for (i = 0; i < 3; i++){int j = 0; for (j = 0; j < 3; j++){printf("%d ", arr[i][j]);}printf("\n");}return 0;
}
77. 5×5数组,将最大值放在中间位置,4个最小值放在四个角上(p291)
void operation(int(*p)[5], int row, int col)
{//一、找最大值//为了不记录最大值的下标,此处使用指针更加方便int* mid =&p[row / 2][col/2];int* max = &p[0][0];//1.找最大值for (int i = 0; i < 5; i++){for (int j = 0; j < 5; j++){if (*max < p[i][j]){max = &p[i][j];}}}//2.找到最大值,交换int tmp = *mid;*mid = *max;*max = tmp;//二、找四个最小值//1.记录由四个角的位置int* corner[4] = { &p[0][0], &p[0][col - 1],&p[row-1][0], &p[row-1][col-1] };//2.遍历数组,寻找最小值for (int k = 0; k < 4; k++)//需要寻找四次{int* min = mid; //每次都要将最小值初始化为数组的最大值for (int n = 0; n < row; n++){for (int m = 0; m < col; m++){//3.判断该位置是否是角落位置int t = 0;for (t = 0; t < k; t++){//找第0个最小数的时候,k=0,意味着没有角落被交换if (&p[n][m] == corner[t]){break;}}if (t != k) //说明是break出来的,该位置已经被交换过了,不需要交换了{continue;//}if (*min > p[n][m]){min = &p[n][m];}}}int tmp = *corner[k];*corner[k] = *min;*min =tmp;}
}
int main()
{int arr[5][5] ={{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},};operation(arr,5,5);int i = 0; for (i = 0; i < 5; i++){int j = 0;for (j = 0; j < 5; j++){printf("%d ", arr[i][j]);}printf("\n");}return 0;
}
78.10个字符串,对他们进行排序(p291)
void sort(char* *p, int count)
{int i = 0;for (i = 0; i < count-1; i++){int j = 0;for (j = 0; j < count - 1 - i; j++){//由大到小排序if (strcmp(*(p + j), *(p + j + 1)) < 0){char* tmp = *(p + j);*(p + j) = *(p + j + 1);*(p + j + 1) = tmp;}}}
}
int main()
{int i = 0;char str[10][20] = { 0 };char* arr[10] = { 0 };for (i = 0; i < 10; i++){//指针数组要想初始化,必须先有一个数组scanf("%s", str[i]);arr[i] = str[i];}sort(arr, 10);for (i = 0; i < 10; i++){printf("%s\n", arr[i]);}return 0;
}
79.逆序10个数(p291)
void reverse(int* arr, int count)
{int* left = arr;int* right = arr + count - 1;while (left < right){int tmp = *left;*left = *right;*right = tmp;left++;right--;}
}int main()
{int arr[10] = { 0 };int i = 0;for (i = 0; i < 10; i++){scanf("%d", &arr[i]);}reverse(arr, 10);for (i = 0; i < 10; i++){printf("%d ", arr[i]);}return 0;
}
80.写三个函数,实现以下功能(数组实现)
4个学生,5门课程
- 求一门课程的平均分
- 找出有两门以上课程不及格的学生,打印出信息
- 找出平均成绩在90分或全部课程在85分以上的学生,记为优秀
void average(int arr[4][5], int row, int col, int course)
{int i = 0;int j = 0;int sum = 0;for (i = 0; i < row; i++){sum += arr[i][course - 1];}printf("课程序号:%d, average = %d\n", course, sum / row);
}void find_student(int arr[4][5], int row, int col)
{int i = 0;int j = 0;for (i = 0; i < row; i++){int count = 0;for (j = 0; j < col; j++){if (arr[i][j] < 60){count++;}}if (count > 2){printf("第%d名学生有两门以上不及格\n", i+1);}}printf("\n");}void find_ave_85(int arr[4][5], int row, int col)
{int i = 0;int j = 0;for (i = 0; i < row; i++){int sum = 0;int average = 0;int count = 0;for (j = 0; j < col; j++){sum += arr[i][j];if (arr[i][j] > 85){count++;}}average = sum / col;//平均分大于90或所有全在85以上if (average > 90 || count == col){printf("第%d名学生优秀\n", i + 1);}}printf("\n");
}int main()
{int arr[4][5] = { 0 };int i = 0;int j = 0;for (i = 0; i < 4; i++){for (j = 0; j < 5; j++){scanf("%d", &arr[i][j]);}}//计算某一门课程平均分average(arr, 4, 5, 1);//找2门以上不及格学生find_student(arr, 4, 5);//找优秀学生find_ave_85(arr, 4, 5);return 0;
}
81输入一个字符串,统计其中连续数字的个数,并将数字放在一个数组中
int main()
{char str[100] = { 0 };char a[10][100] = { 0 };gets(str);char* ptr = str;int row = 0;int col = 0;while (*ptr != '\0'){//当前字符是数字,开始读取数字字符串if (*ptr >= '0' && *ptr <= '9') {while (*ptr >= '0' && *ptr <= '9' && *ptr != '\0'){//连续存储一个数字字符串a[row][col] = *ptr;col++;ptr++;}a[row][col] = '\0';//来到这里,说明非数字字符或者\0//1.非数字字符,开始存储下一个数字字符串row++;col = 0;//2.若是\0,跳出循环,停止访问,以防止越界if (*ptr == '\0'){break;}}//不是字符串,指针后移else{ptr++;}}printf("%d个数字\n", row);for (int i = 0; i < row; i++){printf("%s\n", a[i]);}return 0;
}
82.输入月份,输出对应的英文单词,使用指针数组处理
int main()
{char* arr[13] = { NULL,"January","February","March","April","May","June","July","August","Septembet","October","November","December" };int month = 0;scanf("%d", &month);if (month >= 1 && month <= 12){printf("%s\n", arr[month]);}else{printf("illegal input\n");}return 0;
}