用动态规划写完了发现是找规律的题,还是把代码发上来把,顺带问一句,如果用动态规划的话这道题是不是不能用一维数组
#include<stdio.h>
int main(){
int number;
while( scanf("%d", &number) != EOF){
int all = number*2-1;
int **dp = (int*)malloc(sizeof(int*)*number);
for(int i=0; i<number; i++){
dp[i] = (int*)malloc(sizeof(int)*all);
memset(dp[i], 0, all*sizeof(int));
}
dp[0][all/2]=1;
for(int i =1; i<number; i++){
for(int j = 0; j<all; j++){
if(j==0){
dp[i][j]=dp[i-1][j]+dp[i-1][j+1];
}
else if(j==all-1){
dp[i][j]=dp[i-1][j-1]+dp[i-1][j];
}
else{
dp[i][j]=dp[i-1][j-1]+dp[i-1][j]+dp[i-1][j+1];
}
}
}
int sign = 0;
for(int i=0; i<all; i++){
if(dp[number-1][i]%2==0){
printf("%d\n", i+1);
sign = 1;
break;
}
}
if(sign==0)printf("-1\n");
}
} int main(){
int number;
while( scanf("%d", &number) != EOF){
if(number<=2){
printf("-1\n");
}
else{
int x = (number-3)%4;
switch(x){
case 0:printf("2\n");
break;
case 1:printf("3\n");
break;
case 2:printf("2\n");
break;
case 3:printf("4\n");
break;
}
}
}
}