Infinite String Comparision --2020 Niuke Summer Multi-school Training Camp (First Session)

Infinite String Comparision
idea: This
question does not seem to be difficult, but it omparing a∞-b∞, so you need to pay attention to the way of comparing two strings. When comparing, use loops. It is not necessary to repeat strings of different lengths, and to extend similar infinite lengths for comparison. You only need to repeat the two loops once if there is no answer. i]>a[j] or a[i]<a[j].
code show as below:

#include <iostream>
#include <string>
using namespace std;
int main()
{
    
    
 int i,j,flag=0;
 string a,b;
 while(cin>>a>>b)
 {
    
    
  i=0;
  j=0;
  long long cnt=0;
        if(a.size()>=b.size())
        {
    
    
            flag=0;
        }
        else {
    
    
            flag=1;
        }
  while(1)
  {
    
    
   if(cnt==2)
   {
    
    
    cout<<"="<<endl;
    break;
   } 
   if(i==a.size())
   {
    
    
    i=0;
    if(flag==0)
                    cnt++;
   }
   if(j==b.size())
   {
    
    
    j=0;
    if(flag==1)
                    cnt++;
   }
   if(a[i]==b[j])
   {
    
    
    i++;
    j++;
    continue;
   }
   if(a[i]>b[j])
   {
    
    
    cout<<">"<<endl;
    break;
   }
   if(a[i]<b[j])
   {
    
    
    cout<<"<"<<endl;
    break;
   }
   i++;
   j++;
  }
 }
 return 0;
} 

Input data:
aa
b
zzz
zz
aba
abaa
output data:

<

=

.>

Guess you like

Origin blog.csdn.net/HT24k/article/details/107448603