C/C++ 作业笔记

7-4 查找整数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <stdio.h>

int main()
{
int n, b;
scanf("%d %d", &n, &b);
int shu[n];
int found = 0;
for (int i = 0; i < n; i++) {
scanf("%d", &shu[i]);
if (shu[i] == b) {
printf("%d", i);
found = 1;
break;
}
}

if (!found)
{
printf("Not Found");
}

return 0;
}

7-3 交换最小值和最大值

解题思路;

  • 将数组元素排序,找出最值。
  • 通过最值对原数组遍历,找到元素,并与[0],[n-]交换数值.
    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
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    #include <stdio.h>

    int swap(int *x,int *y)//交换函数
    {
    int jh=0;
    jh = *x;
    *x = *y;
    *y = jh;
    }


    int main()
    {
    int shu[10];
    int shu1[10];
    int n;
    scanf("%d",&n);
    for (int i=0;i<n;i++)
    {
    scanf("%d",&shu[i]);
    }
    for (int u=0;u<n;u++)
    {
    shu1[u] = shu[u];
    }// 复制原数组
    for (int j=0;j<n-1;j++)
    {
    for (int k=0;k<n - 1 -j;k++)
    {
    if (shu[k] > shu[k + 1])
    {
    swap(&shu[k],&shu[k+1]);
    }
    }
    }// 排序

    for (int ch=0;ch<n;ch++)
    {
    if (shu1[ch] == shu[0])
    {
    swap(&shu1[0],&shu1[ch]);
    break;
    }
    }// 查找min 并与[0] 交换
    for (int ch=0;ch<n;ch++)
    {
    if (shu1[ch] == shu[n -1])
    {
    swap(&shu1[n - 1],&shu1[ch]);
    break;
    } // 查找max
    }
    for (int i=0;i<n;i++)
    {
    printf("%d ",shu1[i]);
    }
    }

7-2 求矩阵各行元素之和

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <stdio.h>
int main()
{
int m,n;
scanf("%d %d",&m,&n);
int shu[m][n];
for (int i=0;i<m;i++)
{
for (int j=0;j<n;j++)
{
scanf("%d",&shu[i][j]);
}
}
for (int i=0;i<m;i++)
{
int sum = 0;
for (int j=0;j<n;j++)
{
sum += shu[i][j];
}
printf("%d\n",sum);
}

}

7-4 字符串加密

解题思路;

  • 元素转换规律: str = ‘Z’ - (str - ‘A’)
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    #include <stdio.h>
    #include <string.h>

    void encrypt(char *str) {
    for (int i = 0; str[i] != '\0'; i++) {
    if (str[i] >= 'A' && str[i] <= 'Z') {
    str[i] = 'Z' - (str[i] - 'A');
    } else if (str[i] >= 'a' && str[i] <= 'z') {
    str[i] = 'z' - (str[i] - 'a');
    }
    }
    }

    int main()
    {
    char str[128];
    fgets(str,128,stdin);
    encrypt(str);
    printf("%d\n",strlen(str));
    printf("%s",str);
    return 0;
    }

7-5 幸运数字

解题思路;

  • 使用一个数组(count[10])来保存0-9,出现的次数.下标表示lucky_number.
    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
    #include <stdio.h>
    #include <string.h>

    int main() {
    char input[101]; // 存储输入数字字符串,长度不超过100
    int count[10] = {0}; // 用于统计数字0-9出现的次数
    scanf("%s", input);

    // 遍历输入字符串,统计每个数字出现的次数
    for (int i = 0; i < strlen(input); i++) {
    if (input[i] >= '0' && input[i] <= '9') {
    count[input[i] - '0']++;
    }
    }

    // 找出出现次数最多的数字
    int max_count = 0, lucky_number = 0;
    for (int i = 0; i < 10; i++) {
    if (count[i] > max_count || (count[i] == max_count && i > lucky_number)) {
    max_count = count[i];
    lucky_number = i;
    }
    }

    printf("%d\n", lucky_number);
    return 0;
    }