題目連結
題意:
弟弟學寫字
給定字串,可能代表 one, two, three
但會包含最多一個錯字
如: one - > oqe, two - > twv
輸出其代表的數字
解法:
因為只會有 3 種可能,想辦法區別即可
例如
只有 3 有 5 個字元,先分開
1 跟 2 就拿其中一個的正解來逐個字元比對
因為最多只會錯一個
所以正確字元 >= 2 就正解,否則為另一個
程式(Java):
題意:
弟弟學寫字
給定字串,可能代表 one, two, three
但會包含最多一個錯字
如: one - > oqe, two - > twv
輸出其代表的數字
解法:
因為只會有 3 種可能,想辦法區別即可
例如
只有 3 有 5 個字元,先分開
1 跟 2 就拿其中一個的正解來逐個字元比對
因為最多只會錯一個
所以正確字元 >= 2 就正解,否則為另一個
程式(Java):
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
public class Main { | |
public static void main(String args[]) { | |
Scanner sc = new Scanner(System.in); | |
int t = Integer.valueOf(sc.nextLine()); | |
while (t-- > 0) { | |
String s = sc.nextLine(); | |
if (s.length() == 5) | |
System.out.println(3); | |
else { | |
String one = "oneXXXXX"; | |
int score = 0; | |
for (int i = 0; i < s.length(); i++) | |
if (s.charAt(i) == one.charAt(i)) | |
score++; | |
if (score >= 2) | |
System.out.println(1); | |
else | |
System.out.println(2); | |
} | |
} | |
} | |
} |
留言
張貼留言