uva 10878 - Decode the tape

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1819

这题很简单,就是一个ascii转换,不过代码写的好丑啊,buff数组开大点,他没说上限多少行,还是要注意的。

 1 #include<iostream>
 2 #include<string>
 3 #include<stdio.h>
 4 using namespace std;
 5 void string_replace(string &src){
 6     for(int i=0;i<src.size();i++){
 7         if(src[i]=='o')
 8             src[i]=1;
 9         else
10             src[i]=0;
11     }
12 }
13 int convert_int(string s){
14     int num[7]={64,32,16,8,4,2,1};
15     int sum=0;
16     for(int i=0;i<7;i++)
17         sum+=(int)s[i]*num[i];
18     return sum;
19 }
20 char buff[100000];
21 int main(){
22     string l,s,s1,s2;
23     
24     int i=0,temp=0;
25     while(getline(cin,l)){
26         if(l[0]=='_'){
27             if(temp==0){
28                 temp=1;
29                 continue;
30             }
31             break;
32         }
33         s1.assign(l,2,4);
34         s2.assign(l,7,3);
35         s=s1+s2;
36         string_replace(s);
37         sprintf(&buff[i++],"%c",convert_int(s));
38     }
39     printf("%s",buff);
40 }

 

posted on 2013-12-09 20:55  云在心  阅读(169)  评论(0)    收藏  举报

导航