Files
BlogPosts/YueQian/Homework/1-29.md

164 lines
3.9 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
tags:
- 作业
- C语言
aliases: empty
日期: 2026/1/29
---
**1.定义一个长度为10的数组并从键盘输入10个整数对数组进行赋值并实现下列功能**
1. 判定该数组是否有序
2. 若无序则将数组中的数据进行排序
3. 找出数组中是否存在众数并输出结果
```c
#include <stdio.h>
void bubble_sort(int arr[],int lenth)
{
for(int i = 0;i < lenth;i++){
for(int j = i+1;j < lenth;j++){
if(arr[i] > arr[j]){
arr[i] ^= arr[j];
arr[j] ^= arr[i];
arr[i] ^= arr[j];
}
}
}
}
int sort_check(int arr[],int lenth)
{
int count = 0,fcount=0;
for(int i=0,j=1;i<lenth-1;i++,j++){
if(arr[i] >= arr[j])
count++;
if(arr[i] <= arr[j])
fcount++;
}
if(count == lenth-1 || fcount == lenth-1)
return 1;
else
return 0;
}
void find_mode(int arr[], int length)
{
int count[10] = {0};
int max_freq = 1;
for (int i = 0; i < length; i++) {
if (count[i] != -1) {
int freq = 1;
for (int j = i + 1; j < length; j++) {
if (arr[i] == arr[j]) {
freq++;
count[j] = -1;
}
}
count[i] = freq;
if (freq > max_freq) {
max_freq = freq;
}
}
}
printf("数组众数结果:");
int has_mode = 0;
for (int i = 0; i < length; i++) {
if (count[i] == max_freq) {
if (has_mode) printf("、");
printf("%d出现%d次", arr[i], max_freq);
has_mode = 1;
}
}
if (max_freq == 1) {
printf("所有元素均仅出现1次无有效众数\n");
} else {
printf("(最大频次:%d\n", max_freq);
}
}
int main(int argc, char const *argv[])
{
int arr[10];
int mode;
printf("输入10个数:");
for(int i=0;i<10;i++)
scanf("%d",&arr[i]);
if(sort_check(arr,10))
printf("已排序\n");
else{
printf("未排序\n");
bubble_sort(arr,10);
printf("排序后为:");
for(int i=0;i<10;i++)
printf("%d ",arr[i]);
printf("\n");
}
find_mode(arr,10);
return 0;
}
```
![700](assets/1-29/file-20260129201415992.png)
**2.定义一个二维数组存储一个3x3的矩阵实现如下功能**
1. 计算矩阵对角元素的和
2. 判断矩阵是否是对称矩阵
```c
#include <stdio.h>
#define ROW 3
#define COL 3
int calcDiagSum(int matrix[ROW][COL])
{
    int sum = 0;
    for (int i = 0; i < ROW; i++) {
        sum += matrix[i][i];
    }
    return sum;
}
int isSymmetricMatrix(int matrix[ROW][COL])
{
    for (int i = 0; i < ROW; i++) {
        for (int j = 0; j < COL; j++) {
            if (matrix[i][j] != matrix[j][i]) {
                return 0;
            }
        }
    }
    return 1;
}
int main(int argc, char const *argv[])
{
    int matrix[ROW][COL];
    printf("请输入3x3矩阵的9个整数\n");
    for (int i = 0; i < ROW; i++) {
        for (int j = 0; j < COL; j++) {
            scanf("%d", &matrix[i][j]);
        }
    }
    int diagSum = calcDiagSum(matrix);
    printf("\n矩阵主对角线元素的和为:%d\n", diagSum);
    if (isSymmetricMatrix(matrix))
        printf("该3x3矩阵是对称矩阵\n");
    else
        printf("该3x3矩阵不是对称矩阵\n");
    return 0;
}
```
![700](assets/1-29/file-20260129202925505.png)
**3.小张作为公司的HR设计了一个年会游戏来观察员工的团队协作能力、测试员工在压力下的决策能力寻找潜在的项目领导者现在有N名员工参与并围坐一圈游戏规则如下**
- 从某位员工开始顺时针报数
- 报到数字M的倍数员工被淘汰出局
- 从下一位员工继续报数重复步骤2
- 游戏继续直到只剩下K名员工获胜者
```c
```
**4.小刘作为公司的技术骨干编写了一个程序帮助他每次都能处在胜利者中并最终获得晋升资格请你使用C语言编程复现小刘的程序实现以下功能**
- 输入总人数N、报数间隔M和获胜人数K
- 计算并输出安全的座位位置最后剩下的K人
```c
```