UVa10082通往AC的两大坑
【题目】
A common typing error is to place the hands on the keyboard one row to the right of the correct position. So ‘Q’ is typed as ‘W’ and ‘J’ is typed as ‘K’ and so on. You are to decode a message typed in this manner.
Input
Input consists of several lines of text. Each line may contain digits, spaces, upper case letters (except Q, A, Z), or punctuation shown above [except back-quote (‘)]. Keys labelled with words [Tab, BackSp,Control, etc.] are not represented in the input.
Output
You are to replace each letter or punction symbol by the one immediately to its left on the ‘QWERTY’ keyboard shown above. Spaces in the input should be echoed in the output.
Sample Input
O S, GOMR YPFSU/
Sample Output
I AM FINE TODAY.
【两坑分析】
1、"\"才能被计算机读入’\‘,单独一个’\',数组什么都不存。(这篇底稿也比看到的多打了一个呦~)
2、这个坑找了2小时!!就是不服气别人AC的代码和我的思路一样,为什么我AC不了。
WA的:
#include<stdio.h>
#include<string.h>
int main() {
int c;
char a[100] = "`1234567890-=QWERTYUIOP[]\\ASDFGHJKL;'ZXCVBNM,./";
while((c = getchar()) != EOF) {
if(c == ' ') {
printf(" ");
}
else{
for(int i = 0; i < 100; i++) {
if(a[i] == c) {
printf("%c",a[i-1]);
break;
}
}
}
}
return 0;
}
原因:我对空格的处理和别人不同。别人判断不是需左移的字符(即空格),就原样输出(putchar(c )),我判断是空格就输出空格,后来改成原样输出也不行,所以问题在于黑盒的所谓“空格”,不是键盘输入的空格,二者不相等。
返回去看了下题目,“Spaces in the input should be echoed in the output.”以后小心这个echo,重复,需要一模一样!
AC:
#include<stdio.h>
int main() {
char s[100] = "`1234567890-=QWERTYUIOP[]\\ASDFGHJKL;'ZXCVBNM,./";
int c;
while((c = getchar()) != EOF) {
for(int i = 0; i < 48; i++) { //从100改成48,更严谨,用i判断数组有用部分全部读完,防止剩余部分出现space
if(s[i] == c) {
printf("%c",s[i-1]);
break;
}
if(i == 47) { //47也是,一旦读完即为space,原样输出
printf("%c", c);
}
}
}
return 0;
}