码迷,mamicode.com
首页 > 其他好文 > 详细

Compare Version Numbers

时间:2015-06-11 14:33:02      阅读:103      评论:0      收藏:0      [点我收藏+]

标签:

技术分享
 1 #include <iostream>
 2 #include <string>
 3 #include <cstring>
 4 #include <vector>
 5 #include <map>
 6 #include <cmath>
 7 using namespace std;
 8 typedef unsigned int uint32_t;
 9 typedef struct Version
10 {
11     int m,n;
12 }Version;
13 int cmp(Version v1,Version v2)
14 {
15     if(v1.m!=v2.m)
16         return v1.m>v2.m?1:-1;
17     if(v1.n!=v2.n)
18         return v1.n>v2.n?1:-1;
19        return 0;
20 }
21 class Solution {
22 public:
23     int compareVersion(string version1, string version2) {
24         Version v1=toDouble(version1);
25         Version v2=toDouble(version2);
26         return cmp(v1,v2);
27     }
28     Version toDouble(string v)
29     {
30         int n=v.size(),i;
31         int ans=0,tot=0;
32         bool pt=false;
33         for(i=0;i<n;i++)
34         {
35             if(isdigit(v[i]))
36             {
37                 if(!pt)
38                     ans=ans*10+v[i]-0;
39                 else
40                     tot=tot*10+v[i]-0;
41             }
42             else
43             {
44                 pt=true;
45             }
46         }
47         Version ver;
48         ver.m=ans;
49         ver.n=tot;
50         return ver;
51     }
52 };
53 int main()
54 {
55     int n;
56     string a,b;
57     while(cin>>a>>b)
58     {
59         Solution sol;
60         cout<<sol.compareVersion(a,b)<<endl;
61     }
62     return 0;
63 }
View Code

 

Compare Version Numbers

标签:

原文地址:http://www.cnblogs.com/varcom/p/4568911.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!