【題目】Problem B.尋找變化前01序列

Problem B.尋找變化前01序列

問題描述
給你一個01序列,HDLC協議處理的話,如果出現連續的5個1會補1個0。例如1111110,會變成11111010。
現在給你一個經過HDLC處理後的01序列,你需要找到HDLC處理之前的01序列。
例如給你11111010
你需要輸出1111110

Input
輸入正整數N,表示N例測試。接着輸入N組數據,每組輸入經過HDLC處理過的01序列(長度小於100)。

Output
對每組輸入數據,輸出HDLC處理前的01序列。

Sample Input
2
11111010
1111100

Sample Output
1111110
111110

解答:

import java.util.Scanner;
/*
Problem B.尋找變化前01序列
問題描述
給你一個01序列,HDLC協議處理的話,如果出現連續的5個1會補1個0。例如1111110,會變成11111010。
現在給你一個經過HDLC處理後的01序列,你需要找到HDLC處理之前的01序列。
例如給你11111010
你需要輸出1111110
Input
輸入正整數N,表示N例測試。接着輸入N組數據,每組輸入經過HDLC處理過的01序列(長度小於100)。
Output
對每組輸入數據,輸出HDLC處理前的01序列。
Sample Input
2
11111010
1111100
Sample Output
1111110
111110
 */
public class Test{
	
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		while(scanner.hasNext()) {
			int n = scanner.nextInt();
			for(int t = 0; t < n; t++) {
				int count = 0;
				String str = scanner.next();
				StringBuffer stb = new StringBuffer();
				for(int i = 0; i < str.length(); i++) {
					if(count == 5) {
						count = 0;
						continue;
					}
					if(str.charAt(i) - '0' == 1) count++;
					else count = 0;
					stb.append(str.charAt(i));
				}
				System.out.println(stb.toString());
			}
		}
	}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章