[Arrays]D. Liang 6.20 Computing the weekly hours for each employee
Description:
Suppose the weekly hours for all employee are stored in a two-dimensional array.
Each row records an employee’s seven-day work hous with seven columns.
For example, the following array stores the work hours for eight employees.
Write a program that displays employees and their total hours in decreasing
order of the total hours.
Su M T W H F Sa
Employee 0 2 4 3 4 5 8 8
Employee 1 7 3 4 3 3 4 4
Employee 2 3 3 4 3 3 2 2
Employee 3 9 3 4 7 3 4 1
Employee 4 3 5 4 3 6 3 8
Employee 5 3 4 4 6 3 4 4
Employee 6 3 7 4 8 3 8 4
Employee 7 6 3 5 9 2 7 9
Input:
The first line is a positive integer t for the number of test cases.
Each test case contains m+1 lines. The line 1 contains an integer m (0<m<=100). Then followed m lines, each line contains 7 integers seperated by blanks, for one employee’s seven-day work hours.
Output:
For each test case,output each employee’s number and their total hours in decreasing order of the totoal hours using the format like the sample output.
Sample Input:
2
8
2 4 3 4 5 8 8
7 3 4 3 3 4 4
3 3 4 3 3 2 2
9 3 4 7 3 4 1
3 5 4 3 6 3 8
3 4 4 6 3 4 4
3 7 4 8 3 8 4
6 3 5 9 2 7 9
3
2 4 3 4 5 8 8
7 3 4 3 3 4 4
3 3 4 3 3 2 2
Sample Output:
test case 1:
Employee 7: 41
Employee 6: 37
Employee 0: 34
Employee 4: 32
Employee 3: 31
Employee 1: 28
Employee 5: 28
Employee 2: 20
test case 2:
Employee 0: 34
Employee 1: 28
Employee 2: 20
Programme:
//Date:2020/4/26
//Author:Kamenrider Justice
//参考了Master Chiken的做法
#include<stdio.h>
#include<stdlib.h>
typedef struct Emp//结构体
{
int num;//编号
int hours;//七天的工作时间
}Emp;
int sort(const void* p1,const void* p2)//作为qsort函数的compar函数
{
Emp *a=(Emp*)p1;
Emp *b=(Emp*)p2;
if(a->hours!=b->hours)
{
return b->hours - a->hours;//按工作时间排序
}
else return a->num - b->num;//按编号排序
}
int main()
{
int m,n,i,j,k,sum;
int workTime[100][7];
scanf("%d",&m);
struct Emp worker[100];
for(i=0;i<m;i++)
{
scanf("%d",&n);//工人人数
for(j=0;j<n;j++)
{
for(k=0;k<7;k++)
{
scanf("%d",&workTime[j][k]);//输入工作时间
}
}
for(j=0;j<n;j++)//将总工作时间和编号输入到结构体中
{
sum=0;
for(k=0;k<7;k++)
{
sum+=workTime[j][k];
}
worker[j].num=j;
worker[j].hours=sum;
}
qsort(worker,n,sizeof(Emp),sort);//调用qsort函数
printf("test case %d:\n",i+1);
for(j=0;j<n;j++)
{
printf("Employee %d: %d\n",worker[j].num,worker[j].hours);
}
}
return 0;
}
Django 入门到实战:打造热门博客系统