[CF从零单排#5]112A - Petya and Strings

题目来源: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
inputCopy
aaaa
aaaA
outputCopy
0
inputCopy
abs
Abz
outputCopy
-1
inputCopy
abcdefg
AbCdEfF
outputCopy
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:

http://en.wikipedia.org/wiki/Lexicographical_order

题目大意:

输入两个字符串,保证它们都是字母且长度相等。比较时,不区分大小写。按照字典序进行比较,如果第一个字符串更大,结果是1;一样大,结果是0;如果第二个字符串更大,结果是-1。

题目分析:

模拟题,读入字符串,求出字符串长度len。从0~len-1扫描一遍,先将两个字符串的字母都转为小写,再比较大小。

#include <bits/stdc++.h>
using namespace std;
int main(){
	char s[110], p[110];
	cin >> s >> p;
	int len = strlen(s);
	int k = 0;
	for(int i=0; i<len; i++){
		if(s[i]>='A' && s[i]<='Z')
			s[i] += 'a'-'A';
		if(p[i]>='A' && p[i]<='Z')
			p[i] += 'a'-'A';
		if(s[i]>p[i]){
			k = 1;
			break;
		}else if(s[i]<p[i]){
			k = -1;
			break;
		}
	}
	cout << k;
	return 0;
} 

猜你喜欢

转载自www.cnblogs.com/gdgzliu/p/13368998.html