字母重排

这道还是不错的一道题目,主要是涉及的知识点就是排序模版的运用,在一个就是用内存来解决多重运用的问题;

诶呀呀 还是多做点题目好一点,主要是自己可以多学习思想,如果只是停留在看上面,你是学不会的,

其实聪明的人,大有所在,而我真不算聪明的,所以自己只能使用记呀,背呀,还有就是写来弥补了!

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


using namespace std;

char word[2000][10];

char sorted[2000][10];

int cmp_string(const void *_a, const void *_b)
{
	char *a = (char *)_a;
	char *b = (char *)_b;
	return strcmp(a, b);
}

bool cmp_char(char a, char b)
{
	return a < b;
}

int main()
{
	char s[10];
	char t[10];
	int cnt = 0;
	while (scanf("%s", word[cnt]) != EOF)
	{
		strcpy(sorted[cnt], word[cnt]);
		if (word[cnt][0] == '*')
		{
			break;
		}
		cnt++;
	}
	qsort(word, cnt, sizeof (word[0]), cmp_string);
	qsort(sorted, cnt, sizeof (sorted[0]), cmp_string);
//	cout << "word = " << word[0] << endl;
	for (int i = 0; i < cnt; i++)
	{
		sort(sorted[i], sorted[i] + strlen(sorted[i]), cmp_char);
	}
	while (scanf("%s", s) != EOF)
	{
//		cout << "s =" << s;
		int n = strlen(s);
		sort(s, s + n, cmp_char);
//		cout << "changed s = " << s << endl; 
		for (int i = 0; i < cnt; i++)
		{
			if (strcmp(sorted[i], s) == 0)
			{
				if (i == 0)
				{
					cout << word[i];
				}
				else
				{
					cout << " " << word[i];
				}
			}
		}
		cout << endl;
	}
	system("pause");
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值