50209. Keyboard
難度:3.5/5 Used Time: 9:38
寫出第五行的char arr跟dx dy是關鍵。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char arr[3][10] = {"qwertyuiop",
"asdfghjkl" ,
"zxcvbnm" };
int len[3] = {10, 9, 7};
int dx[6] = {0, -1, -1, 0, 1, 1};
int dy[6] = {1, 1, 0, -1, -1, 0};
int is_valid(int x, int y){
return (x == 0 || x == 1 || x == 2) && y >= 0 && y < len[x];
}
int main(){
char c;
scanf("%c", &c); printf("%c\n", c);
int x, y;
for(int i = 0; i < 3; i++){
for(int j = 0; j < len[i]; j++){
if(arr[i][j] == c){
x = i, y = j;
break;
}
}
}
int q;
while(scanf("%d", &q) != EOF){
int new_x = x + dx[q], new_y = y + dy[q];
if(is_valid(new_x, new_y)){
x = new_x, y = new_y;
printf("%c\n", arr[x][y]);
}
}
}