==================================
2022-7-6
==================================
一.温故知新
赋值运算符 = += -= *= /= ...
a += 1 => a = a + (1)
条件运算符 ? :
part.1 ? part.2 : part.3;
强制类型转换 (数据类型)
求字节大小 sizeof
自增自减 ++ --
变量名++;
++变量名;
-----------------------
分支结构
if else
if(条件)
{
语句
}
else if(条件)
{
语句
}
else if(条件)
{
语句
}
=======================================
homework
1.
(1)打印4 * 5 的 *
例:
*****
*****
*****
*****
(2).打印i * j 的 * (i j 键盘接收)
例:i = 4, j = 5
//4-5 5-6
*****
*****
*****
*****
(3).打印i * j 的 * 边框(i j 键盘接收)
例: i = 4, j = 5
*****
* *
* *
*****
2.打印99乘法表
1 * 1 = 1
1 * 2 = 2 2 * 2 = 4
*
**
***
****
3.计算1+2+3+4+5+...+n, n是从键盘输入的自然数
// 穷举
4.要将5张100元的大钞票,换成等值的50 20 10 5小钞票,每种面值至少一张,要多少张,有多少种换法
5.有1、2、3、4 4个数字,能组成多少个互不相同且无重复的三位数呢
6.有这样的一个等式,xyz+yzz=532,编程求x y z的值
7.计算1^2 + 2^2 + 3^2 + ... +10^2的结果
(1的平方 + 2的平方)
8.输入20个人的成绩,输出不及格的人数(0<=score<60 为不及格)
9.随机产生100个三位随机数,找出其中能被 3 5 7整除的数
10.打印0~100以内所有的质数
(只能被1 和 本身整除的数)
11.猜数游戏(随机产生一个1000以内的数,去猜,
如果大了打印猜的大了,
如果小了打印猜的小了,
如果正确打印对的
直到猜对了再结束,每次打印必须打印出猜的次数)
12. 把11修改成,程序开始时,先输入需要猜的数的范围n
例:(想要猜数的范围是(0~10000) 那么变量n接收键盘上10000
范围都是从0开始)
13.
----------------------------
a + b - 9 = 4
+ - -
c - d * e = 4
/ * -
f + g - h = 4
= = =
4 4 4
求a b c d e f g h (0 ~ 12范围内)//除法指的是整除 c % f == 0
只有一种答案
14.求1+12+123+...前N项之和,其中N是从键盘输入的数(N取值范围(1~9))
n = 5;
sum = 1 + 12 + 123 + 1234 + 12345;
15.爱因斯坦出了一道这样的数学题,有一条长阶梯,若每步跨2阶,则最后剩1阶,若每步跨3阶,则最后省2阶,若每步跨5阶,则最后剩4阶,若每步跨6阶,则最后剩5阶只有每次跨7阶,最后才正好1阶不剩,请问在这条阶梯最少多少阶
16.设N是一个4位数,它的9倍恰好是它的反序数
例:1234 反序数是4321
17.求s = a + aa + aaa + aaaa + ... + aa...a的值,其中a是1~9的一个数字(不超过十项)
例:a = 3; s = 3 + 33 + 333;
18.“百钱百鸡”问题,百钱买百鸡,公鸡1个3块钱,母鸡1个2块钱,小鸡3个一块钱,问公鸡,母鸡,小鸡各多少个
19.打印三角形 (画8个三角形)
* * * * *
* * * *
* * *
* *
*
6_1.打印三角形
录入n,n是直角三角形的高
例: n = 3
*
* * *
* * * * *
n = 4
*
* * *
* * * * *
* * * * * * *
20.猴子吃桃问题,有一天一只猴子摘了一些桃子,它吃掉一半,又吃了一个,第二天也是这样,到了第十天,只有一个桃子了,求猴子摘了多少桃子
21. 4 5 6 7总共能组成多少个不能被4整除的4位数
22.求n!(n的阶乘)
/*
三角形
*/
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
| #include <stdio.h>
int main(void) { int a = 0, b = 0, c = 0;
scanf("%d-%d-%d", &a, &b, &c); if((a+b>c) && (a+c>b) && (b+c>a)) { if((a*a+b*b==c*c) || (a*a+c*c==b*b) || (b*b+c*c==a*a)) { printf("可以构成直角三角形!\n"); } else if(a==b || a==c || b==c) { if(a==b && a==c) { printf("可以构成等边三角形!\n"); } else { printf("可以构成等腰三角形!\n"); } } else { printf("可以构成普通三角形!\n"); } } else { printf("不能构成三角形!\n"); }
return 0; }
|
/*
从小到大打印
*/
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
| #include <stdio.h>
int main(void) { int x = 0, y = 0, z = 0; int tmp = 0;
scanf("%d-%d-%d", &x, &y, &z);
if(x > y) { tmp = x; x = y; y = tmp; }
if(x > z) { tmp = x; x = z; z = tmp; }
if(y > z) { tmp = y; y = z; z = tmp; }
printf("%d<%d<%d\n", x, y, z);
return 0; }
|
/*
输入相应的字符打印相应的语句(默认不区分大小写)
输入a,打印 钟薛高18块钱
输入b,打印 梦龙8块钱
输入c,打印 老冰棒1块钱
输入d,打印 雪莲0.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 26 27 28 29 30 31
| #include <stdio.h>
int main(void) { char c = 0;
scanf("%c", &c);
if(c == 'A' || c == 'a') { printf("钟薛高18块钱\n"); } else if(c == 'B' || c == 'b') { printf("梦龙8块钱\n"); } else if(c == 'C' || c == 'c') { printf("老冰棒1块钱\n"); } else if(c == 'D' || c == 'd') { printf("雪莲0.5块钱\n"); } else { printf("录入失败,请重新录入!\n"); }
return 0; }
|
分支语句
switch(也有程序员会叫做开关语句)
1.switch case default都是C语言的关键字
2.switch语句的结构
语句1;
switch(变量)
{
case 整常量1: 语句2;
case 整常量2: 语句3;
default : 语句4;
}
语句5;
语句1->变量和整常量1是否相等->语句2->语句3->语句4->语句5->end
|
不相等->变量和整常量2是否相等->语句3->语句4->语句5->end
|
不相等->语句4->语句5->end
3.整常量就是整数或者可以表示为整数的内容(字符)
4.在同一个switch...case结构中整常量不能重复
5.在switch语句中加入break关键字
语句1;
switch(变量)
{
case 整常量1: 语句2; break;
case 整常量2: 语句3; break;
default : 语句4; break;
}
语句5;
语句1->变量和整常量1的判断->语句2->break->语句5->结束
|
(不相等)->变量和整常量2的判断->语句3->break->语句5->结束
|
(不相等)->语句4->break->语句5->结束
6.在switch语句中执行break会跳出switch语句
输入相应的字符打印相应的语句(默认不区分大小写)
输入a,打印 钟薛高18块钱
输入b,打印 梦龙8块钱
输入c,打印 老冰棒1块钱
输入d,打印 雪莲0.5块钱
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| #include <stdio.h>
int main(void) { int i = 0;
scanf("%d", &i);
switch(i) { case 1 : printf("111111\n"); case 2 : printf("222222\n"); case 3 : printf("333333\n"); case 4 : printf("444444\n"); default: printf("ERROR!\n"); }
return 0; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| #include <stdio.h>
int main(void) { char i = 0;
scanf("%c", &i);
switch(i) { case 'a' : printf("1111111111111\n"); case 'b' : printf("2222222222222\n"); case 'c' : printf("3333333333333\n"); case 'd' : printf("4444444444444\n"); default: printf("ERROR!\n"); }
return 0; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| #include <stdio.h>
int main(void) { char i = 0; char c = 'a';
scanf("%c", &i);
switch(i) { case 'b' : printf("1111111111111\n"); case 'b' : printf("2222222222222\n"); case 'c' : printf("3333333333333\n"); case 'd' : printf("4444444444444\n"); default: printf("ERROR!\n"); }
return 0; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| #include <stdio.h>
int main(void) { char c = 0;
scanf("%c", &c);
switch(c) { case 'a' : case 'A' : printf("钟薛高18块钱\n"); break; case 'b' : case 'B' : printf("梦龙8块钱\n"); break; case 'c' : case 'C' : printf("老冰棒1块钱\n"); break; case 'd' : case 'D' : printf("雪莲0.5块钱\n"); break; default : printf("ERROR\n"); break; }
return 0; }
|
/*
录入成绩
0 ~ 59 打印 不及格
60 ~ 69 打印 及格
70 ~ 79 打印 中
80 ~ 89 打印 良
90 ~ 100 打印 优秀
*/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| #include <stdio.h>
int main(void) { int score = 0;
scanf("%d", &score);
switch(score / 10) { case 0..5 : printf("不及格!\n"); break; case 6 : printf("及格!\n"); break; case 7 : printf("中!\n"); break; case 8 : printf("良!\n"); break; case 9 : case 10: printf("优秀!\n"); break; }
return 0; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| #include <stdio.h>
int main(void) { char score = 0;
scanf("%c", &score);
switch(score) { case '0' ... '9': printf("Hello World!\n"); break; case 'd' ... 'f': printf("Hello Beijing!\n"); break; }
return 0; }
|
循环语句
1.for是C语言的关键字
2.for循环的使用方法
语句1
for(循环变量的赋值; 循环变量的控制; 循环变量的变化)
{
循环的语句
}
语句2
语句1
|
V
循环变量的赋值
|
V
|--------循环变量的控制<---------
| | 循环变量的变化
V V ^
假 真 |
| 循环的语句------->-------
|
----------------
|
V
语句2
在循环中不要改变循环变量的值否则会影响循环次数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| #include <stdio.h>
int main(void) { int i = 0;
for(i = 0; i < 5; i += 1) { printf("i = %d\n", i); }
return 0; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| #include <stdio.h>
int main(void) { int i = 0;
for(i = 0; i < 10; i++) { printf("%d\n", ++i); }
printf("==== i = %d\n", i);
return 0; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13
| #include <stdio.h>
int main(void) { int i = 0;
for(i = 1; i < 100; i+=2) { printf("%d\n", i); }
return 0; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| #include <stdio.h>
int main(void) { int i = 0; int sum = 0;
for(i = 1; i <= 100; i++) { sum += i; }
printf("sum = %d\n", sum);
return 0; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| #include <stdio.h>
int main(void) { int i = 100; int b = 0, s = 0, g = 0;
for(i = 100; i < 1000; i++) { b = i / 100; s = i / 10 % 10; g = i % 10; if(i == (b*b*b) + (s*s*s) + (g*g*g)) printf("i = %d\n", i); }
return 0; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| #include <stdio.h>
int main(void) { int i = 1; int b = 0, s = 0, g = 0;
for(b = 1; b < 10; b++) { for(s = 0; s < 10; s++) { for(g = 0; g < 10; g++) { if((b*b*b)+(s*s*s)+(g*g*g)==b*100+s*10+g) printf("%d%d%d\n",b,s,g); } } }
return 0; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| #include <stdio.h> #include <unistd.h> #include <stdlib.h>
int main(void) { int hour = 0, min = 0, sec = 0;
for(hour = 0; hour < 24; hour++) { for(min = 0; min < 60; min++) { for(sec = 0; sec < 60; sec++) { system("clear"); printf("%02d : %02d : %02d\n", hour,min,sec); sleep(1); } } }
return 0; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| #include <stdio.h> #include <unistd.h>
int main(void) { int i = 0;
for(i = 0; i < 5; i++) { printf("%d", i); sleep(1); }
return 0; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| #include <stdio.h>
int main(void) { int i = 0, j = 0;
for(i = 0; i < 4; i++) { for(j = 0; j < 5; j++) { printf("[%d %d]", i, j); } printf("\n"); }
return 0; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| #include <stdio.h>
int main(void) { int i = 0, j = 0;
for(i = 0; i < 4; i++) { for(j = 0; j < 5; j++) { printf("*"); } printf("\n"); }
return 0; }
|
/*
1 2 3 4 5
| * * * * * * * * * * * * * * *
|
*/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| #include <stdio.h>
int main(void) { int i = 0, j = 0;
for(i = 1; i < 10; i++) { for(j = 1; j <= i; j++) { printf("%d X %d = %2d ", j, i, i * j); } printf("\n"); }
return 0; }
|