输入 10 个学生 5 门课的成绩,分别用函数实现下列功能:
1.计算每个学生平均分;
2.计算每门课的平均分;
3.找出所有 50 个分数中最高的分数所对应的学生和课程
#define _CRT_SECURE_NO_WARNINGS 1
#include
int row = 0;
int col = 0;
void ave_stu(float arr[][6]) //arr[][6]是算学生平均成绩的
{
int i = 0;
int j = 0;
float sum = 0.0;
for (i = 0; i < 10; i++)
{
sum = 0.0;
for (j = 0; j < 5; j++)
{
sum += arr[i][j];
arr[i][5] = sum / 5;
}
}
}
void ave_cor(float arr[][6])
{
int i = 0;
int j = 0;
float sum = 0.0;
for (j = 0; j < 5; j++)
{
sum = 0.0;
for (i = 0; i < 10; i++)
{
sum += arr[i][j];
}
arr[10][j] = sum / 10;
}
}
float find_high(float arr[][6])
{
int i = 0;
int j = 0;
float max = 0.0;
for (i = 0; i < 10; i++)
{
for (j = 0; j < 5; j++)
{
if (max < arr[i][j])
{
max = arr[i][j];
row = i + 1;
col = j + 1;
}
}
}
return max;
}
int main()
{
float arr[11][6] = { {87,88,92,67,78},{88,86,87,98,90},{76,75,65,65,78},
{67,87,60,90,67},{77,78,85,64,56},{76,89,94,65,76},{78,75,64,67,77},
{77,76,56,87,85},{84,67,78,76,89},{86,75,64,69,90} };
int i = 0;
int j = 0;
ave_stu(arr);
ave_cor(arr);
float max = find_high(arr);
printf("每个学生的平均成绩是:");
for (i = 0; i < 10; i++)
{
printf("%.2f ", arr[i][5]);
}
printf("\n每个课程的平均成绩是:");
for (j = 0; j < 5; j++)
{
printf("%.2f ", arr[10][j]);
}
printf("\n最高成绩是:%.2f,学生号是:%d,课程号是:%d", max, row, col);
return 0;
}
写几个函数:
①输人 10 个职工的姓名和职工号
②按职工号由小到大顺序排序,姓名顺序也随之调整
③要求输入一个职工号,用折半查找法找出该职工的姓名,从主函数输人要查找的职工号,输出该职工姓名
本体代码实现是官方教材课后习题标准答案,非本人写
#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
#define N 10
int main()
{
void input(int[], char name[][8]);
void sort(int[], char name[][8]);
void search(int, int[], char name[][8]);
int num[N], number, flag = 1, c;
char name[N][8];
input(num, name);
sort(num, name);
while (flag == 1)
{
printf("\ninput number to look for:");
scanf("%d", &number);
search(number, num, name);
printf("continue or not(Y/N)?");
getchar();
c = getchar();
if (c == 'N' || c == 'n')
{
flag = 0;
}
}
return 0;
}
void input(int num[], char name[N][8])
{
int i;
for (i = 0; i < N; i++)
{
printf("input No.:");
scanf("%d", &num[i]);
printf("input name: ");
getchar();
gets(name[i]);
}
}
void sort(int num[], char name[N][8])
{
int i, j, min, templ;
char temp2[8];
for (i = 0; i < N - 1; i++)
{
min = i;
for (j = i; j < N; j++)
if (num[min] > num[j])
min = j;
templ = num[i];
strcpy(temp2, name[i]);
num[i] = num[min];
strcpy(name[i], name[min]);
num[min] = templ;
strcpy(name[min], temp2);
}
printf("\n result: \n");
for (i = 0; i < N; i++)
{
printf("\n %5d%10s", num[i], name[i]);
}
}
void search(int n, int num[], char name[N][8])//折半查找的函数
{
int top, bott, mid, loca, sign;
top = 0;
bott = N - 1;
loca = 0;
sign = 1;
if ((n < num[0]) || (n > num[N - 1]))
loca = -1;
while ((sign == 1) && (top <= bott))
{
mid = (bott + top) / 2;
if (n == num[mid])
{
loca = mid;
printf("NO.%d,his name is %s.\n", n, name[loca]);
sign = -1;
}
else if (n < num[mid])
bott = mid - 1;
else
top = mid + 1;
}
if (sign == 1 || loca == -1)
printf("%d not been found.\n", n);
}
输入 4 个整数,找出其中最大的数。用函数的递归调用来处理
#define _CRT_SECURE_NO_WARNINGS 1
#include
int Max(int x, int y)
{
if (x > y)
return x;
else
return y;
}
int findmax(int a, int b, int c, int d)
{
int max = Max(Max(Max(a, b),c),d);
return max;
}
int main()
{
int a = 0;
int b = 0;
int c = 0;
int d = 0;
scanf("%d %d %d %d", &a, &b, &c, &d);
int max = findmax(a, b, c, d);
printf("%d", max);
return 0;
}
用递归法将一个整数转换成字符串。例如,输入 483,应输出字符串"483",n的位数不确定,可以是任意位数的整数。
画图理解:
#define _CRT_SECURE_NO_WARNINGS 1
#include
void change(int n)
{
if (n / 10 != 0)
{
change(n / 10);
}
printf("%c", n % 10 + '0');
}
int main()
{
int num = 0;
scanf("%d", &num);
change(num);
return 0;
}
给出年、月、日,计算该日是该年的第 n 天
#define _CRT_SECURE_NO_WARNINGS 1
#include
int is_leap_year(int year)
{
if ((year % 4 == 0 && year / 100 != 0) || (year % 4 == 0))
return 1;
return 0;
}
int cal_year_month(int year, int month)
{
int arr[13] = { 29,31,28,31,30,31,30,31,31,30,31,30,31 };
if (month == 2 && is_leap_year(year))
return arr[0];
return arr[month];
}
int cal_year_month_day(int year, int month, int day)
{
int i = 0;
int days = 0;
for (i = 1; i < month; i++)
{
days += cal_year_month(year, i);
}
days += day;
return days;
}
int main()
{
int year = 0;
int month = 0;
int day = 0;
scanf("%d %d %d", &year, &month, &day);
int days = cal_year_month_day(year, month, day);
printf("%d年%d月%d日是该年的第%d天", year, month, day, days);
return 0;
}