ASCII码排序
输入三个字符后,按各字符的ASCII码从小到大的顺序输出这三个字符。
Input
输入数据有多组,每组占一行,有三个字符组成,之间无空格。
Output
对于每组输入数据,输出一行,字符中间用一个空格分开。
Sample Input
qwe
asd
zxc
Sample Output
e q w
a d s
c x z
注意事项:字符和字符串的输入
string a和 cin>>a 搭配;
char a[n] 和scanf(“%s”,a)搭配,字符数组a在scanf时会储存一个不含空格、TAB和回车符的字符串。
如果是字符串数组char a[n][m] ,可以用scanf(“%s”,a[i])读取并储存第i个字符串。
另:scanf括号里面的逗号,后面接指针;而数组a本身就代表指针,故不用在前面加&。
代码写法1:
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
using namespace std;
int main()
{
char a[3];
//注意输入:要输入字符串数组,而且输入时数组a前面不加&!不是用循环输入数组元素,否则会错
while(scanf("%s",a)!=EOF)
{
sort(a,a+3); //sort()函数头文件为<algorithm>
for(int i=0;i<3;i++)
{
if(i!=0)
{
printf(" ");
}
printf("%c",a[i]);
}
printf("\n");
}
return 0;
}
代码写法2:
#include<iostream>
#include<cstring>
#include<math.h>
#include<algorithm>
using namespace std;
int main()
{
string a;
while(cin>>a)
{
//a.begin()是数组a的第一个元素(头指针),a.end()为尾指针的后一个
sort(a.begin(),a.end());
for(int i=0;i<a.size();i++) //a.size()为数组a的元素个数
{
if(i==a.size()-1)
cout<<a[i];
else
cout<<a[i]<<" ";
}
cout<<endl;
}
return 0;
}