空输出在蛮力任务上

时间:2018-07-12 14:25:18

标签: c++ c++11

我正在执行蛮力任务,但是当我运行程序时,它会提供一个空的输出文件。有人可以帮我解决这个问题吗?问题说明如下,其后是我的代码。

问题陈述:

编写一个读取两个数字的程序(以10为基数表示): N(1 <= N <= 15) S(0

代码

#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;
    fin >> n >> start;
    vector<int> finals;
    int times = 0;
    for (int i = start + 1; i <= 10000; i++)
    {
        if (times == n)
        {
            for (auto x : finals)
            {
                fout << 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;
}

1 个答案:

答案 0 :(得分:2)

尝试此代码。我做了一些更改。

#include <iostream>
#include <sstream>
#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;

    ostringstream str1;    /*this and the next commented lines are added because "to_string" didnt work in my compiler*/

    while (quo > 0)
    {
        quo = num / base;
        rem = num % base;
        str1 << rem;    //this
        to_reverse += str1.str();    //and this
        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;
    fout.open("dualpal.out", ios::out);   //open the file in binary mode
    //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)
        {

            //just a simple iterator for vector
            for (vector<int>::iterator it = finals.begin(); it != finals.end(); ++it)
            {
                fout << *it << "\n";
            }
            break;
        }
        else
        {
            for (int j = 2; j <= 10; j++)
            {
                if(is_pal(convert(i, j)) == true)
                {
                    times++;
                    finals.push_back(i);
                }
            }
        }
    }
    fout.close();   //close the file
    return 0;
}

使用STRING流

int number = 1000;
ostringstream s;
s << number;
string str = s.str();

此方法可用于将数字转换为字符串。

此代码需要<sstream>头文件。