1583 - 比较字符串

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

题目输入

输入第一行表示测试用例的个数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;
}

时间限制 1 秒
内存限制 32 MB
讨论 统计
上一题 下一题