ZOJ 3952 Fibonacci Sequence Chicken Edition
2017-04-18 08:42
573 查看
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
as the integer in the stack whose index is n.
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.
我所编写的 SCL 程序处理逻辑:
stack 1~30 的位置全部使用 1 (即 8 个
stack 31,32 作为处理用的占位格,利用 Opcode
再将 stack 31 位置第
循环处理出 30 项的结果
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
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. |
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 (1≤n≤30),给出第 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; }
相关文章推荐
- ZOJ 3952 Fibonacci Sequence Chicken Edition(构造/模拟)
- ZOJ 3952 Fibonacci Sequence Chicken Edition 模拟
- ZOJ3952 Fibonacci Sequence Chicken Edition 【模拟】
- 17th浙大校赛 ZOJ3952 Fibonacci Sequence Chicken Edition【汇编】
- 【ZOJ3952 The 17th Zhejiang University Programming Contest E】【简单构造 模拟 or汇编】Fibonacci Sequence Chicken
- zoj Gao The Sequence
- UnicodeDecodeError: 'gbk' codec can't decode byte 0xfd in position 3952: illegal multibyte sequence
- ZOJ1276 Optimal Array Multiplication Sequence dp
- LeetCode - Fibonacci Sequence/Dynamic Programming - Climbing Stairs
- ZOJ 3892 Available Computation Sequence
- hoj 10511 、zoj 1962 ——100位数内的fibonacci
- zoj 1276 Optimal Array Multiplication Sequence
- ZOJ 1276 Optimal Array Multiplication Sequence
- A Problem With Fibonacci Sequence
- [zoj 3774]Power of Fibonacci 数论(二次剩余 拓展欧几里得 等比数列求和)
- UVA 348 & ZOJ 1276 Optimal Array Multiplication Sequence(dp , 矩阵链相乘问题)
- HDU 1021/ZOJ 2060 Fibonacci Again(数论&整除推导&位运算技巧)
- Sicily 1779 Fibonacci Sequence Multiplication
- ZOJ 1276 Optimal Array Multiplication Sequence
- uva 10689 - Yet another Number Sequence(缓存,类Fibonacci)