uva11541 Decoding

uva11541

uDebug11541

題意解釋了行程長度編碼(Run-Length Encoding),並要求對給定的字符串進行對應的解碼。這個題目C語言感覺是非常便捷,用python不知道怎麼走捷徑,於是只好用土辦法一個字符一個字符的判斷累加了。。。

python版本AC代碼

testcase = int(input())
for i in range(testcase+1):
	if i == 0:
		continue
	strlist = input()
	strlen = len(strlist)
	j = 0
	decodestr = "Case {}: ".format(i)
	while j < strlen:
		ch = strlist[j]
		if ch == '\n':
			break
		else:
			cnt = 0
			k = j + 1
			while(k < strlen and strlist[k].isdigit()):
				cnt *= 10
				cnt += int(strlist[k])
				k += 1
			decodestr += ch * cnt
			j = k
	print(decodestr)

C/C++版本AC代碼

#include <iostream>
#include<cstdio>
using namespace std;

//#define ZANGFONG

int main()
{
    #ifdef ZANGFONG
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
    #endif // ZANGFONG
    int testcase,i,cnt;
    char ch;
    scanf("%d\n",&testcase);
    for(i = 1; i <= testcase; i++)
    {
        printf("Case %d: ",i);
        while(scanf("%c",&ch)!=EOF && ch != '\n')
        {
            scanf("%d",&cnt);
            while(cnt)
            {
                printf("%c",ch);
                cnt--;
            }
        }
        printf("\n");
    }
    return 0;
}

 

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