Skip to content

Commit a4b67b8

Browse files
committedAug 25, 2013
aug25
1 parent bbda187 commit a4b67b8

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
 

‎walking_robot.txt

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
Glitch is a walking robort moves in a peculiar problem: it takes x steps forward , then x+1 steps backward, then 2x steps forward, x+2 steps backward,3x steps forward x+3 steps backward , and so on... until it has taken y steps,glitch turns 180 degrees before continuning with its pattern . write a program that prompts x and y and total number of steps taken and outputs how many steps away from its starting point
3+
*/
4+
5+
#include <iostream>
6+
using namespace std;
7+
8+
int main(){
9+
int x,y;
10+
cout<<"input x:"<<endl;
11+
cin>>x;
12+
cout<<"input y"<<endl;
13+
cin>>y;
14+
if (x<1||y<1){
15+
cout<<"wrong input:"<<endl;
16+
return -1;
17+
}
18+
19+
int location=0, i=1;
20+
while(i<=y){
21+
location += i*x;
22+
location -= (i+x);
23+
cout<<"at the "<<i<<"-th step, we are at location: "<<location<<endl;
24+
i++;
25+
}
26+
cout<<"The final location is: "<<location<<endl;
27+
return 0;
28+
}
29+
30+

0 commit comments

Comments
 (0)