ZOJ 3952 Fibonacci Sequence Chicken Edition

来源:互联网 发布:linux 查找隐藏文件 编辑:程序博客网 时间:2024/05/29 19:15

Year 2017 is the year of chicken, so in this problem we introduce you an interesting programming language: the Chicken Language.

Chicken is an esoteric programming language by Torbjörn Söderstedt, in which “chicken” is the only valid symbol. It is inspired by the paper and the presentation presented at the AAAS humor session by Doug Zongke. We strongly recommend you to watch the presentation after the contest. It’s super fun.

As the original chicken language is a bit complicated, we specially designed the Simplified Chicken Language (SCL) for this problem. An SCL program is consisted of only two kinds of tokens: “c” and new line. The number of “c” tokens in the same line corresponds to an opcode. As the program is executed, it will push/pop values to/from a stack (the stack is empty at the beginning). The opcodes and the descriptions for each instruction of SCL are listed below.

Please keep in mind that the stack of the SCL is 1-based. That is to say, the index of the bottom of the stack is considered to be 1. In the following table, we indicate the integer at the top of the stack as x, and the integer just below the top of the stack as y. We also indicate stack[n] as the integer in the stack whose index is n.

Opcode Instruction Description 1 Add Pop x and y, and push x + y back to the stack. If there are less than two integers in the stack, the program will crash. 2 Subtract Pop x and y, and push x - y back to the stack. If there are less than two integers in the stack, the program will crash. 3 Compare Pop x and y. If x equals to y then push 1 onto the stack, otherwise push 0 onto the stack. If there are less than two integers in the stack, the program will crash. 4 Load Load one integer from the standard input and push it onto the stack. If there is nothing to load, the program will crash. 5 Copy Pop x and y, and copy stack[y] to stack[x]. If there are less than two integers in the stack, the program will crash. If x or y is out of the range of the current stack, the program will also crash. 6 Jump Pop x and y. If y is not zero, the program will jump to the x-th line and continue to run. If x is larger than the number of lines of the program, the program will stop. If there are less than two integers in the stack, the program will crash. If x is not a positive integer, the program will also crash. 7+ Push If the Opcode is larger or equal to 7, push the integer Opcode - 7 onto the stack.

When the program stops, it will print out the integer at the top of the stack as the output.

What you need to do is to write an SCL program which can print out the n-th element of the fibonacci sequence. Recall that a fibonacci sequence is a sequence which satisfies f(1) = f(2) = 1 and f(n) = f(n - 1) + f(n - 2) when n ≥ 3.

As the online judge system of the contest does not support SCL, you’re supposed to print out your SCL program using other languages like C or C++. A specially designed program will then judge the correctness of your output.

解题思路

感觉此题题意就是一个大坑,一直看得云里雾里的。简单可以理解为要求利用 C/C++/etc. 打印出一份满足 Simplified Chicken Language (SCL) 的程序,该 SCL 程序要求能够处理:对于询问 n (1n30),给出第 n 项斐波那契数。

我所编写的 SCL 程序处理逻辑:

  • stack 1~30 的位置全部使用 1 (即 8 个 c ) 作为占位格,用于保存斐波那契前 30 项。
  • stack 31,32 作为处理用的占位格,利用 Opcode 7 5 ,将斐波那契第 i-1 项和第 i-2 项分别复制到 stack 31,32 的位置,使用 Opcode 1 处理, stack 31 保存的即为斐波那契第 i 项的结果。
  • 再将 stack 31 位置第 i 项的结果利用Opcode 7, 5 复制回 stack i 的位置。
  • 循环处理出 30 项的结果

代码

#include<bits/stdc++.h>using namespace std;const char c = 'c';int main(){    for(int i=1;i<=30;i++)        cout<<string(8, c)<<endl;    int idx = 30;    for(int i=3;i<=30;i++)    {        if(i == 3)            cout<<string(8, c)<<endl;        cout<<string(8, c)<<endl;        //x = 31,  y = i-2;        cout<<string(i-2+7, c)<<endl<<string(31+7, c)<<endl;        //stack[x] = stack[y];        cout<<string(5, c)<<endl;        //x = 32,  y = i-1;        cout<<string(i-1+7, c)<<endl<<string(32+7, c)<<endl;        //stack[x] = stack[y];        cout<<string(5, c)<<endl;        //stack[31] = stack[31] + stack[32];        cout<<string(1, c)<<endl;        //x = i,  y = 31;        cout<<string(31+7, c)<<endl<<string(i+7, c)<<endl;        //stack[x] = stack[y];        cout<<string(5, c)<<endl;    }    // load y = n    cout<<string(4, c)<<endl;    // x = 31    cout<<string(31+7, c)<<endl;    // stack[x] = stack[y]    cout<<string(5, c)<<endl;}
0 0
原创粉丝点击