UVA 排序与检索2

UVA 156 Ananagrams

#include 
#include 
#include 

char str[100][25], s[100][25];

int str_cmp(const void* a, const void* b) {
	return strcmp((char*)a, (char*)b);
}

int char_cmp(const void* a, const void* b) {
	return *(char*)a - *(char*)b;
}

int main() {
	char in[100];
	int count, i, j;
	/*freopen("D:\\in.txt", "r", stdin);*/
	for (count = 0; scanf("%s", str[count]) != EOF; count++)
	if (str[count][0] == '#') break;
	qsort(str, count, sizeof(str[0]), str_cmp);
	for (i = 0; i < count; i++) {
		int l = strlen(str[i]);
		for (j = 0; j < l; j++) {
			if (str[i][j] >= 'A' && str[i][j] <= 'Z')
				s[i][j] = str[i][j] - 'A' + 'a';
			else s[i][j] = str[i][j];
		}
		qsort(s[i], l, sizeof(char), char_cmp);
	}
	for (i = 0; i < count; i++) {
		int flag = 0;
		for (j = 0; j < count; j++) {
			if (strcmp(s[i], s[j]) == 0)
				flag++;
		}
		if (flag == 1)
			printf("%s\n", str[i]);
	}
	return 0;
}

UVA 123 Searching Quickly

#include   
#include 
#include 

struct ss{
	int x, y;
	char word[20][50]; //单词
	char key[50];      //关键字
}stmp[250], s[250];

char ig[55][15];        // 被忽略的词  
int ign = 0, sn = 0;    // 被忽略词数量和title数量  

// 将字符串大写转小写  
void UTL(char str[]) {
	int len = strlen(str);

	for (int i = 0; i < len; i++)
	if (str[i] >= 'A' && str[i] <= 'Z') {
		str[i] += ('a' - 'A');

	}
	
}
// 将字符串小写转大写  
void LTU(char str[]) {
	int len = strlen(str);

	for (int i = 0; i < len; i++)
	if (str[i] >= 'a' && str[i] <= 'z') {
		str[i] += ('A' - 'a');

	}

}

		// 判断是否为被忽略词  
bool judge(char str[]) {
	for (int i = 0; i < ign; i++) {
		if (0 == strcmp(str, ig[i]))
			return false;

	}
	return true;

}

	// 快排比较函数,如果两个字符串相等,就比较它们的优先级  
int cmp(const void *_a, const void *_b) {
	struct ss *a = (struct ss*)_a;
	struct ss *b = (struct ss*)_b;
	if (strcmp(a->key, b->key) != 0)
		return strcmp(a->key, b->key);
	return a->x - b->x;

}

int main() {

	// 下面两个while 用于输入操作  
	while (scanf("%s", ig[ign])) {
		if (':' == ig[ign][0])
			break;
		ign++;

	}
	getchar();

	char str[50];
	while (scanf("%s", str) != EOF) {
		char ch = getchar();

		// 先将每个单词转为小写  
		UTL(str);

		strcpy(stmp[sn].word[stmp[sn].y], str);
		stmp[sn].y++;           // 记录每句话单词的个数  

		if ('\n' == ch) {
			stmp[sn].x = sn;    // 记录每句话的优先级  
			sn++;

		}

	}

	// 下面对每句话的每个单词进行处理  
	int num = 0;
	for (int i = 0; i < sn; i++) {
		for (int j = 0; j < stmp[i].y; j++) {   // 现在对每个单词进行处理  
			if (judge(stmp[i].word[j])) {
				s[num] = stmp[i];
				LTU(s[num].word[j]);
				strcpy(s[num].key, s[num].word[j]);
				num++;

			}

		}

	}

	// 排序并输出  
	qsort(s, num, sizeof (s[0]), cmp);
	for (int i = 0; i < num; i++) {
		int j;
		for (j = 0; j < s[i].y - 1; j++)
			printf("%s ", s[i].word[j]);
		printf("%s\n", s[i].word[j]);

	}

	return 0;

}

 
  

