初始化方式1
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char ans[5005][5005] = {"1", "1"};
int main()
{
int n;
scanf("%d", &n);
for (int i = 2; i <= n; i++)
{
int lens1 = strlen(ans[i - 1]);
int lens2 = strlen(ans[i - 2]);
char ans1[1005] = "";
char ans2[1005] = "";
for (int j = 0; j < lens1; j++)
{
ans1[lens1 - j - 1] = ans[i - 1][j];
}
for (int j = 0; j < lens2; j++)
{
ans2[lens2 - j - 1] = ans[i - 2][j];
}
int lens = lens1 > lens2 ? lens1 : lens2;
char ans_tmp[5005] = "";
for (int j = 0; j < lens; j++)
{
if (ans1[j])
ans_tmp[j] += ans1[j] - '0';
if (ans2[j])
ans_tmp[j] += ans2[j] - '0';
ans_tmp[j] += '0';
if (ans_tmp[j] > '9')
{
ans_tmp[j] -= 10;
ans_tmp[j + 1] += 1;
}
}
if (ans_tmp[lens])
{
ans_tmp[lens++] += '0';
}
for (int j = 0; j < lens; j++)
{
ans[i][lens - j - 1] = ans_tmp[j];
}
}
printf("%s\n", ans[n]);
return 0;
}
这么写一直报ce,但本地没问题
然后我乱改了下,变成下面的初始化方式
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char ans[5005][5005];
int main()
{
int n;
scanf("%d", &n);
ans[0][0] = '1';
ans[1][0] = '1';
for (int i = 2; i <= n; i++)
{
int lens1 = strlen(ans[i - 1]);
int lens2 = strlen(ans[i - 2]);
char ans1[2005] = "";
char ans2[2005] = "";
for (int j = 0; j < lens1; j++)
{
ans1[lens1 - j - 1] = ans[i - 1][j];
}
for (int j = 0; j < lens2; j++)
{
ans2[lens2 - j - 1] = ans[i - 2][j];
}
int lens = lens1 > lens2 ? lens1 : lens2;
char ans_tmp[3005] = "";
for (int j = 0; j < lens; j++)
{
if (ans1[j])
ans_tmp[j] += ans1[j] - '0';
if (ans2[j])
ans_tmp[j] += ans2[j] - '0';
ans_tmp[j] += '0';
if (ans_tmp[j] > '9')
{
ans_tmp[j] -= 10;
ans_tmp[j + 1] += 1;
}
}
if (ans_tmp[lens])
{
ans_tmp[lens++] += '0';
}
for (int j = 0; j < lens; j++)
{
ans[i][lens - j - 1] = ans_tmp[j];
}
}
printf("%s\n", ans[n]);
return 0;
}
然后就过了,懵了,请求巨巨们帮蒟蒻解答一下