如果version1 > version2 返回1,如果 version1 < version2 返回-1,不然返回0.
输入的version字符串非空,只包含数字和字符.。.字符不代表通常意义上的小数点,只是用来区分数字序列。例如字符串2.5并不代表二点五,只是代表版本是第一级版本号是2,第二级版本号是5.
如果version1 > version2 返回1,如果 version1 < version2 返回-1,不然返回0.
输入的version字符串非空,只包含数字和字符.。.字符不代表通常意义上的小数点,只是用来区分数字序列。例如字符串2.5并不代表二点五,只是代表版本是第一级版本号是2,第二级版本号是5.
两个字符串,用空格分割。
每个字符串为一个version字符串,非空,只包含数字和字符.
只能输出1, -1,或0
0.1 1.1
-1
version1和version2的长度不超过1000,由小数点'.'分隔的每个数字不超过256。
//用c语言的scanf输入int判断更便捷; #include <bits/stdc++.h> using namespace std; int main(){ vector<int> arr1,arr2; int i=0,t; while(1){ if(cin.peek()==' ') break; scanf("%d.",&t); arr1.push_back(t); } while(1){ if(cin.peek()=='\n') break; scanf("%d.",&t); arr2.push_back(t); } while(i!=arr1.size()&&i!=arr2.size()){ if(arr1[i]!=arr2[i]){ t=arr1[i]>arr2[i] ? 1:-1; break; } i++; } if(i==arr1.size()||i==arr2.size()){ if(arr1.size()==arr2.size()) t=0; else t= arr1.size()>arr2.size() ? 1:-1; } cout<<t; return 0; }
x,y = map(lambda x:list(map(int, x.split('.'))), input().split()) if x > y: print(1) elif x < y: print(-1) else: print(0)
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String[] v1 = sc.next().split("\\."); String[] v2 = sc.next().split("\\."); int len = Math.max(v1.length, v2.length); int[] num1 = new int[len]; int[] num2 = new int[len]; for (int i = 0; i < v1.length; i++) { num1[i] = Integer.parseInt(v1[i]); } for (int i = 0; i < v2.length; i++) { num2[i] = Integer.parseInt(v2[i]); } int index = 0; while (index < len) { if (num1[index] < num2[index]) { System.out.println(-1); return; } else if (num1[index] > num2[index]) { System.out.println(1); return; } else { index++; } } System.out.println(0); } }
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line; while((line = br.readLine()) != null){ String[] strArr = line.trim().split(" "); System.out.println(solve(strArr[0], strArr[1])); } } private static int solve(String version1, String version2) { String[] version1Split = version1.split("\\."); String[] version2Split = version2.split("\\."); int idx = 0; // 逐位比较版本号 while(idx < version1Split.length && idx < version2Split.length){ // 有大小关系时直接返回比较结果,否则跳到下一位进行比较 if(Integer.parseInt(version1Split[idx]) > Integer.parseInt(version2Split[idx])) return 1; else if(Integer.parseInt(version1Split[idx]) < Integer.parseInt(version2Split[idx])) return -1; else idx ++; } // 两个版本号的公共长度相同,谁长谁大 if(idx < version1Split.length) return 1; else if(idx < version2Split.length) return -1; else return 0; } }
/* 不仅要考虑字符串长度相同还要考虑不同的时候 */ import java.io.*; public class Main{ public static void main( String[] args ) throws IOException{ BufferedReader br = new BufferedReader( new InputStreamReader( System.in ) ); String[] inarr = br.readLine().split( " " ); String[] str1 = inarr[0].split("\\."); String[] str2 = inarr[1].split("\\."); //先直接进行比较,看看谁的版本高 for( int i=0; i<str1.length && i<str2.length; i++ ){ if( Integer.parseInt( str1[i] ) < Integer.parseInt( str2[i] ) ){ System.out.println( "-1" ); return; } else if( Integer.parseInt( str1[i] ) > Integer.parseInt( str2[i]) ){ System.out.println( "1" ); return; } } //当前面的比较没有成功,有如下情况 //长度相同时版本一致, //一长一短,并且短的部分与长的前面相同 if( str1.length == str2.length ){ System.out.println( "0" ); } else if( str1.length < str2.length ){ System.out.println( "-1" ); } else{ System.out.println( "1" ); } return; } }
#include <bits/stdc++.h> using namespace std; int Cmp(string a, string b){ if(a==b) return 0; int m = a.length(), n = b.length(); int i=0, j=0, p=0, q=0; while(i<m && j<n){ while(i<m && a[i]!='.') i++; string c = a.substr(p,i-p); i++; p = i; while(j<n && b[j]!='.') j++; string d = b.substr(q,j-q); j++; q = j; if(c==d) continue; if(stoi(c)>stoi(d)) return 1; else return -1; } if(i>=m && j<n) return -1; if(i<m && j>=n) return 1; return 0; } int main(){ string a,b; cin>>a>>b; cout<<Cmp(a,b)<<endl; return 0; }
def main(): a = list(input().split(" ")) b, c = [list(map(int,it.split("."))) for it in a] l = abs(len(b) - len(c)) if l and len(b) > len(c): c += [0] * l if l and len(b) < len(c): b += [0] * l for c1, c2 in zip(b, c): if c1 > c2: print(1) return elif c1 < c2: print(-1) return print(0) if __name__ == "__main__": main()
#include <bits/stdc++.h> using namespace std; int main() { string s1,s2; cin>>s1>>s2; int n1=0,n2=0,i=0,j=0; while(i<s1.size()||j<s2.size()) { if(i==s1.size()&&j<s2.size()) { cout<<-1<<endl; break; } if(i<s1.size()&&j==s2.size()) { cout<<1<<endl; break; } if(i>=s1.size()&&j>=s2.size()) { cout<<0<<endl; break; } if(s1[i]!='.'&&i<s1.size()) { n1 += n1*10 + s1[i] - '0'; ++i; } if(s2[j]!='.'&&j<s2.size()) { n2 += n2*10 + s2[j] - '0'; ++j; } if(s1[i]=='.'&&s2[j]=='.') { if(n1>n2) { cout<<1<<endl; break; } else if(n1<n2) { cout<<-1<<endl; break; } else { n1=0; n2=0; i++; j++; } } } if(i==s1.size()&&j==s2.size()) { if(n1>n2) cout<<1<<endl; else if(n1<n2) cout<<-1<<endl; else cout<<0<<endl; } return 0; }
# Python3 代码---完全通过版 def judgeTwoVersion(str0): a, b = str0[0], str0[1] # 将字符串转存为两个列表 list_a, list_b = a.split('.'), b.split('.') # 求最小的长度,考虑到两个字符串可能出现长度不等的情况 mylen = min(len(list_a), len(list_b)) # 等长度判断 for i in range(mylen): if int(list_a[i]) < int(list_b[i]): print('-1') break elif int(list_a[i]) > int(list_b[i]): print('1') break elif int(list_a[i]) == int(list_b[i]): continue # 非等长度判断 if i == mylen - 1 and list_a[mylen - 1] == list_b[mylen - 1]: if len(list_a) > len(list_b): print('1') elif len(list_a) < len(list_b): print('-1') elif len(list_a) == len(list_b): print('0') # 主函数 if __name__ == '__main__': str0 = [i for i in input().split()] # 输入字符串 judgeTwoVersion(str0) # 输出判断结果
需要注意的2个问题:
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String input1 = in.next(); String input2 = in.next(); if (input1.equals(input2)) { System.out.println(0); return; } String[] v1 = input1.split("\\."); String[] v2 = input2.split("\\."); int len1 = v1.length; int len2 = v2.length; for (int i = 0; i < Math.min(len1, len2); i++) { if (Integer.parseInt(v1[i]) < Integer.parseInt(v2[i])) { System.out.println(-1); return; } else if (Integer.parseInt(v1[i]) > Integer.parseInt(v2[i])) { System.out.println(1); return; } } //可能一个版本号是另一个版本的子串,也有可能完全相同 if (len1 < len2) { System.out.println(-1); return; } else if (len1 > len2) { System.out.println(1); return; } else { System.out.println(0); return; } } }
class MainActivity: def main(self): # Read the data v1, v2 = filter(lambda x: len(x) > 0, input().split(' ')) v1 = list(map(int, v1.split('.'))) v2 = list(map(int, v2.split('.'))) # Compare flag = True if len(v1) > len(v2): v1, v2 = v2, v1 flag = False for ptr in range(len(v1)): if v1[ptr] < v2[ptr]: print(-1 if flag else 1) return elif v1[ptr] > v2[ptr]: print(1 if flag else -1) return if len(v2) > len(v1): print(-1 if flag else 1) else: print(0) if __name__ == '__main__': M = MainActivity() M.main()
package main import ( "fmt" "strconv" "strings" ) func main() { str1,str2:="","" fmt.Scan(&str1,&str2) if str1==str2{ fmt.Println(0) }else{ str11:=strings.Split(str1,".") str21:=strings.Split(str2,".") n:=min(len(str11),len(str21)) for i:=0;i<n;i++{ num1,_:=strconv.Atoi(str11[i]) num2,_:=strconv.Atoi(str21[i]) if num1>num2{ fmt.Println(1) return }else if num1<num2{ fmt.Println(-1) return } } if len(str11)<len(str21){ fmt.Println(-1) }else{ fmt.Println(1) } } } func min(a,b int) int { if a>b{ return b } return a }
#include <iostream> (720)#include <string> // #include <stdlib.h> using namespace std; int versionCompare(string& version1, string& version2) { char* p1 = (char*)version1.data(); char* p2 = (char*)version2.data(); int v1, v2; while(*p1 || *p2) { v1 = v2 = 0; if (*p1) v1 = atoi(p1); if (*p2) v2 = atoi(p2); if (v1 > v2) return 1; if (v1 < v2) return -1; while (*p1 && *p1 != '.') ++p1; if (*p1 == '.') ++p1; while (*p2 && *p2 != '.') ++p2; if (*p2 == '.') ++p2; } return 0; } int main() { string version1, version2; while (cin >> version1 >> version2) { int ret = versionCompare(version1, version2); cout << ret << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string version1, version2; cin>>version1>>version2; int n1 = version1.size(), n2 = version2.size(); int p1 = 0, p2 = 0; while(p1<n1 && p2<n2) { int start1 = p1, start2 = p2; while(p1<n1 && version1[p1]!='.') p1++; while(p2<n2 && version2[p2]!='.') p2++; int t1 = stoi(version1.substr(start1, p1-start1)); int t2 = stoi(version2.substr(start2, p2-start2)); if(t1<t2) { cout<<-1<<endl; return 0; } else if(t1>t2) { cout<<1<<endl; return 0; } p1++, p2++; } if(p1>n1 && p2>n2) { cout<<0<<endl; } else if(p1>n1) { cout<<-1<<endl; return 0; } else { cout<<1<<endl; return 0; } }
#include <iostream> #include <cstdio> #include <cstring> using namespace std; int x[10005], y[10005], c1=1, c2=1; char a[100005], b[10005]; int main() { cin >> a >> b; int l = strlen(a); for(int i = 0; i < l; i++) { if(a[i] == '.') c1++; else x[c1] = x[c1] * 10 + (a[i] - '0'); } l = strlen(b); for(int i = 0; i < l; i++) if(b[i] == '.') c2++; else y[c2] = y[c2] * 10 + (b[i] - '0'); for(int i = 1; i <= max(c1,c2); i++) if(x[i] < y[i]) { printf("-1\n"); return 0; } else if(x[i] > y[i]) { printf("1\n"); return 0; } printf("0\n"); return 0; }
#include <bits/stdc++.h> using namespace std; #define Up(i,a,b) for(int i = a; i <= b; i++) #define ms(a,x) memset(a,x,sizeof(a)) typedef long long ll; void split(vector<int> &v,string s) //把字符串s按.分隔开并存入v中 { int bg = 0, ed = s.find('.'); int len = s.length()-1; Up(i,0,len) { if(s[i] == '.') { ed = i; int t = stoi(s.substr(bg,ed-bg)); //获取各级版本号 // cout << t << endl; v.push_back(t); bg = ed+1; } if(i == len) //最后一个版本号 { ed = len+1; int t = stoi(s.substr(bg,ed-bg)); //cout << t << endl; v.push_back(t); } } } int cmp(vector<int> &v1,vector<int> &v2) { int sz = min(v1.size(),v2.size())-1; Up(i,0,sz) { if(v1[i] < v2[i]) return -1; else if(v1[i] > v[2]) return 1; else return 0; } } int main() { ios::sync_with_stdio(false); cin.tie(0),cout.tie(0); string s1,s2; while(cin >> s1 >> s2) { vector<int> v1,v2; split(v1,s1); //分割字符串s1 split(v2,s2); //分割字符串s2 //开始比较版本号大小 cout << cmp(v1,v2) << endl; } return 0; }
#include <stdio.h> #include<string.h> int test(char *ptr1,char *ptr2) { if(atoi(ptr1)>atoi(ptr2)) return 1; else if(atoi(ptr1)<atoi(ptr2)) return -1; else return 0; } int main() { char tmp; char ver1[256]; char ver2[256]; scanf("%s",ver1); scanf("%s",ver2); char *ptr1 = ver1; char *ptr2 = ver2; int ret = 0; int i = 0; int j = 0; int len1 =strlen(ver1); int len2 = strlen(ver2); while(i< len1 || j< len2) { while(ver1[i] != '.' && ver1[i] != '\0') i++; ver1[i] = '\0'; while(ver2[j] != '.' && ver2[j] != '\0') j++; ver2[j] = '\0'; ret = test(ptr1,ptr2); if(ret == 1) { printf("1"); return 0; } else if(ret == -1) { printf("-1"); return 0; } i++; j++; ptr1 = ver1+i; ptr2 = ver2+j; } if(len1 > len2) printf("1"); else if(len1 < len2) printf("-1"); else printf("0"); }
while(line = readline()){ var lines = line.split(" "); var a = lines[0]; var b = lines[1]; var t = a.split("."); var y = b.split("."); var l1 = t.length; var l2 = y.length; for(i=0;i<l1;i++){ t[i]= parseInt(t[i]) } for(i=0;i<l2;i++){ y[i]= parseInt(y[i]) } var len = Math.min(l1,l2); for(i=0;i<len;i++){ if(t[i]>y[i]){ print(1); break; }else if(t[i]<y[i]){ print(-1); break; }else if(t[i]==y[i]&&i<len-1){ continue; }else if(t[i]==y[i]&&i==len-1&&len==l1&&len==l2){ print(0) }else if(t[i]==y[i]&&i==len-1&&len==l2){ print(1) }else if(t[i]==y[i]&&i==len-1&&len==l1){ print(-1) } } }