HDU2000 ASC码排序
HDU2000 ASC码排序

#include <iostream>
using namespace std;

char s[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
				'm', 'n', 'o', 'p','q','r', 's','t','u','v','w','x','y','z'};

int main()
{
	char a;
	char b;
	char c;
	while(cin >> a >> b >> c)
	{
		int flag = 0;
		for(int i=0; i<26; i++)
		{
			if(s[i]==a || s[i]==b || s[i]==c)
			{
				if(!flag)
				{
					cout << s[i];
					flag = 1;
				}
				else
				{
					cout <<" " << s[i];
				}

			}
		}
		cout<< endl;
	}

    return 0;
}

刚开始时的思路,但是没有注意到如果输入有相同的字符的可能性。

还是老老实实的交换吧

#include <iostream>
using namespace std;

void swap(char &a, char &b)
{
	if(a > b)
	{
		char temp = a;
		a = b;
		b = temp;
	}
}

int main()
{
	char s[3];

	while(cin >> s[0] >> s[1] >> s[2])
	{
		int flag = 0;
		if(s[0] > s[1])
		{
			swap(s[0], s[1]);
		}
		if(s[0] > s[2])
		{
			swap(s[0], s[2]);
		}
		if(s[1] > s[2])
		{
			swap(s[1], s[2]);
		}
		for(int i=0; i<3; i++)
		{
			if(!flag)
			{
				cout << s[i];
				flag = 1;
			}
			else
			{
				cout << " " << s[i];
			}

		}
		cout << endl;
	}

    return 0;
}

注意用一个flag来控制输出格式。末尾没有空格
果然还是我想复杂了

#include <iostream>
using namespace std;
int main()
{
	char a[3];
	while (cin >> a)
	{
		if (a[0] > a[1]) swap(a[0], a[1]);
		if (a[0] > a[2]) swap(a[0], a[2]);
		if (a[1] > a[2]) swap(a[1], a[2]);
        cout << a[0] << " " << a[1] << " " << a[2] << endl;
	}
	return 0;
}

直接读入数组,直接输出。使用自带的swap函数即可。

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-10-26
  • 2021-12-09
  • 2021-06-28
  • 2021-07-27
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-02-10
  • 2021-12-22
  • 2022-02-25
  • 2021-07-30
  • 2021-10-11
  • 2022-12-23
相关资源
相似解决方案