112A. Petya and Strings
- time limit per test2 seconds
- memory limit per test256 megabytes
- inputstandard input
- outputstandard output
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.
小彼佳喜欢礼物。他妈妈给他买了两根同样大小的绳子作为生日礼物。字符串由大写和小写拉丁字母组成。现在,Petya想按字典顺序比较这两个字符串。字母的大小写无关紧要,也就是说,大写字母被认为等同于相应的小写字母。帮助Petya进行比较。
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.
前两行中的每一行都包含一个已购买的字符串。字符串的长度从1到100不等。保证字符串长度相同,并且由大写和小写拉丁字母组成。
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.
如果第一个字符串小于第二个字符串,请打印“-1”。如果第二个字符串小于第一个字符串,则打印“1”。如果字符串相等,则打印“0”。请注意,在比较字符串时,不考虑字母的大小写。
Examples
input1
aaaa
aaaA
output1
0
input2
abs
Abz
output2
-1
input3
abcdefg
AbCdEfF
output3
1
Note
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:
如果您想了解有关词典顺序(也称为“词典顺序”或“字母顺序”)的更多正式信息,可以访问以下网站:
Solution
考虑字母在ASCII码上的顺序,A……Z……a……z, 其中,A与a之间间隔为32,因此不考虑大小写的话,就将小写字母的ASCII码与大写字母'A'的ASCII码的差值模32,即可获得小写字母的大写的ASCII码。
Code
#include <iostream>
using namespace std;
//112A. Petya and Strings
int main() {
string a,b;
cin >> a >> b;
int res = 0;
int len = a.size();
int ia,ib = 0;
for(int i = 0;i < len;i++){
ia=(a[i]-'A')%32;
ib=(b[i]-'A')%32;
if(ia < ib){
res = -1;
break;
}else if (ia > ib) {
res = 1;
break;
}
}
cout << res << endl;
return 0;
}