UVaOJ 10878 - Decode the tape

来源:互联网 发布:浙江省网络监管服务网 编辑:程序博客网 时间:2024/06/06 07:26

//version2:蠢方法#include <cstdio>#include <cstring>using namespace std;char buf[15];int main(int argc, char *argv[]){fgets(buf, sizeof(buf), stdin);while(fgets(buf, sizeof(buf), stdin) && buf[0] != '_'){char ch = 0;if(buf[2] == 'o'){ch += 64;}if(buf[3] == 'o'){ch += 32;}if(buf[4] == 'o'){ch += 16;}if(buf[5] == 'o'){ch += 8;}if(buf[7] == 'o'){ch += 4;}if(buf[8] == 'o'){ch += 2;}if(buf[9] == 'o'){ch += 1;}printf("%c", ch);}return 0;}


以下,我认为有学习地方其它答案,直接贴,就不在我自己的方法上改了。
/********************************* *   日期:2013-5-3 *   作者:SJF0115 *   题号: 10878 - Decode the tape *   来源:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=20&page=show_problem&problem=1819 *   结果:AC *   来源:UVA *   总结: **********************************/  #include <stdio.h>  #include <string.h>    int c[] = { 0, 0, 64, 32, 16, 8, 0, 4, 2, 1, 0}; //这个好~  int main() {      char str[15];      int value,i;      //freopen("C:\\Users\\XIAOSI\\Desktop\\acm.txt","r",stdin);         gets(str);      while(gets(str) && str[0] != '_'){          value = 0;          int len = strlen(str);          for(i = 2;i < len;i++){              if(str[i] == 'o'){                  value += c[i];              }          }          printf("%c",value);      }  }  


够简洁,直接!解决问题就要这样。

当然要思考程序写得优不优雅,能不能通用,但是很多时候没有必要。

#include<stdio.h>int main(){    char c;    int bin=0,i=0;    while((c=getchar())!=EOF)    {        if(c=='\n')        {            if(i==8)            {                putchar(bin);                i=0;            }            bin=0;        }        else        {            if(c==' ')            {                bin=bin*2;                i++;            }            else if(c=='o')            {                bin=bin*2+1;                i++;            }        }    }    return 0;}



以下一段是错误的代码,提交这类代码到OJ会导致Runtime Error!

因为我想要把结果存下来然后一次性全部显示,会出现一个问题:不知道为什么,大概就是因为数组越界,然后导致Runtime Error……

然后我就学乖了,学会用上一段代码,得出一个结果就输出一个。虽然,在命令行下,显示的结果很不好看,input和output混在一起,但是是可以通过OJ的。

#include <cstdio>#include <cstring>using namespace std;char ans[500];char buf[15];int mul[7];int main(int argc, char *argv[]){for(int i = 0; i < 7; ++i){mul[i] = 1 << i;}gets(buf);int n = 0;while(gets(buf) && buf[0] != '_'){ans[n] = 0;if(buf[2] == 'o'){ans[n] += mul[6];}if(buf[3] == 'o'){ans[n] += mul[5];}if(buf[4] == 'o'){ans[n] += mul[4];}if(buf[5] == 'o'){ans[n] += mul[3];}if(buf[7] == 'o'){ans[n] += mul[2];}if(buf[8] == 'o'){ans[n] += mul[1];}if(buf[9] == 'o'){ans[n] += mul[0];}++n;}ans[n] = '\0';printf("%s", ans);return 0;}



0 0