我下面的程序在蛮力任务中抛出了 bad_alloc。我相信它是由我的功能中的某些东西引起的,但我无法找出是什么原因造成的。有人可以帮我找出导致此错误的原因吗?问题陈述在下面,然后我的代码在下面。
问题陈述 编写一个程序,读取两个数(以 10 为基数): N (1 <= N <= 15) S (0 < S < 10000) 然后找到并打印(以 10 为基数)前 N 个严格大于的数比 S 写成两个或多个数字基数(2 <= base <= 10)时是回文的。此问题的解决方案不需要处理大于标准 32 位的整数。
代码
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <cmath>
#include <fstream>
using namespace std;
string convert(int num, int base)
{
int quo = 100000;
int rem = 0;
string to_reverse;
while (quo > 0)
{
quo = num / base;
rem = num % base;
to_reverse += to_string(rem);
num /= base;
}
reverse(to_reverse.begin(), to_reverse.end());
return to_reverse;
}
bool is_pal(string conv_num)
{
string reversed_conv_num = conv_num;
reverse(reversed_conv_num.begin(), reversed_conv_num.end());
if (reversed_conv_num == conv_num)
{
return true;
}
return false;
}
int main()
{
ofstream fout("dualpal.out");
ifstream fin("dualpal.in");
int n, start;
cin >> n >> start;
vector<int> finals;
int times = 0;
for (int i = start + 1; i <= 10000; i++)
{
if (times == n)
{
for (auto x : finals)
{
cout << x << "\n";
}
break;
}
else
{
for (int j = 2; j <= 10; j++)
{
if(is_pal(convert(i, j)) == true)
{
times++;
finals.push_back(i);
}
}
}
}
return 0;
}