
版权声明: https://blog.csdn.net/Alibaba_lhl/article/details/79964222
·LEFT:往左移动一个单位
·RIGHT: 往右移动一个单位
·SAME AS i: 和第i 条执行相同的动作。输入保证i 是一个正整数,且不超过之前执行指令数
Input输入第一行为数据组数T (T<=100)。每组数据第一行为整数n (1<=n<=100),即指令条数。以下每行一条指令。指令按照输入顺序编号为1~n。
Output对于每组数据,输出机器人的最终位置。每处理完一组数据,机器人应复位到数轴原点。
Sample Input2 3 LEFT RIGHT SAME AS 2 5 LEFT SAME AS 1 SAME AS 2 SAME AS 1 SAME AS 4Sample Output
1
-5
- 比赛时因为这道题WA了好多次,但就是找不到BUG。后来也没找到。
- 对于字符串输入的问题,要多注意吸收字符等细节问题。可以学学STL中的string ,处理字符串方便一些。
- 通常数组 如果有大于1000的范围 ,写在main函数的外面。(原因:全局变量在静态存储区分配内存,局部变量是在栈上分配内存空间的。(c语言程序在运行时会动态创建一个堆栈段,里面存放着调用栈,保存着函数的调用关系和局部变量。)如果数组太大,可能会造成栈溢出。
- 下面是未AC的 Code
#include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; int main() { int T; scanf("%d",&T); while(T--) { int n; scanf("%d",&n); int a[n+1]; memset(a,0,sizeof(int) * (n+1)); char c; for(int i=1;i<=n;i++) { char ch[15]; if(i==1) c = getchar(); gets(ch); int w = strlen(ch); if(ch[0] == 'L') a[i] = -1; else if(ch[0] == 'R') a[i] = 1; else if(w==9) { int b = ch[8] - '0'; a[i] = a[b]; } else if(w==10) { int b = (ch[8] - '0') * 10 + ch[9] - '0'; a[i] = a[b]; } else if(w==11) { int b = (ch[8] - '0') * 100 + (ch[9] - '0') * 10 + (ch[10] - '0') ; a[i] = a[b]; } } int sum = 0; for(int i=1;i<=n;i++) { // printf("%d ",a[i]); sum = sum + a[i]; } printf("%d\n",sum); } return 0; }
- 下面是AC的 Code
#include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; int a[105]; //通常数组 如果有大于1000的范围 ,写在main函数的外面 int main() { int T; scanf("%d",&T); while(T--) { int n; scanf("%d",&n); // int a[n+1]; 不要这样定义,以防出现不必要的麻烦 memset(a,0,sizeof(int) * (n+1)); char c; for(int i=1;i<=n;i++) { char str1[15]; scanf("%s",str1); if(str1[0] == 'L') a[i] = -1; else if(str1[0] == 'R') a[i] = 1; else { char str2[5]; scanf("%s",str2); int x; scanf("%d",&x); a[i] = a[x]; } } int sum = 0; for(int i=1;i<=n;i++) { // printf("%d ",a[i]); sum = sum + a[i]; } printf("%d\n",sum); } return 0; }
- 上一篇 n^n的末位数字(快速幂取模)
- 下一篇 H-N!的位数(斯特林公式)
查看评论