计蒜客 墙壁涂色
题目:
https://www.jisuanke.com/course/2291/182250
思路:
来自https://blog.csdn.net/a1097304791/article/details/81191256
递推,画图得type[1]=3,type[2]=6,type[3]=6,n从4开始,
①当第1项和第n-1项不同色时。第n项只有一种颜色可以取,即type[n]=typef[n-1]
②当第1项和第n-1项同色时。第n项有两种颜色可以取,同时因为第n块和第n-1块不同色,所以type[n]=2*type[n-2]
相加得type[n] =type[n-1] + type[n-2]*2
1 #include<iostream> 2 #include<cstdio> 3 #include<cstdlib> 4 #include<cstring> 5 #include<queue> 6 using namespace std; 7 8 int n; 9 long long type[55]; 10 11 void work() 12 { 13 type[1]=3; 14 type[2]=6; 15 type[3]=6; 16 for(int i=4;i<=50;i++) 17 { 18 type[i]=type[i-2]*2+type[i-1]; 19 } 20 } 21 22 int main() 23 { 24 work(); 25 while(scanf("%d",&n)==1) 26 { 27 printf("%lld\n",type[n]); 28 } 29 return 0; 30 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步