UVA 排序与检索1

来源:互联网 发布:java无参数函数调用 编辑:程序博客网 时间:2024/06/01 09:22

UVA 340 Master-Mind Hints

题目大意:解题游戏,出题者给出一组数字,解体者来猜。如果解体者给出的这组数字中,有一个数字的数值和位置跟答案一样,那么就给一个A,依次类推,如果数值相同,但位置不同,就给一个B,A优先于B。

#include <stdio.h>int s[1005], g[1005], temp[1005];int main() {int n, i, j, flag, strong, weak, count = 1;/*freopen("D:\\in.txt", "r", stdin);*/while (scanf("%d", &n) != EOF) {if (!n) break;for (i = 0; i < n; i++)scanf("%d", &s[i]);printf("Game %d:\n", count++);flag = 0;while (1) {for (i = 0; i < n; i++)temp[i] = s[i];strong = weak = 0;for (i = 0; i < n; i++) {scanf("%d", &g[i]);if (!g[i]) flag = 1;}if (flag == 1) break;for (i = 0; i < n; i++) {if (temp[i] == g[i]) {strong++;temp[i] = g[i] = 0;}}for (i = 0; i < n; i++)for (j = 0; j < n; j++) {if (g[j] && temp[i] == g[j]) {weak++;temp[i] = g[j] = 0;break;}}printf("    (%d,%d)\n", strong, weak);}}return 0;}

UVA 10420 List of Conquests

#include <stdio.h>#include <string.h>#include <stdlib.h>int cmp(const void* a, const void* b) {return strcmp((char*)a, (char*)b);}char s[2005][80];int main() {int n, i, count;/*freopen("D:\\in.txt", "r", stdin);*/scanf("%d\n", &n);for (i = 0; i < n; i++) {scanf("%s", s[i]);gets(s[i+1]);}qsort(s, n, sizeof(s[0]), cmp);count = 1;for (i = 1; i <= n; i++) {if (0 == strcmp(s[i], s[i - 1]) && i < n)count++;else {printf("%s %d\n", s[i - 1], count);count = 1;}}return 0;}

UVA 10474 Where is the Marble?

#include <stdio.h>#include <string.h>#define MAX 10005int num[MAX], local[MAX];int main() {int i, n, q, in, max, count = 1;/*freopen("D:\\in.txt", "r", stdin);*/while (scanf("%d %d\n", &n, &q) != EOF) {if (n == 0 && q == 0) break;memset(num, 0, sizeof(num));memset(local, 0, sizeof(local));max = 0;printf("CASE# %d:\n", count++);for (i = 0; i < n; i++) {scanf("%d\n", &in);if (max < in)max = in;num[in]++;}for (i = 1; i <= max; i++)local[i] = local[i - 1] + num[i];for (i = 1; i <= q; i++) {scanf("%d\n", &in);if (num[in]) printf("%d found at %d\n", in, local[in - 1] + 1);else printf("%d not found\n", in);}}return 0;}

UVA 152 Tree's a Crowd

#include <stdio.h>#include <string.h>#define MAX 10005int num[MAX], local[MAX];int main() {int i, n, q, in, max, count = 1;/*freopen("D:\\in.txt", "r", stdin);*/while (scanf("%d %d\n", &n, &q) != EOF) {if (n == 0 && q == 0) break;memset(num, 0, sizeof(num));memset(local, 0, sizeof(local));max = 0;printf("CASE# %d:\n", count++);for (i = 0; i < n; i++) {scanf("%d\n", &in);if (max < in)max = in;num[in]++;}for (i = 1; i <= max; i++)local[i] = local[i - 1] + num[i];for (i = 1; i <= q; i++) {scanf("%d\n", &in);if (num[in]) printf("%d found at %d\n", in, local[in - 1] + 1);else printf("%d not found\n", in);}}return 0;}

UVA 120 Stacks of Flapjacks

#include <stdio.h>int x[105], y[105], z[210];int GetMax(int x[], int cur){int i, max;max = 0;for (i = 1; i <= cur; i++) {if (x[i] > x[max])max = i;}return max;}int main() {int step, num = 1, i, cur, max;/*freopen("D:\\in.txt", "r", stdin);*/while (scanf("%d", &x[num]) != EOF) {while (getchar() != '\n') {num++;scanf("%d", &x[num]);}for (i = 1; i <= num; i++)printf("%d ", x[i]);printf("\n");cur = num;step = 0;while (cur > 0) {max = GetMax(x, cur);if (max < cur) {if (max != 1) {step++;z[step] = num + 1 - max;}for (i = 1; i <= max; i++)y[max + 1 - i] = x[i];for (i = 1; i <= max; i++)x[i] = y[i];for (i = 1; i <= cur; i++)y[cur + 1 - i] = x[i];for (i = 1; i <= cur; i++)x[i] = y[i];step++;z[step] = num + 1 - cur;}cur--;}for (i = 1; i <= step; i++)printf("%d ", z[i]);printf("0\n");num = 1;}return 0;}






0 0
原创粉丝点击