2017-12-29 41 views
2

私は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; 
} 

答えて

3

12桁がintが通常どのように表現されるかである32ビットの数、に収まるように長すぎる:これは私のコードです。したがって、>>を使用してその番号を読み取ると失敗し、cin >> inputがfalse値に変換され、ループが終了します。

障害モードの処理の詳細については、operator >> documentationを参照してください。

+0

ありがとうございました!私は見つけることができた最大の演算子、この場合は 'unsigned long long int'を使ってみました。最大の桁数は最大20になりましたので、今できる最善の方法だと思います。それは膨大な数であり、とにかく多くの数字が必要になるとは思わない。 –

+1

@DalvAlzar - 'long long unsigned'は' 18446744073709551615'より大きい値をサポートできるとは保証されていません。それは19桁の数字が大丈夫だが、20桁ではないことを意味します(20桁の値のすべてが表現可能であるとは限りません)。 – Peter

1

あなたが使用タイプで表すことができ、ベース10の最大桁数を取得することができstd::numeric_limits::digits10定数:

std::cout << std::numeric_limits<int>::digits10 << '\n'; 

チャンスはタイプintのための最大有効桁数が9である、とあなたが標準入力から12を供給しようとしています。プログラムがクラッシュしない場合、(cin >> input)の条件は単にfalseと評価されます。

関連する問題