学习4-14期视频笔记
1、杨辉三角
#include
#include
void main()
{
#define n 10
int a[n][n] = { 0 };
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j <= i; j++)
{
if (j==0||i == j)
{
a[i][j] = 1;
}
else
{
a[i][j] = a[i - 1][j- 1] + a[i - 1][j];
}
}
}
for (int i = 0; i < n - 1; i++)
{
printf("%*d", 20 -2*i,a[i][0]);
for (int j = 1; j <= i; j++)
{
printf("%4d",a[i][j]);
}
printf("\n");
}
system("pause");
}
2、二维数组
//inta[3][4];
//&a[0][0]等同&a
//&a[i][0]等同&a[i]
//&a[i][j]等同a[i]+j
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 4; j++)
{
printf("%4d",a[i][j]= i *4 + j+1);
}
printf("\n");
}
//12 3 4
//56 7 8
//910 11 12
//转置
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 4; j++)
{
b[j][i] = a[i][j];
}
}
3、多维数组
多维数组在内存中位置是连续的
三维数组:
int a[3][4][5] = {0};
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 4; j++)
{
for (int k = 0; k < 5;k++)
{
a[i][j][k] = i * 4*5+ j*5 +k+ 1;
}
}
}
//for (int i = 0; i < 60; i++)
//{
// a[i/ 4 / 5][i % 20 / 5][i % 20 % 5] = i;
//}
多维数组的查找
4、枚举
enum sex{man,women,weizhi};//0,1,2
for (enum sex a = man; a <=weizhi;a++)
{
printf("%d,",a);
}
输出结果:0,1,
5、生成随机密码
#include
#include
#include
voidmain()
{
time_t times;
srand((unsigned int)time(×));//取随机数种子
int length = rand() % 10 + 6; //6-15位密码
char str[10] = {'a','b','c','d','e','f','g','h','e','j' };
for (int i = 0; i < length; i++)
{
int num = rand() % 10;
printf("%c", str[num]);
}
printf("\n");
system("pause");
}
6、数组与函数
#include
#include
//数组作为函数参数时会退化为指针,传递原地址,而不会创建副本
void show(inta[10])//传递的是地址,数组是例外,//这里的inta[10]与int *a作用等同,可以相互替换
{
printf("%p", &a);//与主函数的地址相同
printf("\n show=%d",sizeof(a));//4
int b[10] = { 0,1,2,3,4,5,6,7,8,9 };
printf("\n show=%d",sizeof(b));//40
}
void main()
{
int a[10] = { 0,1,2,3,4,5,6,7,8,9 };
printf("%p", &a);
printf("%d", sizeof(a));//40
show(a);
system("pause");
}
数组没有副本机制,
7、洗牌
#include
#include
#include
void main()
{
int a[54];
for (int i = 0; i < 54; i++)
{
printf("%d", a[i] = i);
}
srand((unsigned int)time(NULL));//随机数种子
for (int i = 0; i < 53; i++)
{
int num = i+rand()%(53-i);
int temp = a[i];
a[i] = a[num];
a[num] = temp;
}
printf("\n洗牌之后\n");
for (int i = 0; i < 54; i++)
{
printf("%d", a[i]);
}
system("pause");
}