UVA 10194 Football (aka Soccer)

#include 
#include 
#include 

struct Team {
	char name[50];
	int b, c, d, e, f, g, h, i;
}team[50];

int n, t, g, i;

int find(char str[]) {
	for (i = 0; i < t; i++)
	if (strcmp(team[i].name, str) == 0)
		return i;
	return 0;
}

void UTL(char str[]) {
	int l = strlen(str);
	for (i = 0; i < l; i++)
	if (str[i] >= 'A' && str[i] <= 'Z')
		str[i] += 'a' - 'A';
}

int cmp(const void* _a, const void* _b) {
	struct Team *a = (struct Team*)_a;
	struct Team *b = (struct Team*)_b;
	if (a->b != b->b)
		return b->b - a->b;
	if (a->d != b->d)
		return b->d - a->d;
	if (a->g != b->g)
		return b->g - a->g;
	if (a->h != b->h)
		return b->h - a->h;
	if (a->c != b->c)
		return a->c - b->c;
	char ta[50], tb[50];
	strcpy(ta, a->name);
	strcpy(tb, b->name);
	UTL(ta); UTL(tb);
	return strcmp(ta, tb);
}

int main() {
	char tName[105];
	/*freopen("D:\\in.txt", "r", stdin); */
		scanf("%d", &n);
	getchar();
	while (n--) {
		gets(tName);
		memset(team, 0, sizeof(team));
		scanf("%d", &t);
		getchar();
		for (i = 0; i < t; i++)
			gets(team[i].name);
		scanf("%d", &g);
		getchar();
		while (g--) {
			char ch, a[50], b[50];
			int x = 0, w, l;
			while (ch = getchar())  {
				if (ch == '#')
					break;
				a[x] = ch;
				x++;
			}
			a[x] = '\0';
			scanf("%d", &w);
			getchar();
			scanf("%d", &l);
			getchar();
			gets(b);

			int aa, bb;
			aa = find(a);
			bb = find(b);

			team[aa].c += 1;
			team[bb].c += 1;

			team[aa].h += w;
			team[aa].i += l;
			team[bb].h += l;
			team[bb].i += w;

			team[aa].g = team[aa].h - team[aa].i;
			team[bb].g = team[bb].h - team[bb].i;

			if (w > l) {
				team[aa].b += 3;
				team[aa].d += 1;
				team[bb].f += 1;
			}
			else if (w == l) {
				team[aa].b += 1;
				team[bb].b += 1;
				team[aa].e += 1;
				team[bb].e += 1;
			}
			else {
				team[bb].b += 3;
				team[bb].d += 1;
				team[aa].f += 1;
			}
		}
		qsort(team, t, sizeof(team[0]), cmp);
		printf("%s\n", tName);
		for (i = 0; i < t; i++) {
			printf("%d) %s %dp, %dg (%d-%d-%d), %dgd (%d-%d)\n",
				i + 1, team[i].name, team[i].b, team[i].c, team[i].d,
				team[i].e, team[i].f, team[i].g, team[i].h, team[i].i);
		}
		if (n > 0) printf("\n");
	}
	return 0;
}

UVA 755 487--3279

#include 
#include 
#include 

int s[100005];
int num[] = { 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 0, 7, 7, 8, 8, 8, 9, 9, 9, 0};

int cmp(const void *a, const void *b) {
	return *(int*)a - *(int*)b;
}

