求二维数组周边元素之和
#include "stdio.h"
#define M 4
#define N 5
int fun(int Matrix[][N])
{
int sum = 0;
for (int i = 0; i < M; i++)
if (i != 0 && i != M - 1)
sum += Matrix[i][0] + Matrix[i][N - 1];
else
for (int j = 0; j < N; j++)
sum += Matrix[i][j];
return sum;
}
int main()
{
int num;
int Matrix[M][N] = {0};
for (int i = 0; i < M; i++)
for (int j = 0; j < N; j++)
{
scanf("%d", &Matrix[i][j]);
}
num = fun(Matrix);
printf("%d", num);
}
学生成绩处理
#include "stdio.h"
#define N 5
int main()
{
struct stu
{
char id[10];
int math;
int c_language;
} stu[N];
int c_language_max = 0;
int sum;
printf("请输入各位同学的信息\n");
printf("学号 数学 C语言\n");
for (int i = 0; i < N; i++)
{
scanf("%s %d %d", stu[i].id, &stu[i].math, &stu[i].c_language);
if (stu[i].c_language > c_language_max)
{
c_language_max = stu[i].c_language;
sum = 1;
}
else if (stu[i].c_language == c_language_max)
{
sum++;
}
}
printf("C语言最高分:%d,人数:%d\n", c_language_max, sum);
printf("C语言分数为%d的同学相关信息如下:\n", c_language_max);
printf("学号 数学 C语言\n");
for (int i = 0; i < N; i++)
{
if (stu[i].c_language == c_language_max)
printf("%s %d %d\n", stu[i].id, stu[i].math, stu[i].c_language);
}
}
字符串排序
#include "stdio.h"
#include "string.h"
#define N 10
void sort(char strs[][21], int type)
{
char s[21];
for (int i = 0; i < N - 1; i++)
{
for (int j = N - 1; j > i; j--)
{
if (type == 1 && (strcmp(strs[j], strs[j - 1]) < 0))
{
strcpy(s, strs[j]);
strcpy(strs[j], strs[j - 1]);
strcpy(strs[j - 1], s);
}
else if (type == 0 && (strcmp(strs[j], strs[j - 1]) > 0))
{
strcpy(s, strs[j]);
strcpy(strs[j], strs[j - 1]);
strcpy(strs[j - 1], s);
}
}
}
}
int main()
{
char strs[N][21];
int type;
printf("请输入10个字符串\n");
for (int i = 0; i < N; i++)
{
scanf("%s", strs[i]);
}
printf("请输入排序方式,按字母增序:1,按字母减序:0\n");
printf("type =");
scanf("%d", &type);
sort(strs, type);
for (int i = 0; i < N; i++)
{
printf("%s\n", strs[i]);
}
}