基准时间限制:1秒 空间限制:131072KB 分值:40
难度:4级算法题
如果n是斐波那契的项数就输出B,否则输出A,打表找的规律,附打表程序
#include<cstdio> using namespace std; long long a[1000][1000]; long long g[1000][1000]; long long fib[100] = {1,1}; int tot = 0; bool dfs(int k,int n){ if(n == 0) { return 0; } if(g[n][k] > 0) return a[n][k]; g[n][k] = 1; int f = 0; for(int i = 1; i <= k + k; i++){ if(i <= n) { if(dfs(i,n - i) == 0) f = 1; } } a[n][k] = f; return f; } int main(){ long long T,n; for(tot = 2; fib[tot - 1] + fib[tot - 2] <= 1e9; tot++){ fib[tot] = fib[tot - 1] + fib[tot - 2]; } /* printf("A "); for(int i = 1; i <= 100; i++){ int flag = 0; for(int j = 1; j < i; j++){ if(dfs(j,i - j) == 0) flag = 1; } if(flag == 0) printf("%d ",i); }*/ scanf("%lld",&T); while(T--){ scanf("%lld",&n); int flag = 0; for(int i = 1; i <= tot; i++){ if(fib[i] == n) flag = 1; } if(flag == 1) puts("B"); else puts("A"); } return 0; }原文链接:https://www.f2er.com/bash/391769.html