WERTYU - UVA 10082 - Virtual Judgehttps://vjudge.net/problem/UVA-10082两种方法,第一种紫皮书上的,第二种网上的,感觉网上的更简单
#includeusing namespace std; char s[] = "`1234567890-=QWERTYUIOP[]\ASDFGHJKL;'ZXCVBNM,./"; int main () { int i, c; while ((c = getchar()) != EOF) { for (i = 1; s[i] && s[i] != c; i++); //检查字符在字符串s中是否存在,并定位他的位置 if (s[i])//就是判断字符存不存在于字符串s里,不存在的话s[i]就为' '即false //或者if(i<=strlen(s)-1) putchar(s[i - 1]); else//不存在直接输出 putchar(c); } }
s[i]是什么意思:
s是一个指针变量,如果你不了解什么是指针
他等价于一个数组
即char *s="1234567890-=qwertyuiop[]asdfghjkl;'\zxcvbnm,./";
等价于 char s[]="1234567890-=qwertyuiop[]asdfghjkl;'\zxcvbnm,./";
s[i]就相当于取数组里相对应的元素的值
有什么情况s[i]会出现false:
因为字符串以' '(ascii是0,而0为false)结束,也就是说当a[i]==' '就为false
#include#include using namespace std; char s[] = "`1234567890-=QWERTYUIOP[]\ASDFGHJKL;'ZXCVBNM,./"; int main() { char c; while ((c = getchar()) != EOF) { if (c == ' ') printf(" "); else if (c == 'n') printf("n"); else { for (int i = 0; i < strlen(s); i++) if (c == s[i]) { printf("%c", s[i - 1]); break; } } } }



