[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
一.定义数组
<类型>变量名称[元素数量];
(元素数量必须是整数)
如∶int grades [100];
特点∶①其中所有元素具有相同的数据类型
②一但创建,不能改变大小
(数组中的元素在内存中依次排列)
如int a[10];有十个单位∶a[0],a[1]
……a[9]
每个单元就是一个int类型的变量
[]里面从0开始,被称为下标或索引
二.数组运算
举例∶
const int number=10; int x; int count[number]; int i; for (i=0;i<number;i++){
count[i]=0; } scanf("%d",&x); while(x!=-1){ if(x>=0&&x<=9){ count[x]++; }
scanf("%d",&x); } for(i=0;i<number;i++){ printf("%d:%d\n",i,count[i]); }
其中const int number=10;是数组的大小
int count[number];是定义数组
⑴求数组单元个数
i=sizeof(a)/sizeof(a[0])
⑵数组的赋值
数组变量本身不能被赋值,要把一个数组的所有元素变成另一个数组,必须使用遍历
如:int a[]={ , , , ,}
for(i=0;i<length;i++){ b[i]=a[i]; }
遍历数组常见错误∶
①循环结束条件是<=数组长度
②离开循环后,继续用i的来做数组元素的下标
数组作为函数的参数时(往往必须再用另一个参数来传入数组的大小)∶
①不能在[]中给出数组的大小
②不能再利用sizeof来计算数组的元素个数
三.二维数组
如int [3][5];
a是一个3行5列的矩阵
与一维数组相比,二维数组的遍历则需两重循环
for(i=0;i<3;i++){ for(j=0;j<5;j++){ a[i][j]=i*j; } }
①列数必须给出,行数可以省略
②每行一个{},逗号分隔
③如果省略,表示补零