五:斐波那契数列
代码
#include <iostream>
#include <cstring>
using namespace std;
typedef long long ll;
const int N = 10010;
ll a[N];
int n;
int main()
{
cin >> n;
a[1] = 1;
a[2] = 1;
for(int i = 3; i <= n + 1; i++)
{
a[i] = a[i - 1] + a[i - 2];
// cout << i << " " << a[i] << endl;
}
cout << a[n + 1] << endl;
return 0;
}
六:代金券组合
搜索写法(40%)
代码
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
const int N = 10010;
int a[N];
int n, p;
int ans = 0x3f3f3f3f;
bool f;
void dfs(int r, int num)
{
if(num >= ans) return;
if(r == 0)
{
ans = min(ans, num);
}
if(r < 0)
return;
for(int i = 1; i <= n; i++)
{
dfs(r - a[i], num + 1);
}
return;
}
int main()
{
while(cin >> p && p)
{
f = false;
cin >> n;
for(int i = 1; i <= n; i++)
cin >> a[i];
sort(a + 1, a + n + 1);
reverse(a + 1, a + 1 + n);
dfs(p, 0);
}
cout <<ans << endl;
return 0;
}
DP代码 完全背包
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
const int N = 10010;
int a[N];
int n, p;
int f[N];
int main()
{
while(cin >> p && p)
{
cin >> n;
for(int i = 1; i <= n; i++)
cin >> a[i];
memset(f, 0x3f, sizeof f);
f[0] = 0;
for(int i = 1; i <= n; i++)
for(int j = a[i]; j <= p; j++)
{
f[j] = min(f[j], f[j - a[i]] + 1);
}
if(f[p] > 0x3f3f3f3f / 2)
cout << "Impossible" << endl;
else cout << f[p] << endl;
}
return 0;
}
七:迷宫寻路
代码
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
const int N = 110;
int a[N][N];
int f[N][N];
int n, m;
int main()
{
cin >> n >> m;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
cin >>a[i][j];
memset(f, 0x3f, sizeof f);
f[1][1] = a[1][1];
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
{
if(i == 1 && j == 1) continue;
f[i][j] = min(f[i - 1][j], f[i][j - 1]);
f[i][j] += a[i][j];
}
cout << f[n][m] << endl;
return 0;
}
八:星际穿越
代码
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
int a[10][10][10];
bool vis[10][10][10];
int n;
int xx, yy, zz, maxx = 0;
int ans = 0;
void dfs(int x, int y, int z, int num)
{
ans = max(ans, num);
int fx = x - 1;
int fy = y;
int fz = z;
if (fx >= 0 && fx < n && !vis[fx][fy][fz] && a[fx][fy][fz] < a[x][y][z])
{
vis[fx][fy][fz] = true;
dfs(fx, fy, fz, num + a[fx][fy][fz]);
vis[fx][fy][fz] = false;
}
fx = x + 1;
fy = y;
fz = z;
if (fx >= 0 && fx < n && !vis[fx][fy][fz] && a[fx][fy][fz] < a[x][y][z])
{
vis[fx][fy][fz] = true;
dfs(fx, fy, fz, num + a[fx][fy][fz]);
vis[fx][fy][fz] = false;
}
fx = x;
fy = y - 1;
fz = z;
if (fy >= 0 && fy < n && !vis[fx][fy][fz] && a[fx][fy][fz] < a[x][y][z])
{
vis[fx][fy][fz] = true;
dfs(fx, fy, fz, num + a[fx][fy][fz]);
vis[fx][fy][fz] = false;
}
fx = x;
fy = y + 1;
fz = z;
if (fy >= 0 && fy < n && !vis[fx][fy][fz] && a[fx][fy][fz] < a[x][y][z])
{
vis[fx][fy][fz] = true;
dfs(fx, fy, fz, num + a[fx][fy][fz]);
vis[fx][fy][fz] = false;
}
fx = x;
fy = y;
fz = z - 1;
if (fz >= 0 && fz < n && !vis[fx][fy][fz] && a[fx][fy][fz] < a[x][y][z])
{
vis[fx][fy][fz] = true;
dfs(fx, fy, fz, num + a[fx][fy][fz]);
vis[fx][fy][fz] = false;
}
fx = x;
fy = y;
fz = z + 1;
if (fz >= 0 && fz < n && !vis[fx][fy][fz] && a[fx][fy][fz] < a[x][y][z])
{
vis[fx][fy][fz] = true;
dfs(fx, fy, fz, num + a[fx][fy][fz]);
vis[fx][fy][fz] = false;
}
}
int main()
{
cin >> n;
for (int i = 1; i <= n * n * n; i++)
{
int x, y, z;
cin >> x >> y >> z;
int p;
cin >> p;
a[x][y][z] = p;
if (p > maxx)
{
xx = x;
yy = y;
zz = z;
maxx = p;
}
}
vis[xx][yy][zz] = true;
dfs(xx, yy, zz, maxx);
cout << ans << endl;
return 0;
}