1081.cpp

来自「哈尔滨工业大学ACM 竞赛网上在线试题集锦的源代码」· C++ 代码 · 共 57 行

CPP
57
字号
/*  This Code is Submitted by wywcgs for Problem 1081 on 2006-01-06 at 23:41:47 */ 
#include <cstdio>
#include <cstring>
#include <cctype>
#include <cstdlib>
#include <algorithm>
using namespace std;

const int MAX = 128;

int wcmp(const void*, const void*);

int main()
{
	bool isw[MAX];
	char word[MAX][16];
	int num[MAX], i;
	
	while(true) {
		char ch = getchar();
		if(ch == '.') break;
		else ungetc(ch, stdin);
		int tn = 0, nn = 0, wn = 0;
		while(ch != '.') {
			ch = getchar();
			isw[tn] = isalpha(ch);
			ungetc(ch, stdin);
			if(isw[tn++]) scanf("%[a-zA-Z]", word[wn++]);
			else scanf("%d", &num[nn++]);
			scanf("%c\n", &ch);
		}
		sort(num, num+nn);
		qsort(word, wn, sizeof(word[0]), wcmp);
		int wh = 0, nh = 0;
		for(i = 0; i < tn; i++) {
			if(isw[i]) printf("%s", word[wh++]);
			else printf("%d", num[nh++]);
			if(i == tn-1) printf(".\n");
			else printf(", ");
		}
	}
	
	return 0;
}

int wcmp(const void* a, const void* b)
{
	char x[16], y[16];
	strcpy(x, (char*)a); strcpy(y, (char*)b);
	int i;
	for(i = 0; i < 16; i++) {
		if(isupper(x[i])) x[i] -= 'A' - 'a';
		if(isupper(y[i])) y[i] -= 'A' - 'a';
	}
	return strcmp(x, y);
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?