112A. Petya and Strings
implementation/strings, 800, http://codeforces.com/problemset/problem/112/A
Little Petya loves presents. His mum bought him two strings of the same size for his birthday. The strings consist of uppercase and lowercase Latin letters. Now Petya wants to compare those two strings lexicographically. The letters' case does not matter, that is an uppercase letter is considered equivalent to the corresponding lowercase letter. Help Petya perform the comparison.
Input
Each of the first two lines contains a bought string. The strings' lengths range from 1 to 100 inclusive. It is guaranteed that the strings are of the same length and also consist of uppercase and lowercase Latin letters.
Output
If the first string is less than the second one, print "-1". If the second string is less than the first one, print "1". If the strings are equal, print "0". Note that the letters' case is not taken into consideration when the strings are compared.
Examples
input
aaaa
aaaAoutput
0input
abs
Abzoutput
-1input
abcdefg
AbCdEfFoutput
1Note
If you want more formal information about the lexicographical order (also known as the "dictionary order" or "alphabetical order"), you can visit the following site:
按字典方式排序,不考虑大小写。一个个字母比即可。
s1 = input().lower()
s2 = input().lower()
num = 0
for i in range(len(s1)):
if ord(s1[i]) < ord(s2[i]):
print('-1')
break
elif ord(s1[i]) > ord(s2[i]):
print('1')
break
else:
num += 1
if num == len(s1):
print('0')可以直接比较字符串的大小,不用一个一个比较(比较字符串大小本来就是一个一个比较的,这样代码会比较简) (来自江雨翔)
line1 = input()
line2 = input()
if line1.lower() > line2.lower():
print(1)
elif line1.lower() < line2.lower():
print(-1)
else:
print(0)short code
i=input;a=i().lower();b=i().lower()
print((a>b) - (a<b))()表 判断,返回布尔值,加减运算 True是 1 False是 0
C++,陆宸
#include <iostream>
#include <cstring>
using namespace std;
int main (){
string str1, str2;
int len;
int cnt1, cnt2;
cnt1 = 0;
cnt2 = 0;
cin >> str1 >> str2;
len = str1.size();
for (int i = 0; i < len; i++){
if (str1[i] < 97){
str1[i] += 32;
}
if (str2[i] < 97){
str2[i] += 32;
}
if (str1[i] > str2[i]){
cout << 1;
break;
}else if (str1[i] < str2[i]){
cout << -1;
break;
}else if (str1[i] == str2[i] && i == len - 1){
cout << 0;
break;
}
}
return 0;
}