1583 - 比较字符串

通过次数

0

提交次数

0

时间限制 : 1 秒 内存限制 : 32 MB

输入两个字符串,比较两字符串的长度大小关系。

题目输入

输入第一行表示测试用例的个数m,接下来m行每行两个字符串AB,字符串长度不超过50

题目输出

输出m行。若两字符串长度相等则输出A is equal long to B;若AB长,则输出A is longer than B;否则输出A is shorter than B

输入/输出样例

输入格式

2
abc xy
bbb ccc

输出格式

abc is longer than xy
bbb is equal long to ccc

C语言解答

#include <stdio.h>
#include <string.h>

int main()
{
	int n,a,b;
	char str1[100],str2[100];
	scanf("%d",&n);
	while(n--)
	{
		scanf("%s",str1);
		scanf("%s",str2);
		a=strlen(str1);
		b=strlen(str2);
		if(a > b)
		printf("%s is longer than %s\n",str1,str2);
		if(a == b)
		printf("%s is equal long to %s\n",str1,str2);
		if(a < b)
		printf("%s is shorter than %s\n",str1,str2);	
	}
}

C++解答

#include <cstdio>
#include <cstring>

int main() {
    //freopen("test.in", "r", stdin);
    //freopen("test.out", "w", stdout);
    int t;
    char s1[100], s2[100];
    scanf("%d", &t);
    while (t--) {
        scanf("%s %s", s1, s2);
        int k = strlen(s1) - strlen(s2);
        if (k > 0)
            printf("%s is longer than %s\n", s1, s2);
        else
            printf("%s %s %s\n", s1, 0 == k ? "is equal long to" : "is shorter than", s2);
    }
    return 0;
}

Java解答

import java.util.*;
public class Main 
{
	public static void main(String[] args)
	{
		Scanner cin = new Scanner(System.in);
		String s1,s2;
		int n;
		while(cin.hasNext())
		{
			n=cin.nextInt();
			for(int i=0;i<n;i++)
			{
				s1=cin.next();
				s2=cin.next();
				if(s1.length()>s2.length())
				{
					System.out.println(s1+" is longer than "+s2);
				}
				else if(s1.length()==s2.length())
				{
					System.out.println(s1+" is equal long to "+s2);
				}
				else
				{
					System.out.println(s1+" is shorter than "+s2);
				}
			}
		}
		cin.close();
	}
}

Python解答

for o in range(input()):
    n, m = raw_input().split()
    j = len(n)
    k = len(m)
    if j == k:
        print n + ' is equal long to ' + m
    elif j > k:
        print n + ' is longer than ' + m
    else:
        print n + ' is shorter than ' + m