Warning: session_start(): open(/tmp/sess_5404983616121b2e6c37293b71478a45, O_RDWR) failed: No space left on device (28) in /data/wiki/inc/init.php on line 239

Warning: session_start(): Failed to read session data: files (path: ) in /data/wiki/inc/init.php on line 239

Warning: Cannot modify header information - headers already sent by (output started at /data/wiki/inc/init.php:239) in /data/wiki/inc/auth.php on line 430

Warning: Cannot modify header information - headers already sent by (output started at /data/wiki/inc/init.php:239) in /data/wiki/inc/Action/Export.php on line 103

Warning: Cannot modify header information - headers already sent by (output started at /data/wiki/inc/init.php:239) in /data/wiki/inc/Action/Export.php on line 103

Warning: Cannot modify header information - headers already sent by (output started at /data/wiki/inc/init.php:239) in /data/wiki/inc/Action/Export.php on line 103
善用常量数组往往能简化代码。定义常量数组时无须指明大小,编译器会计算。 下面是两道例题 $WERTYU$($UVa10082$) #include char s[] = "`1234567890-=QWERTYUIOP[]\\ASDFGHJKL;'ZXCVBNM,./"; int main(){ int i, c; while((c = getchar()) != EOF){ for (i=1; s[i] && s[i]!=c;i++); if(s[i]) putchar(s[i-1]); else putchar(c); } return 0; } 回文词($UVa401$) 输入一个字符串,判断它是否为回文串以及镜像串。输入字符串保证不含数字0。(使用常量数组) #include #include #include const char* rev = "A 3 HIL JM O 2TUVWXY51SE Z 8 "; const char* msg[] = {"not a palindrome", "a regular palindrome", "a mirrored string", "a mirrored palindrome"}; char r(char ch) { if(isalpha(ch)) return rev[ch - 'A']; return rev[ch - '0' + 25]; } int main() { char s[30]; while(scanf("%s", s) == 1) { int len = strlen(s); int p = 1, m = 1; for(int i = 0; i < (len+1)/2; i++) { if(s[i] != s[len-1-i]) p = 0; if(r(s[i]) != s[len-1-i]) m = 0; } printf("%s -- is %s.\n\n", s, msg[m*2+p]); } return 0; }