因为是填空题,没有时间限制,因此我们可以直接暴力枚举得出答案
上代码
#include<iostream>
#include<cstring>
#include<algorithm>
#include<string>
using namespace std;
const int N = 100000000;
int main(void)
{
int ans = 0;
for (int i = 1; i <= N; i++) {
string s = to_string(i);
if (s.size() % 2 == 0) {//如果是偶数位
int a = 0, b = 0;
for (int i = 0; i < s.size() / 2; i++) {
a += s[i]; b += s[s.size() - 1 - i];
}
if (a == b) {
ans++;
}
}
}
cout << ans << endl;
return 0;
}
由于时间复杂度很大,因此得需要一段时间才能出答案
由于没有时间限制,因此我们可以用dfs暴搜,由于每道题只有答对与答错两种,并且当答题题数多于30或者得分为100时候直接返回,当得分为70并且答题题数为30时候直接记录答案即可
上代码
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#define int long long//开longlong防止爆掉
using namespace std;
int ans = 0;
void dfs(int score, int x)//分别表示得分和第x道题
{
if (score == 100 || x > 30) return; //如果得分到100或者回答完所有题目,不符合条件,就返回
if (score == 70 && x == 30) {//如果回答完时候是70分,答案加1
ans++;
return;
}
dfs(score + 10, x + 1);//答对
dfs(0, x + 1);//答错
}
signed main(void)
{
dfs(0, 0);
cout << ans << endl;
return 0;
}
答案如图