排序

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 48472 Accepted Submission(s): 13987

Problem Description
输入一行数字,如果我们把这行数字中的‘5’都看成空格,那么就得到一行用空格分割的若干非负整数(可能有些整数以‘0’开头,这些头部的‘0’应该被忽略掉,除非这个整数就是由若干个‘0’组成的,这时这个整数就是0)。

你的任务是:对这些分割得到的整数,依从小到大的顺序排序输出。

Input
输入包含多组测试用例,每组输入数据只有一行数字(数字之间没有空格),这行数字的长度不大于1000。

输入数据保证:分割得到的非负整数不会大于100000000;输入数据不可能全由‘5’组成。

Output
对于每个测试用例,输出分割得到的整数排序的结果,相邻的两个整数之间用一个空格分开,每组输出占一行。

Sample Input
0051231232050775

Sample Output
0 77 12312320

Source
POJ

题解:使用atoi()函数和strtok()函数搞定。。。。
strtok函数解析:
Syntax:
#include < string.h>
char *strtok( char *str1, const char *str2 );

The strtok() function returns a pointer to the next “token” in str1, where str2 contains the delimiters that determine the token. strtok() returns NULL if no token is found. In order to convert a string to tokens, the first call to strtok() should have str1 point to the string to be tokenized. All calls after this should have str1 be NULL.

For example:

char str[] = “now # is the time for all # good men to come to the # aid of their country”;
char delims[] = “#”;
char *result = NULL;
result = strtok( str, delims );
while( result != NULL ) {
printf( “result is \”%s\”\n”, result );
result = strtok( NULL, delims );
}

The above code will display the following output:

result is “now ”
result is ” is the time for all ”
result is ” good men to come to the ”
result is ” aid of their country”

AC代码:

#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
char s[1005];
int a[1005];
char * temp;
int cmp(const void *a,const void *b)
{
return *(int *)a-*(int *)b;
}
int main()
{
int c;
while(gets(s))
{
a[0]=atoi(strtok(s,"5"));
c=1;
while(temp=strtok(NULL,"5"))
{
a[c++]=atoi(temp);
}
qsort(a,c,sizeof a[0],cmp);
printf("%d",a[0]);
for(int i=1;i<c;i++)
{
printf(" %d",a[i]);
}
printf("\n");
}
return 0;
}

orAC2代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<cstdlib>
using namespace std;
const int maxx = 1001;
char a[maxx][9]; //字符串
long b[maxx]; //数字

int cmp(const void *a,const void *b){
return *(int*)a-*(int*)b;
}
int main()
{
string s;
while(cin>>s){
int i=0,j=0,k=0;
for(i=0,j=0,k=0;s[i]!='\0';i++,k++){
a[j][k]=s[i];

if(s[i]=='5'){
a[j][k]='\0';
if(strlen(a[j])==0){
k=-1;continue;
}
b[j]=atoi(a[j]); //将字符串转换成数字
j++;k=-1;
}
}

a[j][k]='\0';
b[j]=atoi(a[j]);
if(s[i-1]=='5')
j--;

qsort(b,j+1,sizeof(long),cmp);

for(i=0;i<=j;i++)
{
cout<<b[i];
if(i<j)
cout<<" ";
}
cout<<endl;
}
return 0;
}