题目1079:手机键盘

#include
#include
char str[110];
int main() {
	while (scanf("%s", str) != EOF) {
		int len = strlen(str);
		int time = 0;
		int num1, num2, cnt;
		if (str[0] == 's') {
			num1 = (str[0] - 'a' - 1) / 3;
			cnt = (str[0] - 'a' - 1) % 3 + 2;
		} else if (str[0] > 's' && str[0] < 'z') {
			num1 = (str[0] - 'a' - 1) / 3;
			cnt = (str[0] - 'a' - 1) % 3 + 1;
		} else if (str[0] == 'z') {
			num1 = (str[0] - 'a' - 2) / 3;
			cnt = (str[0] - 'a' - 2) % 3 + 2;
		} else {
			num1 = (str[0] - 'a') / 3;
			cnt = (str[0] - 'a') % 3 + 1;
		}
		time += cnt;
		for (int i = 1; i < len; i++) {
			if (str[i] == 's') {
				num2 = (str[i] - 'a' - 1) / 3;
				cnt = (str[i] - 'a' - 1) % 3 + 2;
			} else if (str[i] > 's' && str[i] < 'z') {
				num2 = (str[i] - 'a' - 1) / 3;
				cnt = (str[i] - 'a' - 1) % 3 + 1;
			} else if (str[i] == 'z') {
				num2 = (str[i] - 'a' - 2) / 3;
				cnt = (str[i] - 'a' - 2) % 3 + 2;
			} else {
				num2 = (str[i] - 'a') / 3;
				cnt = (str[i] - 'a') % 3 + 1;
			}
			if (num1 == num2)
				time += 2 + cnt;
			else
				time += cnt;
			num1 = num2;
		}
		printf("%d\n", time);
	}
	return 0;
}

题目链接:

http://ac.jobdu.com/problem.php?pid=1079

你可能感兴趣的:(算法,九度OJ)