Created
September 24, 2014 15:30
-
-
Save ywjno/d67f89f8c8e68b776952 to your computer and use it in GitHub Desktop.
coursera_[Computer Programming]_homework2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#define SPEED 10 | |
// 北:0,东:1,南:2,西:3 | |
int path = 0; | |
int x = 0; | |
int y = 0; | |
int getDistance(int time) { | |
return SPEED * time; | |
} | |
void firstLeave(int time) { | |
y += getDistance(time); | |
} | |
void left(int time) { | |
switch (path) { | |
case 0: | |
x -= getDistance(time); | |
path = 3; | |
break; | |
case 1: | |
y += getDistance(time); | |
path = 0; | |
break; | |
case 2: | |
x += getDistance(time); | |
path = 1; | |
break; | |
case 3: | |
y -= getDistance(time); | |
path = 2; | |
break; | |
default : | |
break; | |
} | |
} | |
void right(int time) { | |
switch (path) { | |
case 0: | |
x += getDistance(time); | |
path = 1; | |
break; | |
case 1: | |
y -= getDistance(time); | |
path = 2; | |
break; | |
case 2: | |
x -= getDistance(time); | |
path = 3; | |
break; | |
case 3: | |
y += getDistance(time); | |
path = 0; | |
break; | |
default : | |
break; | |
} | |
} | |
int main(void) { | |
// Question 1 | |
// int input[26]; | |
// input[0] = 0; | |
// input[1] = 1; | |
// input[2] = 1; | |
// input[3] = 1; | |
// input[4] = 2; | |
// input[5] = 1; | |
// input[6] = 3; | |
// input[7] = 1; | |
// input[8] = 4; | |
// input[9] = 1; | |
// input[10] = 5; | |
// input[11] = 1; | |
// input[12] = 6; | |
// input[13] = 1; | |
// input[14] = 7; | |
// input[15] = 1; | |
// input[16] = 8; | |
// input[17] = 1; | |
// input[18] = 9; | |
// input[19] = 1; | |
// input[20] = 10; | |
// input[21] = 1; | |
// input[22] = 11; | |
// input[23] = 1; | |
// input[24] = 12; | |
// input[25] = 3; | |
// Question 2 | |
// int input[12]; | |
// input[0] = 3; | |
// input[1] = 1; | |
// input[2] = 8; | |
// input[3] = 2; | |
// input[4] = 8; | |
// input[5] = 2; | |
// input[6] = 10; | |
// input[7] = 2; | |
// input[8] = 15; | |
// input[9] = 1; | |
// input[10] = 20; | |
// input[11] = 3; | |
// Question 3 | |
// int input[24]; | |
// input[0] = 2; | |
// input[1] = 2; | |
// input[2] = 5; | |
// input[3] = 1; | |
// input[4] = 8; | |
// input[5] = 2; | |
// input[6] = 10; | |
// input[7] = 2; | |
// input[8] = 13; | |
// input[9] = 2; | |
// input[10] = 18; | |
// input[11] = 2; | |
// input[12] = 22; | |
// input[13] = 1; | |
// input[14] = 23; | |
// input[15] = 2; | |
// input[16] = 29; | |
// input[17] = 2; | |
// input[18] = 33; | |
// input[19] = 1; | |
// input[20] = 35; | |
// input[21] = 1; | |
// input[22] = 50; | |
// input[23] = 3; | |
// Question 4 | |
// int input[28]; | |
// input[0] = 0; | |
// input[1] = 2; | |
// input[2] = 0; | |
// input[3] = 2; | |
// input[4] = 0; | |
// input[5] = 2; | |
// input[6] = 0; | |
// input[7] = 1; | |
// input[8] = 0; | |
// input[9] = 2; | |
// input[10] = 1; | |
// input[11] = 2; | |
// input[12] = 2; | |
// input[13] = 2; | |
// input[14] = 4; | |
// input[15] = 2; | |
// input[16] = 7; | |
// input[17] = 2; | |
// input[18] = 12; | |
// input[19] = 2; | |
// input[20] = 20; | |
// input[21] = 2; | |
// input[22] = 33; | |
// input[23] = 2; | |
// input[24] = 54; | |
// input[25] = 2; | |
// input[26] = 88; | |
// input[27] = 3; | |
// Question 5 | |
int input[2]; | |
input[0] = 5; | |
input[1] = 3; | |
// 第一个数不为0,表示朝北运动 | |
if (input[0] != 0) { | |
firstLeave(input[0]); | |
} | |
int i = 1; | |
while (i <= sizeof(input)/sizeof(int)) { | |
// printf("%d %d\n", x, y); | |
if (input[i] == 1) { | |
left(input[i+1] - input[i-1]); | |
} | |
if (input[i] == 2) { | |
right(input[i+1] - input[i-1]); | |
} | |
if (input[i] == 3) { | |
printf("%d %d\n", x, y); | |
return 0; | |
} | |
i += 2; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment