【題目敘述】https://zerojudge.tw/ShowProblem?problemid=a466
【解題想法】
- 函式 bool cmp(string s1, string s2):比較兩個字串相異的字元數目是否小於等於 1。
#include <iostream>
using namespace std;
bool cmp(string s1, string s2){
int cnt = 0;
for (int i = 0; i < s1.size(); i++){
if (s1[i] != s2[i]) cnt++;
}
return (cnt <= 1);
}
int main() {
int T;
string s;
cin >> T;
while (T--){
cin >> s;
if (s.size() == 5){
cout << "3\n";
} else {
if (cmp("one", s)) cout << "1\n";
if (cmp("two", s)) cout << "2\n";
}
}
return 0;
}
Python code (credit: Amy Chou)
def cmp(s1, s2):
cnt = 0
for i in range(len(s1)):
if s1[i] != s2[i]:
cnt += 1
return cnt <= 1
T = int(input())
for _ in range(T):
s = input()
if len(s) == 5:
print(3)
else:
if cmp("one", s):
print(1)
if cmp("two", s):
print(2)