hdu 1736 美观化文字
模拟,不过好多地方要注意。
对于每一行,左右双引号要重新计算,有可能有全角的双引号,也要算进去。
代码如下:
1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <algorithm> 5 6 using namespace std; 7 8 int main() { 9 char str[2][3]; 10 strcpy(str[0], "“"); 11 strcpy(str[1], "”"); 12 // freopen("in", "r", stdin); 13 char ch = getchar(); 14 bool L = true; 15 while (ch != EOF) { 16 if (ch == '\n') L = true; 17 if (ch == ',') printf(","); 18 else if (ch == '.') printf("。"); 19 else if (ch == '!') printf("!"); 20 else if (ch == str[0][0]) { 21 ch = getchar(); 22 if (ch == str[0][1]) { 23 if (L) printf("“"); 24 else printf("”"); 25 L = !L; 26 } else { 27 putchar(str[0][0]); 28 continue; 29 } 30 } else if (ch == str[1][0]) { 31 ch = getchar(); 32 if (ch == str[1][1]) { 33 if (L) printf("“"); 34 else printf("”"); 35 L = !L; 36 } else { 37 putchar(str[1][0]); 38 continue; 39 } 40 } else if (ch == '\"') { 41 if (L) printf("“"); 42 else printf("”"); 43 L = !L; 44 } else if (ch == '<') { 45 ch = getchar(); 46 if (ch == '<') printf("《"); 47 else { 48 putchar('<'); 49 continue; 50 } 51 } else if (ch == '>') { 52 ch = getchar(); 53 if (ch == '>') printf("》"); 54 else { 55 putchar('>'); 56 continue; 57 } 58 } else if (ch == '?') printf("?"); 59 else putchar(ch); 60 ch = getchar(); 61 } 62 return 0; 63 }
——written by Lyon