C/PTA——7.数组1

    • 7-1 计算最大值出现的次数
      • 1.题目要求
      • 2.代码实现
    • 7-2 求一批整数中出现最多的个位数字
      • 1.题目要求
      • 2.代码实现
    • 7-3 装箱问题
      • 1.题目要求
      • 2.代码实现
    • 7-4 数组-值钱的微信号
      • 1.题目要求
      • 2.代码实现
    • 7-5 数组-吹泡泡
      • 1.题目要求
      • 2.代码实现
    • 7-6 数组-数学鬼才
      • 1.题目要求
      • 2.代码实现

7-1 计算最大值出现的次数

1.题目要求

2.代码实现

#includeint main(){int arr[1001] = { 0 };int n = 0;scanf("%d", &n);int count = 0;for (int i = 0; i < n; i++){scanf("%d", &arr[i]);}for (int i = 0; i < n - 1; i++)//先排序{int j = 0;for (j = 0; j < n - i - 1; j++){if (arr[j] > arr[j + 1]){int tmp = arr[j];arr[j] = arr[j + 1];arr[j + 1] = tmp;}}}for (int i = 0; i < n; i++)//然后计数{if (arr[i] == arr[n - 1]) count++;}printf("%d %d", arr[n - 1], count);return 0;}

7-2 求一批整数中出现最多的个位数字

1.题目要求

2.代码实现

#includeint main() {int num, b, sum = 0, max = 0;int flag[10];int a[10] = { 0 };for (int i = 0; i < 10; i++) {flag[i] = -1;}scanf("%d", &num);for (int i = 0; i < num; i++) {scanf("%d", &b);if (b == 0) {a[0]++;}else {while (b > 0) {a[b % 10]++;b /= 10;}}}for (int i = 0; i < 10; i++) {if (a[i] > max) {max = a[i];}}for (int i = 0; i < 10; i++) {if (a[i] == max) {flag[sum++] = i;}}printf("%d: ", max);for (int i = 0; i < sum; i++) {if (flag[i] != -1) {printf("%d", flag[i]);if (i != sum - 1) {printf(" ");}}}return 0;}

7-3 装箱问题

1.题目要求

2.代码实现

#includeint main(){int m, j, i;scanf("%d", &m);int a[m];int c[10007] = { 0 };//箱内物品总大小int b[m];//记录箱子号for (i = 0; i < m; i++){scanf("%d", &a[i]);}int k = 0;for (i = 0; i < m; i++){int p = 0;//设置装箱开关for (j = 0; j <= k; j++){if ((c[j] + a[i]) <= 100){c[j] += a[i];b[i] = j + 1;p = 1;break;}}if (p == 0)//未装进则再开一个箱子{k++;c[k] += a[i];b[i] = k + 1;}}for (i = 0; i < m; i++){printf("%d %d\n", a[i], b[i]);}printf("%d", k + 1);return 0;}

7-4 数组-值钱的微信号

1.题目要求

2.代码实现

#includeint main(){int k = 0;int n = 0;scanf("%d %d", &k, &n);int arr[51] = { 0 };for (int i = 0; i < n; i++){scanf("%d", &arr[i]);}for (int i = 0; i < n - 1; i++)//先进行冒泡排序,把消耗的金币按小到大排序{int j = 0;for (j = 0; j < n - i - 1; j++){if (arr[j] > arr[j + 1]){int tmp = arr[j];arr[j] = arr[j + 1];arr[j + 1] = tmp;}}}int sum = 0;int count = 0;for (int i = 0; i < n; i++)//消耗的越少就要的微信个数越多{sum += arr[i];if (sum < k)count++;}printf("%d", count);return 0;}

7-5 数组-吹泡泡

1.题目要求

2.代码实现

#include#define pai 3.14int main(){int n = 0;scanf("%d", &n);double flag[1000] = { 0.0 };//记录泡泡的编号的数组double arr[1000] = { 0.0 };//收集体积的数组for (int i = 0; i < n; i++){double x = 0;scanf("%lf", &x);arr[i] = 1.33 * pai * pow(x, 3);flag[i] = 1.33 * pai * pow(x, 3);}for (int i = 0; i < n - 1; i++)//冒泡排序,按照大到小排序泡泡体积{int j = 0;for (j = 0; j < n - i - 1; j++){if (arr[j] < arr[j + 1]){double tmp = arr[j];arr[j] = arr[j + 1];arr[j + 1] = tmp;}}}for (int i = 0; i < n; i++){printf("%.2lf ", arr[i]);//输出体积for (int j = 0; j < n; j++){if (flag[j] == arr[i]){printf("%d\n", j + 1);//输出编号flag[j]=0.0;//置零是因为有相同的体积,编号会重复,所以置零break;}}}return 0;}

7-6 数组-数学鬼才

1.题目要求

2.代码实现

#includeint main(){int n = 0;scanf("%d", &n);long long sum=1;for (int i = 0; i < n; i++){sum=1;long long x = 0;scanf("%lld", &x);if(x<2018){for(long long j=2;j<=x;j++){sum=(sum*j)%2018;} printf("%lld\n",sum);}else//大于2018的%2018都会是0,会超时,所以直接输出0即可printf("0\n");}return 0;}