題目連結
題意:
題目會給好幾行測資
其中包含偶數個雙引號 " 字元
測資輸入什麼就輸出什麼
除了
遇到第單數個雙引號
要輸出 ``
遇到第雙數個雙引號
要輸出'' (兩個單引號)
解法:
不需要特別的解法
可以用一個布林變數
紀錄現在是單數個或雙數個
照著規定輸出就可以了
程式(Java):
題意:
題目會給好幾行測資
其中包含偶數個雙引號 " 字元
測資輸入什麼就輸出什麼
除了
遇到第單數個雙引號
要輸出 ``
遇到第雙數個雙引號
要輸出'' (兩個單引號)
解法:
不需要特別的解法
可以用一個布林變數
紀錄現在是單數個或雙數個
照著規定輸出就可以了
程式(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); | |
boolean first = true; | |
while (sc.hasNext()) { | |
String s = sc.nextLine(); | |
for (int i = 0; i < s.length(); i++) | |
if (s.charAt(i) == '"') { | |
if (first) { | |
System.out.print("``"); | |
first = false; | |
} | |
else { | |
System.out.print("''"); | |
first = true; | |
} | |
} | |
else | |
System.out.print(s.charAt(i)); | |
System.out.println(); | |
} | |
} | |
} |
留言
張貼留言