利用递归的遍历式算法
缺点很明显:很大的运算量(无法减少),以及由此导致的超长运算时间。
优点:冬天可以跑一跑当暖手宝。
//
// main.cpp
//
// Common Test Emilia
//
// Created by Emilia on 2016/11/8
// Copyright © 2016年 Emilia. All rights reserved.
//
#include
#include
int process(int len, char *in);
int cyc(int sta, int *cord, int mid);
int main(void)
{
char input[50] = { 0 };
int i = 0, length;
int output;
while (input[i] = getchar())
{
if (input[0] == '0')
return 0;
if (input[i] == '\n')
{
length = i;
output = process(length, input);
printf("%d\n", output);
for (i = 0; length > 0; input[--length] = 0);
}
else
i++;
}
return 0;
}
int process(int len, char *in)
{
int rec[50] = { 0 };
int re = 1, num = 0;
for (int i = 0; i < len - 1; i++)
{
if (in[i] == '1' && in[i + 1] != '0')
rec[i] = 1;
else if (in[i] == '0')
for (int t = i - 1;t < len - 1; t++)
in[t] = in[t + 2];
else if (in[i] == '2')
if (in[i + 1] <= '6' && in[i + 1] != '0')
rec[i] = 1;
}
len = strlen(in);
for (int t = 0; t < 50; t++)
num += rec[t];
for (int t = 1;t <= num;t++)
{
re += cyc(0, rec, t);
}
return re;
}
int cyc(int sta, int *cord, int mid)
{
int num = 0;
if (mid > 1)
{
mid--;
for (int t = sta;t < 50;t++)
{
if (cord[t] == 1)
{
sta = t + 2;
num += cyc(sta, cord, mid);
}
}
return num;
}
else
{
for (int t = sta; t < 50; t++)
num += cord[t];
return num;
}
}
结合了斐波切那思想的由后至前的递归算法
缺点:很大的重复运算量(可以减少),以及由此导致的超长运算时间。
优点:冬天可以跑一跑当暖手宝,但没有Ver 0.1效果好。
//
// main.cpp
//
// Common Test Emilia
//
// Created by Emilia on 2016/11/8
// Copyright © 2016年 Emilia. All rights reserved.
//
#include
#include
int process(int len, char* in);
int main(void)
{
char input[50] = { 0 };
int i = 0, length;
int output;
while (input[i] = getchar())
{
if (input[0] == '0')
return 0;
if (input[i] == '\n')
{
length = i;
output = process(length, input);
printf("%d\n", output);
for (i = 0; length > 0; input[--length] = 0);
}
else
i++;
}
return 0;
}
int process(int len, char* in)
{
int num = 0, lim;
lim = (in[len - 2] - '0') * 10 + in[len - 1] - '0';
if (len >= 2 && lim < 27)
{
num += process(len - 2, in);
}
else
num += 0;
if (len > 1 && in[len - 1] != '0')
{
num += process(len - 1, in);
}
else
num += 0;
if (len <= 1)
num += 1;
return num;
}
结合了斐波切那思想的由前至后的递归算法
优点:解决了Ver 0.2的重复运算问题(用数组储存了所有运算结果),以及由此而来的快速运算。
缺点:不能当暖手宝了。
//
// main.cpp
//
// Common Test Emilia
//
// Created by Emilia on 2016/11/8
// Copyright © 2016年 Emilia. All rights reserved.
//
#include
#include
int main(void)
{
char input[6000] = { 0 };
int i = 0, length;
long long result[6001] = { 1, 1 };
while (input[i] = getchar())
{
if (input[0] == '0')
return 0;
if (input[i] == '\n')
{
length = i;
for (int t = 1; t < length; t++)
{
if (input[t - 1] == '1' || input[t - 1] == '2' && input[t] <= '6')
{
result[t + 1] += result[t - 1];
}
if (input[t] != '0')
{
result[t + 1] += result[t];
}
}
printf("%lld\n", result[length]);
for (i = 1; i < 5001; i++, result[i] = 0);
for (i = 0; length > 0; input[--length] = 0);
}
else
i++;
}
return 0;
}
由此可见一个好的算法有多么重要:p
SAMPLE INPUT
11111111111
1111111111111111111111
111111111111111111111111111111111
11111111111111111111111111111111111111111111
0
SAMPLE OUTPUT
89
10946
1346269
165580141
Time Cost
Line 1
Ver 0.1: <1ms
Ver 0.2: <1ms
Ver 1.0: <1ms
Line 2
Ver 0.1: 21ms
Ver 0.2: 1ms
Ver 1.0: <1ms
Line 3
Ver 0.1: 2153ms
Ver 0.2: 78ms
Ver 1.0: <1ms
Line 4
Ver 0.1: 233207ms
Ver 0.2: 9042ms
Ver 1.0: <1ms
Line 5 (input: 1x50)
Ver 1.0: <1ms
Line 6 (input: 1x60)
Ver 1.0: <1ms
Line 7 (input: 1x70)
Ver 1.0: <1ms
Line 8 (input: 1x80)
Ver 1.0: <1ms
Line 9 (input: 1x90)
Ver 1.0: <1ms
Platform:
CPU: Intel(R)Core(TM)i7-4710HQ @ 2.50GHz(Turbo on)
Memory: 16G
ONLY FOR RECORD
Copyright © 2016年 Emilia. All rights reserved.