这个问题在这里已有答案:
我一直在关注Coursera上的算法课程,我试着把我学到的东西放到代码中。这应该是一个“分而治之”的算法,我希望这部分是正常的。我有一个问题,我遇到它只是弄乱它:一切正常,直到我输入一个12位数的程序。当我这样做时,它只是结束cin并输出所有先前排序的数字(如果之前没有数字,则为空格)。如果可以的话,请告诉我如果发现错误有什么问题。这是我的代码:
#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;
}
12位十进制数字太长,无法容纳32位数字,这通常表示int
。因此,使用>>
读取该数字将失败,并且cin >> input
将转换为false值,从而终止循环。
有关处理故障模式的详细信息,请参阅operator >>
documentation。
您可以使用std::numeric_limits::digits10常量获取可由类型表示的最大基数为10的数字:
std::cout << std::numeric_limits<int>::digits10 << '\n';
有可能是int
类型的最大有效位数为9,并且您尝试通过标准输入提供12。程序不会崩溃,(cin >> input)
的条件只是评估为false
。
对于32位整数,12位数太多,尝试使用unsigned long long int
,检查这些限制:http://www.cplusplus.com/reference/climits/