C语言学习3

C语言笔记3

第四课 class 4

guess number:

when the number is between 1 to 100, we need to guess for 7 times maximum.

read more: https://arya-1017.github.io/2020/07/12/《算法图解》读书笔记1/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <stdio.h>
#include <stdlib.h>
#include <time.h> //import the time package

int main(int argc, char *argv[]) {
int n,g;
int count=1;
srand((unsigned)time(NULL)); //make the random number changes as the time changes
printf("guess a number from 0 to 100.\n");
scanf("%d",&g);
n=rand()%100+1; //make it within 1 to 100
while(g!=n)
{
if (g>n) printf("It is too large!\n");
else printf("It is too small!\n");
scanf("%d",&g);
count++;
}
printf("You are right! You used %d times.\n",count);
return 0;
}

guess a number from 0 to 100.
50
It is too large!
25
It is too small!
37
It is too large!
30
It is too large!
27
You are right! You used 5 times.

antitone and positive sequence:

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
//antitone
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
int number,i,num,result;
scanf("%d",&number);
while(1){
num=number%10; //get the last digit
result=result*10+num; //put the number at the end of result
number/=10; //delete the last digits
if(number==0) break;
}
printf("%d",result);
}

/*input:12345
output:54321*/


//positive with space between
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(int argc, char *argv[]) {
int number,i,n,result,num;
n=0;
scanf("%d",&number);
num=number;

while(1){
n++;
number/=10;
if(number==0) break; //count the digits
}

for(i=1;i<=n;i++){
result=num/pow(10,n-i); //get the first digit
printf("%d ",result);
num=num-result*pow(10,n-i); //delete the last digit
}
return 0;
}

/*input:12345
output:1 2 3 4 5*/

for循环嵌套

输入一个小于6的正整数,输出从它开始的4个数的所有排序方法组成的3位数

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
#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
int a,j,n,count=0;
scanf("%d",&a);
for(int i=a;i<a+4;i++){
for(j=a;j<a+4;j++){
for(n=a;n<a+4;n++){
if(i!=j &&i!=n && j!=n){
printf("%d%d%d",i,j,n);
count++;
if(count<6) printf(" ");
else{
printf("\n");
count=0;
}
}
}
}
}
return 0;
}

念数字read the numbers

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
int number,i,n,result,num;
n=0;
scanf("%d",&number);
if (number<0){
printf("fu ");
number=-number;
}
num=number;
while(1){
n++;
number/=10;
if(number==0) break;
}

for(i=1;i<=n;i++){
result=num/pow(10,n-i);
switch(result){
case 1:printf("yi ");break;
case 2:printf("er ");break;
case 3:printf("san ");break;
case 4:printf("si ");break;
case 5:printf("wu ");break;
case 6:printf("liu ");break;
case 7:printf("qi ");break;
case 8:printf("ba ");break;
case 9:printf("qiu ");break;
case 0:printf("ling ");break;
}
num=num-result*pow(10,n-i);
}

第五课 class 5

一维数组

类型说明符 数组名[常量表达式] //常量表达式为个数(连续的),正整数。

数组只包含一种类型

int a[10];

引用:a[0], a[1], … …, a[9] //不能一次引用全部的数组

下标在上下界之内

打印数组名打印出的为存放数组首个元素的地址

初始化:在赋值时:int a[8]={0,1,2,3,4,5,6,7}, 若数组大小小于元素数,编译错误

若部分赋值,则其余补零(不可只定义空的大括号)

初始化时可以不填写常量表达式而给元素赋值

若没有赋值,元素为随机值

常量表达式可以用已定义的变量(或其计算)代替,但此时不可赋值

用下标越界可访问数组前后地址的值

冒泡法:(排序)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main(int argc, char *argv[]) {
int i,d[6]={13,76,97,65,38,49},n,t;
for(i=1;i<6;i++){ //进行5轮比较
for(n=1;n<6-i;n++){ //相邻数字两两相比,最值会被排在最左或最右
if(d[n]>d[n+1]){ //交换位置
t=d[n];
d[n]=d[n+1];
d[n+1]=t;
}
}
}
for(i=0;i<6;i++){
printf("%d ",d[i]);
}
return 0;
}

二维数组

类型说明符 数组名[常量表达式] [常量表达式]

int a[4][2] //行可以不写(前),列一定要限定

a[0][0] a[0][1] a[0][2] a[0][3]

a[1][0] a[1][1] a[1][2] a[1][3]

e.g.将每行最大值放在开头

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
int main(int argc, char *argv[]) {
int i,j,t=0,a[3][4]={{1,2,3,4},{8,5,9,0},{6,7,3,1}};
int index;
for(i=0;i<=2;i++){
for (j=0;j<=3;j++){
printf("%d ",a[i][j]);
}
printf("\n");
}
for(i=0;i<=2;i++){
index=0;
t=a[i][j];
for (j=0;j<=3;j++){
if(t<a[i][j]){
t=a[i][j];
index=j;
}
}
printf("%d\n",t);
if(index==0) continue;
else
{
a[i][index]=a[i][0];
a[i][0]=t;
}
}
printf("after:\n");
for(i=0;i<=2;i++){
for (j=0;j<=3;j++){
printf("%d ",a[i][j]);
}
printf("\n");
}
return 0;
}

第六课 class 6

字符数组

char 数组名[常量表达式]

char 数组名[常量表达式1][常量表达式2]

int a[6]; 合法但浪费内存

char c[3]={‘c’,’h’,76} //数字为ascii码

若字符个数<数组长度,则剩下的用空字符代替(’\0’)

char a[10]={“china”}; or char a[]=”china”;

*单引号为字符,双引号为字符串

char str[3][6]={“zhang”,”wang”,”li”}; //三行,每行最多6个

printf(“%s%c”,str[0],str[1][3]);

gets(字符数组名):输入字符串(可输入空格),遇到第一个回车结束

puts():输出字符串:puts(字符数组名)、puts(字符串)//字符串加双引号,输出后直接换行

一般字符个数比字符串多一个(即\0),printf遇到空字符时结束

scanf 遇到空格、回车、tab结束输入

如:

1
2
3
4
5
char c1[6],c2[6];
scanf("%s%s",c1,c2);
//输入:a good book
printf("%s %s",c1,c2);
//输出结果:a good

将字符串赋给字符,赋的值为首位字符地址,加一即为下一位

1
2
3
4
5
char str[6]="china";
char c;
c=*(str+1);
printf("%c",c);
//out:h

字符串处理

#include “string.h”

strcat(字符数组1,字符数组2)

字符数组1空间要能容纳字符数组2

strcopy(字符数组1,字符数串2)

覆盖字符数组1,复制到第一个’\0’前

strlen(字符串)

包括有效字符(包括空格和tab),不包括‘\0’

代替:for(i=0;str[i]!=’\0’;i++);

strcmp(字符串1,字符串2);

比较:从左往右比较ascii码:第一个大则返回1,小则返回-1,相同往后移一位直到结束

strlwr():变小写

strupr():变大写