UVA - 213 Message Decoding (輸入字串並對單個字元進行操作的輸入輸出)

來源:互聯網
上載者:User

標籤:des   style   blog   color   io   os   ar   for   div   

 

POINT:

  關於表示一個編碼:利用code字元數組表示一個編碼字元,其中code[len][val]表示長度為len,二進位值為val的字元;

 

主程式如下:

 1 #include <iostream> 2 #include <sstream> 3 #include <cstdio> 4 #include <cstring> 5 #include <cmath> 6 #include <string> 7 #include <vector> 8 #include <set> 9 #include <cctype>10 #include <algorithm>11 #include <cmath>12 #include <deque>13 #include <queue>14 #include <map>15 #include <stack>16 #include <list>17 #include <iomanip>18 19 using namespace std;20 #define INF 0x7fffffff21 #define maxn 101022 typedef unsigned long long ull;23 24 int code[8][1<<8];25 26 int main()27 {28     while(readcodes())29     {30         //printcodes();31         for(;;)32         {33             int len = readint(3);34             if(len == 0)    break;35             //printf("len = %d\n", len);36 37             for(;;)38             {39                 int v = readint(len);40                 //printf("v = %d\n", v);41                 if(v == (1<<len)-1) break;42                 putchar(code[len][v]);43             }44         }45         putchar(‘\n‘);46     }47     return 0;48 }

 

其中readcodes()函數用來讀取編號,readint()函數用來讀取c位二進位字元並轉化為整數(val);

如何處理“編碼文本可由多行組成”

 1 bool readcodes() 2 { 3     memset(code, 0, sizeof(code));//清空數組 4     code[1][0] = readchar();//輸入開始;單個字元輸入 5     for(int len = 2; len <= 7; len++) 6     { 7         for(int val = 0; val < (1<<len)-1; val++) 8         { 9             int ch = getchar();10             if(ch == EOF)   return 0;11             if(ch == ‘\n‘ || ch == ‘\r‘)    return 1;12 13             code[len][val] = ch;14         }15     }16     return 1;17 }18 int readint(int c)19 {20     int v = 0;21     while(c--)22         v = v*2+readchar()-‘0‘;23     return v;24 }

readchar()函數代碼如下:

1 int readchar()2 {3     for(;;)4     {5         int ch = getchar();6         if(ch != ‘\n‘ && ch != ‘\t‘)    return ch;7     }8 }

 

另外,Printcodes()函數本身對題目沒有要求,但是對於調試代碼是有用的。

UVA - 213 Message Decoding (輸入字串並對單個字元進行操作的輸入輸出)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.