私はCourseraのアルゴリズムについての講座を続けており、学習したことをコードに入れようとしました。これは「&征服アルゴリズム」アルゴリズムと考えられており、その部分が問題ないことを願っています。私はちょうどそれを使いこなす遭遇した問題がある:プログラムに12桁の数字を入力するまですべてうまくいく。私がそれをするとき、それはちょうどcinを終了し、すべての前の数字を並べ替えて出力します(数字が前にない場合は空白です)。できれば、間違いを見つけたら何が間違っているのか教えてください。12桁の数字を入力すると、次のコードがクラッシュするのはなぜですか?
#include "stdafx.h"
#include <iostream>
#include <vector>
using namespace std;
// setup global variable for the number of inversions needed
int inversions = 0;
// function to merge 2 sublists into 1 sorted list
vector<int> Merge_and_Count(vector<int>& split_lo, vector<int>& split_hi) {
// setup output variable -> merged, sorted list of the 2 input sublists
vector<int> out;
int l = 0;
int m = 0;
// loop through all the elements of the 2 sublists
for (size_t k = 0; k < split_lo.size() + split_hi.size(); k++) {
// check if we reached the end of the first sublist
if (l < split_lo.size()) {
// check if we reached the end of the second sublist
if (m < split_hi.size()) {
// check which element is smaller and sort accordingly
if (split_lo[l] < split_hi[m]) {
out.push_back(split_lo[l]);
l++;
}
else if (split_hi[m] < split_lo[l]) {
out.push_back(split_hi[m]);
m++;
inversions++;
}
}
else {
out.push_back(split_lo[l]);
l++;
inversions++;
}
}
else {
out.push_back(split_hi[m]);
m++;
}
}
return out;
}
// function that loops itself to split input into halves until it reaches the base case (1 element array)
vector<int> MergeSort_and_CountInversions(vector<int>& V) {
// if we reached the base case, terminate the loop and feed the output to the previous loop to be processed
if (V.size() == 1) return V;
// if we didn't reach the base case
else {
// continue halving the sublists
size_t const half_size = V.size()/2;
vector<int> split_lo(V.begin(), V.begin() + half_size);
vector<int> split_hi(V.begin() + half_size, V.end());
// feed them back into the loop
return Merge_and_Count(MergeSort_and_CountInversions(split_lo), MergeSort_and_CountInversions(split_hi));
}
}
// main function of the app, runs everything
int main()
{
// setup main variables
int input;
vector<int> V;
// get input
cout << "Enter your numbers to be sorted (enter Y when you wish to proceed to the sorting)." << endl;
cout << "Note: do NOT use duplicates (for example, do not input 1 and 1 again)!" << endl;
while (cin >> input)
V.push_back(input);
cout << "\nThe numbers you chose were: " << endl;
for (size_t i = 0; i < V.size(); i++)
cout << V[i] << " ";
// get sorted output
vector<int> sorted = MergeSort_and_CountInversions(V);
cout << "\n\nHere are your numbers sorted: " << endl;
for (size_t j = 0; j < sorted.size(); j++)
cout << sorted[j] << " ";
// show number of inversions that were needed
cout << "\n\nThe number of inversions needed were: " << inversions << endl;
return 0;
}
ありがとうございました!私は見つけることができた最大の演算子、この場合は 'unsigned long long int'を使ってみました。最大の桁数は最大20になりましたので、今できる最善の方法だと思います。それは膨大な数であり、とにかく多くの数字が必要になるとは思わない。 –
@DalvAlzar - 'long long unsigned'は' 18446744073709551615'より大きい値をサポートできるとは保証されていません。それは19桁の数字が大丈夫だが、20桁ではないことを意味します(20桁の値のすべてが表現可能であるとは限りません)。 – Peter