阵列变数(1)
14 阵列简介
阵列是一群具有相同的资料型态元素集合的资料型态。在记忆体中,一个阵列会使用一段连续的记忆体空间来存放。
阵列的变数定义:
元素资料型态 阵列变数名称 [元素个数] ; int var[3];
初始化时最少需要指定一个值,而未指定值的元素会被自动指定为0
int counter[6] = {0};
在初始化时,阵列元素个数值可以省略,会以初始化元素个数取代。
int counter[ ] = {0, 0, 0, 0, 0, 0};
14.1 骰子点数出现的次数的统计(未使用阵列)
#include <stdio.h> #include <stdlib.h> #include <time.h> int main() { srand(time(0)); int counter1 = 0, counter2 = 0, counter3 = 0; int counter4 = 0, counter5 = 0, counter6 = 0; int i; for (i = 1; i <= 6000; ++i) { int dice = rand() % 6 + 1; switch (dice) { case 1: counter1++; break; case 2: counter2++; break; case 3: counter3++; break; case 4: counter4++; break; case 5: counter5++; break; case 6: counter6++; break; } } printf("1: %d\n", counter1); printf("2: %d\n", counter1); printf("3: %d\n", counter1); printf("4: %d\n", counter1); printf("5: %d\n", counter1); printf("6: %d\n", counter1); return 0; }
未使用阵列
#include <stdio.h> #include <stdlib.h> #include <time.h> int main() { srand(time(0)); int counter[6] = {0}; //初始化 int i; for (i = 1; i <= 6000; ++i) { int dice = rand() % 6 + 1; switch (dice) { case 1: counter[0]++; break; case 2: counter[1]++; break; case 3: counter[2]++; break; case 4: counter[3]++; break; case 5: counter[4]++; break; case 6: counter[5]++; break; } } printf("1: %d\n", counter[0]); printf("2: %d\n", counter[1]); printf("3: %d\n", counter[2]); printf("4: %d\n", counter[3]); printf("5: %d\n", counter[4]); printf("6: %d\n", counter[5]); return 0; } 1: 982 2: 1014 3: 992 4: 1032 5: 1039 6: 941 Process returned 0 (0x0) execution time : 13.322 s Press any key to continue.
使用阵列后
#include <stdio.h> #include <stdlib.h> #include <time.h> int main() { srand(time(0)); int counter[6] = {0}; int i,j; for (i = 1;i <= 6000; ++i) { int dice = rand() % 6 + 1; for (i = 1; j <= 6; ++j) { if (dice == j) { counter[j-1]++; } } } for (j = 1; j <= 6; ++j) { printf("%d: %d\n", j, counter[j-1]); } return 0; }
这个练习可能很难输出,但是要理解其中的阵列含义
再一次
#include <stdio.h> #include <stdlib.h> #include <time.h> int main() { srand(time(0)); int counter[6] = {0}; int i, j; for (i = 1; i <= 6000; ++i) { int dice = rand() % 6 + 1; counter[dice-1]++; } for (j = 1; j <= 6; ++j) { printf("%d: %d\n", j, counter[j-1]); } return 0; } 1: 1001 2: 993 3: 975 4: 1044 5: 1013 6: 974 Process returned 0 (0x0) execution time : 1.242 s Press any key to continue.
// 一开始用简单的方法 #include <stdio.h> int main() { int total = 0, id; do { scanf("%d", &id); switch (id) { case 1: total += 90;break; case 2: total += 75;break; case 3: total += 83;break; case 4: total += 89;break; case 5: total += 71;break; } } while (id != 0); printf("Total: %d\n", total); return 0; } 1 2 3 4 5 1 2 3 4 5 0 Total: 816 Process returned 0 (0x0) execution time : 11.317 s Press any key to continue.
// 使用阵列后 #include <stdio.h> int main() { int prices[5] = {90, 75, 83, 71}; int total = 0, id; while(1) { scanf("%d", &id); if (id == 0) { break; } total += prices[id-1]; } printf("Total: %d\n", total); return 0; } #include <stdio.h> int main() { int prices[5] = {90, 75, 83, 71}; int total = 0, id; do { scanf("%d", &id); if (id != 0) { total += prices[id-1]; } } while (id != 0); printf("Total: %d\n", total); return 0; }
阵列使其变得更为弹性
14.5 两颗骰子点数和出现次数统计和的练习(使用阵列)
#include <stdio.h> #include <stdlib.h> #include <time.h> int main() { srand(time(0)); int counter[11] = {0}; int i; for (i = 1; i <= 10000; i++) { int dice1 = rand() % 6 + 1; int dice2 = rand() % 6 + 1; int sum = dice1 + dice2; counter[sum-2]++; } for (i = 2; i <= 12; i++) { printf("%d: %d\n", i, counter[i-2]); } return 0; } 2: 284 3: 597 4: 802 5: 1135 6: 1381 7: 1710 8: 1370 9: 1039 10: 825 11: 573 12: 284 Process returned 0 (0x0) execution time : 1.121 s Press any key to continue.
15.1 查询数字的练习(使用阵列)
#include <stdio.h> int main() { int i,n[5]; for (i = 1; i <= 5; i++) { printf("%d:",i); scanf("%d", &n[i-1]); } while (1) { printf("Q: "); scanf("%d", &i); if (i == 0) { break; } printf("%d\n", n[i-1]); } return 0; } 1:1 2:23 3:67 4:77 5:89 Q: 5 89 Q: 0 Process returned 0 (0x0) execution time : 38.987 s Press any key to continue.
15.2 查询范围内数字的练习(使用阵列)
#include <stdio.h> int main() { //存数字 int i,n[10]; for (i = 1; i <= 10; i++) { scanf("%d", &n[i-1]); } while(1) { int l, r; printf("L R: "); scanf("%d%d", &l, &r); if(l == 0 && r == 0) { break; } printf("Ans: "); for (i = 0; i < 10; i++ ) { if (n[i] >= l && n[i] <= r) { printf("%d", n[i]); } } printf("\n"); } return 0; } 07 17 27 37 47 57 67 77 87 97 L R: 70 80 Ans: 77 L R: 1 90 Ans: 71727374757677787 L R: 77 97 Ans: 778797 L R: 0 0 Process returned 0 (0x0) execution time : 39.275 s Press any key to continue.
15.3 查詢上限內最大數字的練習 (使用陣列)
#include <stdio.h> int main() { int i, n[10], q; for (i = 1; i <= 10; i++) { scanf("%d", &n[i-1]); } // 用数组来存10个数字,就是这样存的。 while (1) {// 无限回圈 int max_i = -1; // 每次它都要归零 printf("Q: "); scanf("%d", &q); if (q == 0) { break; // 终止条件 } for (i = 0; i < 10; i++) { if (n[i] <= q && (max_i == -1 || n[i] > n[max_i])) {// 这里的||一个成立就够了 max_i = i; // 索引号 } } if (max_i != -1) { printf("%d\n", n[max_i]); } } return 0; } 12 23 34 56 67 78 98 87 97 99 Q: 10 Q: 15 12 Q: 100 99 Q: 13 12 Q: 11 Q: 0 Process returned 0 (0x0) execution time : 25.608 s Press any key to continue.
15.4 查詢最接近數字的練習 (使用陣列)
#include <stdio.h> int main() { int i,q,n[10]; for (i = 1; i <= 10; i++) { scanf("%d", &n[i-1]); } while (1) { printf("Q: "); scanf("%d", &q); if (q == 0) { break; } int nearest_n = n[0], nearest_d = abs(q-n[0]); for (i = 1; i < 10; i++) { int d =abs(q-n[i]); if ( d < nearest_d || (d == nearest_d && n[i] < nearest_n)) { nearest_d = d; nearest_n = n[i]; } } printf("%d\n", nearest_n); } return 0; } 13 23 78 77 37 30 40 50 83 99 Q: 25 23 Q: 34 37 Q: 9 13 Q: 0 Process returned 0 (0x0) execution time : 30.611 s Press any key to continue.
15.5 写数字统计长条图的练习(使用阵列)
#include <stdio.h> int main() { int i, j, n, b[10] = {0}; for (i = 1; i <= 10; i++) { scanf("%d", &n); b[(n-1)/10]++; //存数 } for (i = 1; i <= 10; i++) { printf("%3d: ", i*10); for (j = 1; j <= b[i-1]; j++) { //这里的b[i-1]表示一个区间里有几个星星 printf("*"); } printf("\n"); }return 0; } 12 24 35 45 55 65 75 88 98 99 10: 20: * 30: * 40: * 50: * 60: * 70: * 80: * 90: * 100: ** Process returned 0 (0x0) execution time : 36.454 s Press any key to continue.
15.6 求小范围内的众数 (使用阵列)
当次数一样多的时候,数大的输出。
#include <stdio.h> int main() { int i, n, b[10] = {0}; for (i = 1; i <= 10; i++) { scanf("%d", &n); b[n]++; } int ans = 0; //ans表示出现最多次数的那个数 for (n = 1; n < 10; n++) { if (b[n] >= b[ans]) { ans = n; } } printf("Ans: %d\n", ans); return 0; } 1 3 4 4 5 4 4 6 7 8 Ans: 4 Process returned 0 (0x0) execution time : 18.670 s Press any key to continue.