关于数组初始值的问题求解答
查看原帖
关于数组初始值的问题求解答
285414
Swiftie_wyc22楼主2022/12/14 12:13
#include <bits/stdc++.h>
#define rei register int
#define ll long long
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define rep(i, s, n, c) for (register int i = s; i <= n; i+=c)
#define repd(i, s, n, c) for (register int i = s; i >= n; i-=c)
#define CHECK cout<<"WALKED"<<endl;
inline int read(){int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0' && ch<='9')x=(x<<3)+(x<<1)+ch-'0',ch=getchar();return x*f;}
#define pb push_back
#define ls id<<1
#define rs id<<1|1
const int INF = INT_MAX;
long long binpow(long long a, long long b, ll mod){long long res = 1;  while (b > 0){if (b & 1) res = res * a % mod;a = a * a % mod;  b >>= 1;  }  return res;}

using namespace std;
int p[] = {0,2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199};
int main()
{
	int n;
	while (cin >> n) {
		int f[205];
		memset(f, 0, sizeof(f));
		f[0] = 1;
		rep (i, 1, 46, 1) {
			rep(j, p[i], n, 1) {
				f[j] += f[j - p[i]];
			}
		}
		cout << f[n] << endl;
	}
    return 0;
}

这是我的代码,过了,但我迷惑的一点是为什么赋f[0]=1?我一开始设的f[2]=1,后来试着试着就搞过来了。这个初始值怎么设?有原理嘛?求大佬解答谢谢!

2022/12/14 12:13
加载中...