int main() {
	int n, t, i, j, sum, flag, l, count, cur;
	char temp[100];
	/*freopen("D:\\in.txt", "r", stdin);*/
	scanf("%d", &t);
	while (t--) {
		scanf("%d", &n);
		for (i = 0; i < n; i++) {
			memset(temp, 0, sizeof(temp));
			scanf("%s", temp);
			l = strlen(temp);
			sum = 0;
			for (j = 0; j < l; j++) {
				if (temp[j] == '\0') break;
				if (temp[j] >= 'A' && temp[j] <= 'Z')
					sum = sum * 10 + num[temp[j] - 'A'];
				else if (temp[j] >= '0' && temp[j] <= '9')
					sum = sum * 10 + temp[j] - '0';
			}
			s[i] = sum;
		}

		qsort(s, n, sizeof(int), cmp); 

		count = flag = cur = 0;

		for (i = 0; i < n - 1; i++) {
		    if (s[i] == s[i + 1])
		/*for (i = 0; i < n; i++) {                          
		if (s[i] == s[i + 1] && i + 1 < n)  */  			   
				count++;
			else {
				if (count) {
					printf("%03d-%04d %d\n", s[i] / 10000, s[i] % 10000, count + 1);
					flag = 1;
					count = 0;
				}
			}
		}
		if (i == (n - 1) && count > 0) {
			printf("%03d-%04d %d\n", s[i] / 10000, s[i] % 10000, count + 1);
			flag = 1;
		}
		if (!flag) printf("No duplicates.\n");
		if (t) printf("\n");
	}
	return 0;
}

UVA 10785 The Mad Numerologist

#include
#include

char s1[6] = "AUEOI", s2[22] = "JSBKTCLDMVNWFXGPYHQZR", s[250][250], ch;

int cmp(int k, int n, int m)
{
	int i;
	if (((m - n) % 2 == 0) && (s[k][n] > s[k][m])) 
		return 1;
	return 0;
};

void main()
{
	int use1[6], use2[22], a[6], b[21], i, j, k, l, n, pos1, pos2;
	int Case, count = 1;
	/*freopen("D:\\in.txt", "r", stdin);*/
	scanf("%d", &Case);
	i = 1;
	while (Case--) {
		scanf("%d", &k);
	for (i = 0; i < 5; i++)
		a[i] = 21;
	for (i = 0; i < 21; i++)
		b[i] = 5;

		pos1 = 0; pos2 = 0;
		for (i = 0; i < 5; i++)
			use1[i] = a[i];
		for (i = 0; i < 21; i++)
			use2[i] = b[i];
		for (i = 1; i <= k; i++)
		{
			if (i % 2 == 1) {
				while ((pos1 < 5) && (use1[pos1] == 0))
					++pos1; 
				--use1[pos1]; 
				s[k][i - 1] = s1[pos1];
			}
			else  {
				while ((pos2 < 21) && (use2[pos2] == 0))
					++pos2; 
				--use2[pos2];
				s[k][i - 1] = s2[pos2];
			}
		}
		s[k][k] = '\0';
		for (i = 1; i < k - 1; i++)
		for (j = i + 1; j < k; j++)
		if (cmp(k, i, j)) { 
			ch = s[k][i]; 
			s[k][i] = s[k][j]; 
			s[k][j] = ch; 
		}
		printf("Case %d: %s\n", count++, s[k]);
	}
}

UVA 400 Unix ls

#include 
#include 
#include 

char str[105][65];

int cmp(const void *a, const void *b) {
	return strcmp((char*)a, (char*)b);
}

void align(char s[], int len) {
	int l = len - strlen(s);
	printf("%s", s);
	while (l--)
		printf(" ");
}

int main() {
	int n, row, column, l, max, i, j;
/*	freopen("D:\\in.txt", "r", stdin);*/
	while (scanf("%d", &n) != EOF) {
		max = column = row = 0;
		for (i = 0; i < n; i++) {
			scanf("%s", str[i]);
			l = strlen(str[i]);
			if (l > max)
				max = l;
		}
		qsort(str, n, sizeof(str[0]), cmp);
		column = 62 / (max + 2);
		row = n / column;
		if (n % column != 0) row++;

		printf("------------------------------------------------------------\n");
		for (i = 0; i < row; i++) {
			for (j = 0; j < column; j++) {
				int temp = row * j + i;
				if (temp >= n) continue;
				if (temp >= ((int)((n - 1) / row)) * row)
					align(str[temp], max);
				else align(str[temp], max + 2);
			}
			printf("\n");
		}
	}
	return 0;
}



你可能感兴趣的:(UVA)