HDU 水题 1859 1860 1862

5 篇文章 0 订阅

hdu 1859


#include <stdio.h>

struct Point{
	int x,y;
}min,max,tmp;

int main(void)
{
	while( scanf("%d %d",&tmp.x, &tmp.y), tmp.x!=0 || tmp.y!=0 )
	{
		min.x = tmp.x;
		min.y = tmp.y;
		max.x = tmp.x;
		max.y = tmp.y;
		while(scanf("%d %d",&tmp.x, &tmp.y), tmp.x!=0 || tmp.y!=0)
		{
			if(tmp.x < min.x)
				min.x = tmp.x;
			if(tmp.x > max.x)
				max.x = tmp.x;
				
			if(tmp.y < min.y)
				min.y = tmp.y;
			if(tmp.y > max.y)
				max.y = tmp.y;
		}
		printf("%d %d %d %d\n", min.x,min.y,max.x,max.y);
	}
	
	return 0;
}


hdu 1860

#include <stdio.h>
#include <string.h>

char charSet[128];

int main(void)
{	
	char buf1[6];
	char buf2[83];
	int len = 0;
	
	while( gets(buf1), buf1[0] != '#' )
	{
		memset(charSet, -1, sizeof(charSet));
		len = strlen(buf1);
		for(int i=0; i<len; i++)		
			charSet[ buf1[i] ] = 0;		
		
		gets(buf2);
		len = strlen(buf2);
		for(int i=0; i<len; i++)
		{
			if( charSet[ buf2[i] ] == -1 )
				continue;
			else
				charSet[ buf2[i] ]++;
		}
		
		len = strlen(buf1);
		for(int i=0; i<len; i++)
		{
			printf("%c %d\n",buf1[i], charSet[buf1[i]]);
		}
	}
	return 0;
}


HDU1862

这题虽然是水题,但是让我复习了下qsort函数的用法,本来就会对数组排个序,现在知道怎么对结构体进行二级排序了,具体参考另一篇文章:

sort和qsort函数的用法


AC代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct excel{
	int num;
	char name[10];
	int score;
}table[100000];

int cmp1(const void *a, const void *b)
{
	return ((excel *)a)->num - ((excel *)b)->num;
}

int cmp2(const void *a, const void *b)
{
	if(strcmp( ((excel *)a)->name, ((excel *)b)->name ) != 0)
	{
		return strcmp( ((excel *)a)->name, ((excel *)b)->name );
	}
	
	return ((excel *)a)->num - ((excel *)b)->num;
}

int cmp3(const void *a, const void *b)
{
	if( ((excel *)a)->score != ((excel *)b)->score )
		return ((excel *)a)->score - ((excel *)b)->score;
	
	return ((excel *)a)->num - ((excel *)b)->num;
}

int Case=0;

int main(void)
{
	int n,c;
	while(scanf("%d %d", &n, &c), n!=0 )
	{
		for(int i=0; i<n; i++)
		{
			scanf("%d %s %d", &table[i].num, &table[i].name, &table[i].score);
		}
		switch(c)
		{
			case 1:
			qsort(table, n, sizeof(table[0]), cmp1);
			break;
			case 2:
			qsort(table, n, sizeof(table[0]), cmp2);
			break;
			case 3:
			qsort(table, n, sizeof(table[0]), cmp3);
			break;
		}
		
		printf("Case %d:\n", ++Case);
		for(int i=0; i<n; i++)
		{
			printf("%06d %s %d\n", table[i].num, table[i].name, table[i].score);
		}
	}
	return 0;
}


下面这个是用sort函数写的1862,开始用了比较多的cin和cout,执行时间比之前的慢了很多很多!后面又改回去了,这里充分体会到了输入输出时用scanf和printf的好处。

在其他不变的情况下,把qsort改为sort会快些

#include <algorithm>
#include <stdio.h>
 
using namespace std;

struct excel{
	int num;
	char name[10];
	int score;
}table[100000];

bool cmp1(excel a, excel b)
{
	return a.num < b.num;
}

bool cmp2(excel a, excel b)
{
	if(strcmp( a.name, b.name ) != 0)
	{
		return strcmp( a.name, b.name ) < 0;
	}
	
	return a.num < b.num;
}

bool cmp3(excel a, excel b)
{
	if( a.score != b.score )
		return a.score < b.score;
	
	return a.num < b.num;
}

int Case=0;

int main(void)
{
	int n,c;
	while(scanf("%d %d", &n, &c), n!=0 )
	{
		for(int i=0; i<n; i++)
		{
			scanf("%d %s %d", &table[i].num, &table[i].name, &table[i].score);
		}
		switch(c)
		{
			case 1:
			sort(table, table + n, cmp1);
			break;
			case 2:
			sort(table, table + n, cmp2);
			break;
			case 3:
			sort(table, table + n, cmp3);
			break;
		}
		
		printf("Case %d:\n", ++Case);
		for(int i=0; i<n; i++)
		{
			printf("%06d %s %d\n", table[i].num, table[i].name, table[i].score);
			//cout << setw(6) << setfill('0')<< table[i].num <<" "<< table[i].name <<" "<< table[i].score<<endl;
		}
	}
	return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值