比较字符串
题目描述
输入两个字符串,比较两字符串的长度大小关系。
输入
输入第一行表示测试用例的个数m,接下来m行每行两个字符串A和B,字符串长度不超过50。
输出
输出m行。若两字符串长度相等则输出A is equal long to B;若A比B长,则输出A is longer than B;否则输出A is shorter than B。
样例输入 Copy
2
abc xy
bbb ccc
样例输出 Copy
abc is longer than xy
bbb is equal long to ccc
#include<bits/stdc++.h> using namespace std; int f(char a[],char b[]){ int ans=0; int temp=0; int a1=0,a2=1,a3=-1; for(int i=0;i<10;i++){ if(a[i]>'0'){ ans++; } if(b[i]>'0'){ temp++; } } if(temp==ans){ return a1; }else{ if(temp>ans){ return a2; }else{ return a3; } } } int main(){ int n; cin>>n; for(int i=0;i<n;i++){ int ans=0; char a[10]={0},b[10]={0}; cin>>a>>b; if(f(a,b)==1){ cout<<a<<" is shorter than "<<b<<endl; }else{ if(f(a,b)==0){ cout<<a<<" is equal long to "<<b<<endl; }else{ if(f(a,b)==-1){ cout<<a<<" is longer than "<<b<<endl; } } } } }