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
The
For instance,
Here is an example of version numbers ordering:
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;
}
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.5is 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;
}
相关文章推荐
- react_native环境问题
- js实现让div可拖动
- 8VC Venture Cup 2016 - Final Round (Div. 2 Edition) A. Orchestra
- hdfs原理
- 1039. Course List for Student (25)
- 用js制作form表单验证、二级联动、登陆界面升级版
- XML
- java回调机制
- microstation level3 10 elliptical cone solid 、ellipsoid、polyhedron
- 项目管理利器maven入门笔记(1)
- JQuery EasyUI学习教程之datagrid 添加、修改、删除操作
- poj 2299(离散化+树状数组)
- Cookie的介绍和使用
- android 日常迭代与维护总结二
- springmvc form表单提交报400错误
- yii2获取表及字段
- virsh常用操作
- 关于图表的JS插件
- centos更新163源并升级内核
- Linux小知识