題目連結
題意:
一個解碼的概念
每組測資以數個 (字母 + 個數) 的組合而成
照規則輸出即可
如:
輸入 A3B5C4
輸出 AAABBBBBCCCC
解法:
這題題目很直觀
比較要思考的是測資的讀法
因為數字不一定只有一位 (EX: A12B2D333)
所以除了紀錄字母外
也要能判斷代表數字的範圍
(如果用 String 讀測資,就得紀錄數字的頭尾 index)
程式(Java):
題意:
一個解碼的概念
每組測資以數個 (字母 + 個數) 的組合而成
照規則輸出即可
如:
輸入 A3B5C4
輸出 AAABBBBBCCCC
解法:
這題題目很直觀
比較要思考的是測資的讀法
因為數字不一定只有一位 (EX: A12B2D333)
所以除了紀錄字母外
也要能判斷代表數字的範圍
(如果用 String 讀測資,就得紀錄數字的頭尾 index)
程式(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()); | |
for (int i = 1; i <= t; i++) { | |
System.out.print("Case " + i + ": "); | |
int index = 0; | |
char c; | |
String s = sc.nextLine(); | |
while (index != s.length()) { | |
c = s.charAt(index++); | |
int begin = index; | |
while ((s.charAt(index) >= 48) && (s.charAt(index) <= 57)) { | |
if (++index == s.length()) | |
break; | |
} | |
for (int j = 1; j <= Integer.valueOf(s.substring(begin, index)); j++) | |
System.out.print(c); | |
} | |
System.out.println(); | |
} | |
} | |
} |
留言
張貼留言