java做法:
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner (System.in);
while(sc.hasNext()){
String ss = sc.next();
String [] str = ss.split("5"); //放入字符串数组
int []arr=new int[str.length];
int j=0;
for(int i=0; i < str.length; i++){
if(!str[i].equals(""))//注意过滤空串(由于分割相连的5造成的)!!!
arr[j++] = Integer.valueOf(str[i]);
}
Arrays.sort(arr,0,j);
for(int i=0 ; i< j; i++){
System.out.print(i!=0?" "+arr[i]:arr[i]);//小心PE!!!
}
System.out.println();
}
}
}
import java.util.*;
import java.io.*;
public class HDU1106
{
public static void main(String[] args)
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s;
while((s=br.readLine())!=null)
{
//去掉字符串中空格,防止useDelimiter连续读取整数出错
Scanner sc = new Scanner(s.trim());
int k = 0;
int a[];
a=new int[1001];
//匹配正则表达式,5+表示匹配一个或多个5,不会产生空字符
sc.useDelimiter("5+");
while(sc.hasNextInt())
a[k++]=sc.nextInt();
sc.close();
Arrays.sort(a,0,k);
for(int i=0;i<k;i++)
{
if(i==k-1)
System.out.println(a[i]);
else
System.out.print(a[i]+" ");
}
}
}
}
while(sc.hasNext()) {
if(sc.hasNextInt()) {
//do sth
}else {
//直接跳过
next();
}
}
C/C++代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<stdlib.h>
using namespace std;
char a[1006],ok[1006];
int t[1006];
int main (){
int i,j,str;
while(scanf("%s",a)!=EOF){//gets(a)!=NULL都可以
str=strlen(a);
int end=0,flag=0,n=0,k=0,sum=0;
for(i=0;i<str;){
if(a[i]!='5'){//收集不为5的连续的数字字符
ok[end++]=a[i++];
flag=1;
if(i!=str) continue;//注意!!!不写判断语句就会漏掉最后的5的后面的非5的数
}
if(flag){
ok[end]='\0';
t[n++]=atoi(ok);
end=0;
flag=0;
}
if(a[i]=='5'&&a[++i]!='5')//跳过5
continue;
}
sort(t,t+n );//排序
for(i=0;i<n-1;i++)
printf("%d ",t[i]);
printf("%d\n",t[n-1]);
}
return 0;
}