[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

虽然老说下次争取写简洁点,然而没做到嗐。

#include<stdio.h>
void count(int);
int main(){
	int t;
	scanf("%d\n",&t);
	int test[t];
	for(int i=0;i<t;i++){
		printf("test case %d:\n",i+1);
		scanf("%d ",&test[i]);
		count(test[i]); 
	}//常规操作,录入工时并调用函数
	return 0;
}
void count(int n){
	int time[n][7];
	int sum[n];
	int list[n];
	//其实是想要整两个一样的数组先
	for(int i=0;i<n;i++){
		sum[i]=0;
		list[i]=0;
		for(int k=0;k<7;k++){
			scanf("%d ",&time[i][k]);
			sum[i]+=time[i][k];
			list[i]+=time[i][k];
		}
	}
	int t=list[0];
	for(int i=0;i<n-1;i++){
		for(int k=0;k<n-1-i;k++){
			if(list[k]<list[k+1]){
			t=list[k];
			list[k]=list[k+1];
			list[k+1]=t;
            }//对list数组按时长排好序
    	}
    }
	for(int i=0;i<n;i++){
		for(int k=0;k<n;k++){
		if(sum[k]==list[i]){
		printf("Employee %d: %d\n",k,list[i]);
		sum[k]=-1;}//值变为-1相当于舍弃不用这个数了
		}//将员工工作时间与排好序的list数组对比
	}//当这个员工的工时是当前list里的时长时
}//就输出员工编号和工时,这样输出就是排好序的

如果有更好的改进方法欢迎评论告诉我,谢谢!
等我整明白了那个该死的卡牌和种树之后就接着更。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值