首页 > 代码库 > HDOJ1031数学题

HDOJ1031数学题

/*
	1031题意:
	n*m的矩阵
	把第m列的所有元素相加后按从大到小排序,若和相同的话,按列数的升序排序
	最后输出前k的列数号
	sort,qsort的应用 
*/ 

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <math.h>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <cstring>
#include <sstream>
using namespace std;

#define input freopen("input.txt","r",stdin);
#define output freopen("output.txt","w",stdout);
#define For1(i,a,b) for (i=a;i<b;i++)
#define For2(i,a,b) for (i=a;i<=b;i++)
#define Dec(i,a,b) for (i=a;i>b;i--)
#define Dec2(i,a,b) for (i=a;i>=b;i--)
#define Sca_d(x) scanf("%d",&x)
#define Sca_s(x) scanf("%s",x)
#define Sca_c(x) scanf("%c",&x)
#define Sca_f(x) scanf("%f",&x)
#define Sca_lf(x) scanf("%lf",&x)
#define Fill(x,a) memset(x,a,sizeof(x))
#define MAXN 100001

template <typename T>  
T gcd(T a,T b)  
{
    return b==0?a:gcd(b,a%b);  
}
 
template <typename T>  
T lcm(T a,T b)  
{  
    return a/gcd(a,b)*b;  
}

bool book[MAXN];

struct Node
{
	double plus;
	int pos;
}node[MAXN];

int cmp(const void *a,const void *b)
{
	struct Node *c =(Node *)a;
	struct Node *d =(Node *)b;
	if (c->plus != d->plus) return d->plus - c->plus;
	return c->pos - d->pos;
}

int main()
{
	//input;
	int n,m,k,i,j;
	double num;
	while(cin>>n>>m>>k)
	{
		Fill(book,0);
		Fill(node,0);
		For2(i,1,n)
			For2(j,1,m)
			{
				Sca_lf(num);
				node[j].plus+=num;
			}
		For2(j,1,m) node[j].pos=j;
		qsort(node+1,m,sizeof(node[0]),cmp);
		For2(i,1,k)
			book[node[i].pos]=1;
		int space_flag=0;
		Dec2(i,m,1)
			if (book[i])
				if (space_flag)
					printf(" %d",i);
				else
					printf("%d",i),space_flag=1;
		cout<<endl;
	}
	return 0;
}

HDOJ1031数学题