[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

Problem Source: 程序设计I Chapter6 Arrays

//   Date:2020/4/24
//   Author:xiezhg5
#include <stdio.h>
#include <stdlib.h>
typedef struct NODE {
	int num;
	int count;
} NODE;  //定义了一个结构体变量
//使用qsort函数
int cmp(const void *p1, const void *p2) {
	NODE *d = (NODE*)p1;
	NODE *c = (NODE*)p2;
	if(c->count != d->count)
		return c->count - d->count;   //按照工作时数降序输出 
	else
		return d->num - c->num;     //若工作时数相同,则比较序号(升序) 
}
NODE employee[1000];
int main(void) {
	int test;
	scanf("%d",&test);
	for(int k=1; k <= test; k++) {
		int n;
		scanf("%d",&n);
		for(int i=0; i < n; i++) {
			//结构体的好处就显现出来 
			employee[i].num=i;   //记录标号 
			employee[i].count=0;
			for(int j=0; j < 7; j++) {
				int m;
				scanf("%d",&m);
				//求和 
				employee[i].count+=m;
			}
		}
		//使用C语言库函数qsort进行排序节省时间 
		qsort(employee,n,sizeof(NODE),cmp);
		printf("test case %d:\n",k);
		for(int i=0; i < n; i++) {
			printf("Employee %d: %d\n",employee[i].num,employee[i].count);
		}
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值