从键盘中输入正整数n,求表达式(1^2)/(1+1)+(2^2)/(2+1)+(3^2)/(3+1)+……+(n^2)/(n+1)的值。(此处i^2代表i的平方,i从1~n)
#include <stdio.h>
#include <stdlib.h>
struct student {
int num;
char name[20];
float score[4];
};
void input(struct student a[], int);
void aver(struct student a[], int,struct student b[]);
void order(struct student a[], int);
void output(struct student a[], int,struct student b[]);
int main() {
struct student stu[100];
struct student b[100];
int n;
printf("请输入学生个数:");
scanf("%d", &n);
input(stu, n);
aver(stu, n,b);
order(stu, n);
output(stu, n,b);
return 0;
}
void input(struct student a[], int n) {
int i, j;
// printf("请输入学生的学号,姓名,英语,数学,物理,语文成绩:\n");
for (i = 0; i < n; i++) {
printf("请输入第%d个学生的学号,姓名,英语,数学,物理,语文成绩:\n", i + 1);
scanf("%d", &a[i].num);
//gets(a[i].name);
scanf("%s", &a[i].name);
for (j = 0; j < 4; j++)
scanf("%f", &a[i].score[j]);
}
}
void aver(struct student a[], int n,struct student b[]) {
int i=0, j;
for (i = 0; i < n; i++) {
b[i].score[0] = 0;
for (j = 0; j < 4; j++)
b[i].score[0] += a[i].score[j];
b[i].score[0] = b[i].score[0] / 4;
}
}
void order(struct student a[], int n) {
int i, j;
struct student t[1];
for (i = 1; i < n; i++)
for (j = 0; j < n - i; j++)
if (a[j].score[3] < a[j + 1].score[3]) {
t[1] = a[j];
a[j] = a[j + 1];
a[j + 1] = t[1];
}
}
void output(struct student a[], int n,struct student b[]) {
int i, j;
printf("学号\t姓名\t 英语\t 数学\t 物理\t 语文\t 平均成绩如下:\n");
for (i = 0; i < n; i++) {
printf("%d\t%s\t", a[i].num, a[i].name);
for (j = 0; j < 4; j++)
printf("%.2f\t", a[i].score[j]);
printf("%.2f", b[i].score[0]);
printf("\n");
}
}
帮你改了一些,看看测试: