HDU 1004 數論水題

寫這篇文章就是爲了長記性。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Node
{
	char color[16];
	int time;
}T[101];
int cmp(const void *_a, const void *_b)
{
	struct Node *a = (struct Node*)_a;
	struct Node *b = (struct Node*)_b;
	//如果寫成下式準錯, 不能用大於,小於號。必須寫成-號
	//return b->time > a->time; 
	return b->time - a->time;
	//這樣寫也對
	//return ((struct Node*)_b)->time - ((struct Node*)_a)->time;
}
int main()
{
	int n, num, i, j, ok;
	char str[16];
	while (scanf("%d", &n) && n )
	{
		memset(T, 0, sizeof(T));
		scanf("%s", &str);
		strcpy(T[0].color, str);
		num = T[0].time = 1;
		for (i = 1; i < n; i++)
		{
			ok = 0;
			scanf("%s", &str);
			for (j = 0; j < num; j++)
				if (strcmp(T[j].color, str) == 0)
				{
					T[j].time++;
					ok = 1;
					break;
				}
			if (!ok)
			{
				strcpy(T[num].color, str);
				T[num++].time = 1;
			}
		}
		qsort(T, num, sizeof(struct Node), cmp);
		printf("%s\n", T[0].color);
	}
	return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章