返回列表 發帖

402 字串與檔案處理 (字串比較)

1. 題目說明:
請依下列題意進行作答,使輸出值符合題意要求。

2. 設計說明:
請撰寫一程式,讓使用者輸入兩個相同長度的字串與一個正整數n,字串長度皆不超過128個字元,依ASCII碼表上的順序比對兩字串前n個字元,最後輸出兩字串前n個字元的比較結果。若使用者輸入正整數n超過字串長度,則輸出「error」。

提示:若使用 Java 語言答題,請以「JP」開頭命名包含 main 靜態方法的 class,評測系統才能正確評分。

3. 輸入輸出:
輸入說明
兩個相同長度的字串及一個正整數

輸出說明
兩字串前n個字元的比較結果(大於、等於、小於)

範例輸入1
Apple ipad
Apple ipod
5

範例輸出1
Apple ipad = Apple ipod

範例輸入2
Apple ipad
Apple ipod
9

範例輸出2
Apple ipad < Apple ipod

範例輸入3
Apple ipad
Apple ipod
15

範例輸出3
error

本帖隱藏的內容需要回復才可以瀏覽
Python
  1. str1 = input()
  2. str2 = input()
  3. n = int(input())
  4. compare = 0

  5. if n > len(str1):
  6.     print('error')
  7.     exit()

  8. for i in range(n):
  9.     compare += ord(str1[i]) - ord(str2[i])

  10. if compare > 0:
  11.     print(str1 + ' > ' + str2)
  12. elif compare < 0:
  13.     print(str1 + ' < ' + str2)
  14. else:
  15.     print(str1 + ' = ' + str2)
複製代碼

本帖最後由 鄭傳諭 於 2024-1-31 15:03 編輯

str1=input()
str2=input()
n=int(input())
if(n>len(str1) or n>len(str2)):
    print("error")
else:
    s=0
    for i in range(n):
        s=s+(ord(str1)-ord(str2))
    if(s==0):
        print(f"{str1}={str2}")
    if(s==0):
        print(f"{str1}>{str2}")   
    if(s==0):
        print(f"{str1}<{str2}")

TOP

  1. str1=input()
  2. str2=input()
  3. n=int(input())
  4. if len(str1)<n or len(str2)<n:
  5.     print("error")
  6. else:
  7.     for i in range(n):
  8.         s=ord(str1[i])-ord(str2[i])
  9.     if s==0:
  10.         print(f"{str1} = {str2}")
  11.     elif s<0:
  12.         print(f"{str1} < {str2}")
  13.     else:
  14.         print(f"{str1} > {str2}")
複製代碼

TOP

  1. a=input()
  2. b=input()
  3. n=int(input())
  4. t=0
  5. if(n>len(a) or n>len(b)):
  6.     print("error")
  7. else:
  8.     for i in range(n):
  9.         t+=ord(a[i])-ord(b[i])
  10.     if(t<0):
  11.         print(f"{a} < {b}")
  12.     elif(t>0):
  13.         print(f"{a} > {b}")
  14.     else:
  15.         print(f"{a} = {b}")
複製代碼

TOP

  1. str1=input()   
  2. str2=input()
  3. n=int(input())
  4. s=0

  5. if n>len(str1):
  6.     print("error")
  7. else:
  8.     for i in range(n):
  9.         s+=ord(str1[i])-ord(str2[i])
  10.     if s==0:
  11.         print(f"{str1} = {str2}")
  12.     elif s>0:
  13.         print(f"{str1} > {str2}")
  14.     else:
  15.         print(f"{str1} < {str2}")
複製代碼
回復 1# 鄭繼威

TOP

str1=input()
str2=input()
n=int(input())
if(n>len(str1) or n>len(str2)):
    print("error")
else:
    for i in range(n):
         s=0
         s=s+ord(str1[i])-ord(str2[i])
    if(s>0):
        print(f"{str1} > {str2}")
    elif(s<0):
        print(f"{str1} < {str2}")
    else:
        print(f"{str1} = {str2}")

TOP

  1. str1=input()
  2. str2=input()
  3. n=int(input())
  4. if(n>len(str1)or n>len(str2)):
  5.     print("error")
  6. else:
  7.     s=0
  8.     for i in range(n):
  9.         s=s+(ord(str1[1])-ord(str2[i]))
  10.         
  11.     if(s==0):
  12.         print(f"{str1} = {str2}")
  13.     elif(s>0):
  14.         print(f"{str1} > {str2}")
  15.     else:
  16.         print(f"{str1} < {str2}")
複製代碼

TOP

  1. #include<bits/stdc++.h>
  2. using namespace std;

  3. int main()
  4. {
  5.     string str1;
  6.     string str2;
  7.     int n;

  8.     getline(cin,str1);
  9.     getline(cin,str2);
  10.     cin>>n;
  11.     if(n>str1.length())
  12.     {
  13.         cout<<"error";
  14.     }

  15.     int sum=0;
  16.     for(int i=0;i<n;i++)
  17.     {
  18.         sum=sum+(str1[i]-str2[i]);
  19.     }
  20.     if(sum>0)
  21.     {
  22.         cout<<str1<<" > "<<str2;
  23.     }
  24.     else if(sum<0)
  25.     {
  26.         cout<<str1<<" < "<<str2;
  27.     }
  28.      else
  29.      {
  30.          cout<<str1<<" = "<<str2;
  31.      }
  32.     return 0;
  33. }
複製代碼

TOP

返回列表