被0分割的数字

描述有一行数字 ,现在定义 0表示空格,即这行数字被分割成若干个数 要求将这些数按照从小到大顺序排列,若该行数字全为零 则表示为零,两个数字之间可能有多个0,开头和结尾可能都有0,所有的0都看成空格,数字的个数不超过100。

 
输入
输入有n组数据
每组数据都有一行数字(每个数在整形范围内)
输出
输出0或去掉多余空格的数据
样例输入
4
000
00123
12301
1230
样例输出
0
123
1 123
123

  

 

代码:

#include <iostream>
#include <string>
#include <fstream> 
#include <algorithm>

using namespace std;

int main()
{	
	ifstream in("in.txt");
	ofstream out("out.txt");
	cin.rdbuf(in.rdbuf());
	cout.rdbuf(out.rdbuf());
	
	int n;
	cin >> n;
	while(n--) {
		
		int res[1000] = {0};
		
		string strNum;
		cin >> strNum;

		int i, j = 0;
		for(i = 0; i < strNum.length(); i++)
			if(strNum[i] == '0') {
				j++;	
			} else {
				res[j] = res[j] * 10 + strNum[i] - '0';
			}


		if(i == j) {
			cout << "0" << endl;
			continue;
		}
		
		sort(res, res + j + 1);
		
		for(int i = 0; i <= j; i++) {
			if(res[i] != 0) cout << res[i] << " ";
		}
		
		cout << endl;
	}

	return 0;
}

  

原文地址:https://www.cnblogs.com/hfultrastrong/p/6430425.html