UVa10082通往AC的两大坑

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;
}

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值