G.版本号控制

版权声明:晓程原创 https://blog.csdn.net/qq_43469554/article/details/88124282

小蒜头写了一个 APP,存在很多 BUG,不得不一直修复。每次提交更改后,Git 会对当前的版本生产一个版本号,形如 X1,X2,X3⋯Xn 。其中 Xi为不超过 9 位的可能包含前导零的非负数,以英文句号’.'隔开,长度不确定。较早时间的版本号一定小于较晚时间的版本号。比较两个版本号的大小,先比较 X1,如果相等,在比较下一位,直到找到不相等的 Xi ,或者出现其中一Xi
不存在的情况,Xi较小的或者不存在的那个就是版本号较小的。现在蒜头手上有一些版本号,但是已经被花椰妹打乱了顺序,所以需要麻烦你帮蒜头把这些版本号按找时间递增的顺序排序。

输入格式

第一行输入一个整数 n,表示版本号的个数。接下来 n 排,每行只包含数字和英文句号.,表示一个版本号。

输出格式

输出 n 行,每行一个版本号,为版本号排序后的结果。

样例输入:
5
16.04.2
12.04
16.04
16.10
14.04
样例输出:
12.04
14.04
16.04
16.04.2
16.10

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;
struct node {
	string version;
	vector<int> v;
} G[110];
bool cmp(const node &a, const node &b) 
{
	for (int i = 0; ; i++)
	{
		if (a.v[i] != b.v[i])
		{
			return a.v[i] < b.v[i];
		} 
	}
}
int main()
{
	int n;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		cin >> G[i].version;
		int cnt = 0;
		for (int j = 0; j < G[i].version.size(); j++)
		{
			if (G[i].version[j] == '.')
			{
				G[i].v.push_back(cnt);
				cnt = 0;
			} 
			else
			{
				cnt = cnt * 10 + G[i].version[j] - '0';
			}
		}
		G[i].v.push_back(cnt);
	}
	sort(G, G + n, cmp);
	for (int i = 0; i < n; i++)
	{
		cout << G[i].version << endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_43469554/article/details/88124282