HDoj 1031 Design T-shirt(题目很绕的水题)

这篇博客主要探讨了HDOJ 1031题目的复杂要求,作者在理解题目上花费了较长时间。为熟悉快速排序的运用,作者在解决此问题时应用了两次快速排序算法。博客内容围绕算法思路和实现代码展开。
摘要由CSDN通过智能技术生成

思路:题目理解了很久。。。。(调用了两个快排,只是为了熟悉下快排怎么用)

代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
void swp(int *a,int *b)
{
	int temp;
	temp=*a;
	*a=*b;
	*b=temp;
	return;
	
}
void quick_sort(int *a,int low,int high)
{
	int i;
	int j;
	int base;
	i=low;
	j=high;
	
	if(low>high)
		return;
	base=a[low];
	while(i<j)
	{
		while(i<j)
		{
			if(a[j]<base)
				break;
			j--;
		}
			
		while(i<j)
		{
			if(a[i]>base)
				break;
			i++;
		}
		if(i<j)
			swp(&a[i],&a[j]);
		if(i==j)
			swp(&a[low],&a[i]);
		
		
	}
	
	quick_sort(a,low,i-1);
	quick_sort(a,j+1,high);
	return;
	
}

void sort(float *A,int left,int right)
{
    if(left>=right) return;
    float x=A[(left+right)>>1];
    int low=left;
	int high=right;
    while(low<high)
    {
        while(A[low]>x)
            low++;
        while(A[high]<x)
            high--;
        if(low<=high)
        {
            float Temp=A[low];
            A[low]=A[high];
            A[high]=Temp;
            low++;
            high--;
        }
    }
    sort(A,left,high);
    sort(A,low,right);
}

int main(int argc, char *argv[]) {
    float a[500][500];
	int n,m,k;
	int i,j;
	float b[500];
	float c[500]; 
	int x;
	int end[500];
	while(scanf("%d%d%d",&n,&m,&k)!=EOF)
	{
		for(i=1;i<=n;i++)
			for(j=1;j<=m;j++)
				a[i][j]=0.0;
		memset(b,0.0,sizeof(b));
		memset(b,0.0,sizeof(c));
		x=0;
		for(i=1;i<=n;i++)
		{
			for(j=1;j<=m;j++)
			{
				scanf("%f",&a[i][j]);
			}
			
			
		}
		for(j=1;j<=m;j++)
		{
			for(i=1;i<=n;i++)
			{
				b[j]+=a[i][j];
			}
		}
		for(j=1;j<=m;j++)
			c[j]=b[j];
		sort(b,1,m);
		for(i=1;i<=k;i++)
		{
			for(j=1;j<=m;j++)
			{
				if(b[i]==c[j])
				{
					end[x]=j;
					x++;
					c[j]=-1;
					break;
				}	
			
				
			}
		}
		quick_sort(end,0,x-1);
		for(i=x-1;i>=0;i--)
		{
			if(i!=0)
				printf("%d ",end[i]);
			else
				printf("%d",end[i]);
		}
		
		printf("\n");
	
		
		
	}
	 
    

    
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值