您的位置:首页 > 其它

Compare Version Numbers

2016-07-09 10:18 225 查看
Compare two version numbers version1 and version1.

If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0.

You may assume that the version strings are non-empty and contain only digits and the 
.
 character.

The 
.
 character does not represent a decimal point and is used to separate number sequences.

For instance, 
2.5
 is not "two and a half" or "half way to version three", it is the fifth second-level revision of the second first-level revision.

Here is an example of version numbers ordering:

0.1 < 1.1 < 1.2 < 13.37

Credits:

Special thanks to @ts for adding this problem and creating all test cases.

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;

int comparenumver(string, string);
int main()
{
string a = "23.49";
string b = "23.49";
int result;
result = comparenumver(a, b);
cout << result << endl;
system("pause");
return 0;
}

int comparenumver(string a, string b)
{
/*int index1 = a.find('.', 0);
int index2 = b.find('.', 0);
string a1 = a.substr(0, index1);
string b1 = b.substr(0, index2);
int aa1 = atoi(a1.c_str());
int bb1 = atoi(b1.c_str());
if (aa1 > bb1)
{
return 1;
}
else if (aa1 < bb1)
{
return -1;
}
else
{

string a1 = a.substr(index1+1, a.size()-1-index1);
cout << a1;
string b1 = b.substr(index2+1, b.size()-1 - index2);
int aa1 = atoi(a1.c_str());
int bb1 = atoi(b1.c_str());
if (aa1 > bb1)
{
return 1;
}
else if (aa1 < bb1)
{
return -1;
}
else
{
return 0;
}
}*/
int n1 = a.size();
int n2 = b.size();
int i = 0; int j = 0;
int d1, d2;
string tem1;
string tem2;
while (i < n1 || j < n2)
{
while (i < n1 && a[i] != '.')
{
tem1 = tem1 + a[i];
i++;
}
while (j < n2 && b[j] != '.')
{
tem2 = tem2 + b[j];
j++;
}
d1 = atoi(tem1.c_str());
d2 = atoi(tem2.c_str());
if (d1 > d2)
{
return 1;
}
else if (d1 < d2)
{
return -1;
}
tem1 = "";
tem2 = "";
i++;
j++;
}
return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: