链接到问题: https ://www.codewars.com/kata/54d496788776e49e6b00052f/train/cpp
伙计们,你能帮我弄清楚为什么我的解决方案在 Visual Studio 中运行良好,但在 Codewars 中却不行吗?
Codewars中的错误如下:
UndefinedBehaviorSanitizer:DEADLYSIGNAL
==1==ERROR: UndefinedBehaviorSanitizer: SEGV on unknown address 0x000000000000 (pc 0x000000425dce bp 0x000000000000 sp 0x7ffca477d200 T1)
==1==The signal is caused by a READ memory access.
==1==Hint: address points to the zero page.
==1==WARNING: invalid path to external symbolizer!
==1==WARNING: Failed to use and restart external symbolizer!
#0 0x425dcd (/workspace/test+0x425dcd)
#1 0x425a0a (/workspace/test+0x425a0a)
#2 0x42b951 (/workspace/test+0x42b951)
#3 0x429b5e (/workspace/test+0x429b5e)
#4 0x42958d (/workspace/test+0x42958d)
#5 0x42927b (/workspace/test+0x42927b)
#6 0x42e235 (/workspace/test+0x42e235)
#7 0x4287fd (/workspace/test+0x4287fd)
#8 0x7f833e965bf6 (/lib/x86_64-linux-gnu/libc.so.6+0x21bf6)
#9 0x404a29 (/workspace/test+0x404a29)
UndefinedBehaviorSanitizer can not provide additional info.
==1==ABORTING
*在 Codewars 中,测试运行良好,但尝试没有,计算都是正确的,但是当它进入第 3 题时,错误发生了,它不再继续解决问题。
我在 Codewars 中使用的代码:
#include <iostream>
#include <vector>
#include <string>
#include <tuple>
#include <chrono>
#include <thread>
//Sum by Factors(Finished)
//====================
using namespace std;
class SumOfDivided
{
vector<int> SuitablePrimeArray;
string getSumOfNumbersByPrimeFactorAndMakeBracket(int PrimeNumber, vector<int>& I);//for createBrackets
tuple<int, vector<int>> findSmallestNumberInArrayAndDeleteIt(vector<int> Numbers);//for sortAscendingly
public:
int findLargestNumberInArray(vector<int> Numbers);
vector<int> findPrimeNumbersTill(int End);
vector<int> findPrimeFactorsFromArray(vector<int> PrimeArr, vector<int> Numbers);
void printVectorArray(vector<int> array);
string executeKata(vector<int> Numbers);
string createBrackets(vector<int> FinalPrimeArr, vector<int>& Numbers);
vector<int> sortAscendingly(vector<int> input);
vector<int> generateRandomVector(int NumberCount, int minimum, int maximum);
int generateRandomNumber(int minimum, int maximum);
static string sumOfDivided(vector<int> &lst);
};
string SumOfDivided::sumOfDivided(vector<int> &lst){
SumOfDivided test;
return test.executeKata(lst);
}
string SumOfDivided::executeKata(vector<int> Numbers) {
cout << "The array: " << endl;
printVectorArray(Numbers);
cout << endl;
int TheLargestNumber = findLargestNumberInArray(Numbers);
cout << "The largest number from this array: " << endl << TheLargestNumber << endl;
cout << endl;
cout << "All the prime factors of this array: " << endl;
//printVectorArray(findFactorsFromArray(findPrimeNumbersTill(TheLargestNumber), Numbers));
SuitablePrimeArray = findPrimeFactorsFromArray(findPrimeNumbersTill(TheLargestNumber), Numbers);
printVectorArray(SuitablePrimeArray);
cout << endl;
string Answer = createBrackets(SuitablePrimeArray, Numbers);
cout << "Answer for the 4 kyu Kata(Sum by Factors): " << endl << Answer << endl;
return Answer;
}
int SumOfDivided::findLargestNumberInArray(vector<int> Numbers) {
int largest, i, pos;
largest = Numbers[0];
for (i = 1; i < Numbers.size(); i++) {
if (Numbers[i] > largest) {
largest = Numbers[i];
pos = i;
}
}
return largest;
}
vector<int> SumOfDivided::findPrimeNumbersTill(int End) {
vector<int> PrimeArr;
//PrimeArr.reserve(50);
int position = 0;
int n = 2;
while (position < End) {
bool isPrime = true;
for (int i = 2; i <= n / 2; ++i) {
if (n % i == 0) {
isPrime = false; break;
}
}
//if (isPrime){PrimeArr[position] = n; position++;}
if (isPrime) {
PrimeArr.push_back(n);
position++;
}
n++;
}
/*
for (int i = 0; i < position; i++) {
cout << PrimeArr[i] << ",";
}
*/
//printVectorArray(PrimeArr);
//cout << endl;
return PrimeArr;
}
vector<int> SumOfDivided::findPrimeFactorsFromArray(vector<int> PrimeArr, vector<int> Numbers) {
/*
for (int i = 0; i < PrimeArr.size(); i++) {
cout << PrimeArr[i] << ", ";
}
*/
vector<int> FinalPrimeFactorArray;
int position = 0;
for (int i = 0; i < Numbers.size(); i++) {
for (int j = 0; j < PrimeArr.size(); j++) {
if (Numbers[i] % PrimeArr[j] == 0 && find(FinalPrimeFactorArray.begin(), FinalPrimeFactorArray.end(), PrimeArr[j]) == FinalPrimeFactorArray.end()) {
FinalPrimeFactorArray.push_back(PrimeArr[j]); position++;
}
}
}
return sortAscendingly(FinalPrimeFactorArray);;
}
void SumOfDivided::printVectorArray(vector<int> array) {
for (int i = 0; i < array.size(); i++) {
cout << array[i] << ",";
}
cout << endl;
}
//=======================the code below is from part of my codewar unfinished scripts
string SumOfDivided::createBrackets(vector<int> FinalPrimeArr, vector<int>& Numbers) {
string Answer;
vector<int> I;
for (int i = 0; i < Numbers.size(); i++) {
I.push_back(0);
I[i] = Numbers[i];
//cout << "push back x" << i + 1 << endl;
}
for (int i = 0; i < FinalPrimeArr.size(); i++) {
Answer += getSumOfNumbersByPrimeFactorAndMakeBracket(FinalPrimeArr[i], I);
}
return Answer;
}
string SumOfDivided::getSumOfNumbersByPrimeFactorAndMakeBracket(int PrimeNumber, vector<int>& I) {
int Sum = 0;
for (int i = 0; i < I.size(); i++) {
//add this number to the total if the current number is divisible by the prime number
if (I[i] % PrimeNumber == 0)Sum += I[i];
}
//cout << "The sum of numbers divisible by the prime number " << PrimeNumber << " is " << Sum << endl;
string temp = "(" + to_string(PrimeNumber) + " " + to_string(Sum) + ")";
//cout << temp << endl;
return temp;
}
//=====================SORT ASCENDINGLY
tuple<int, vector<int>> SumOfDivided::findSmallestNumberInArrayAndDeleteIt(vector<int> Numbers) {
int smallest, i, pos(0);
smallest = Numbers[0];
for (i = 1; i < Numbers.size(); i++) {
if (Numbers[i] < smallest) {
smallest = Numbers[i];
pos = i;
}
}
Numbers.erase(Numbers.begin() + pos);
return make_tuple(smallest, Numbers);
}
vector<int> SumOfDivided::sortAscendingly(vector<int> input) {
vector<int> output;
int CurrentLargestNumber;
int End = input.size();
for (int i = 0; i < End; i++) {
tie(CurrentLargestNumber, input) = findSmallestNumberInArrayAndDeleteIt(input);
//cout << CurrentLargestNumber << ", ";
output.push_back(CurrentLargestNumber);
}
return output;
}
//=============================RANDOM GENERATION
vector<int> SumOfDivided::generateRandomVector(int NumberCount, int minimum, int maximum) {
vector<int> vecRandomValues;
int i = 0, randValue = 0;
srand(time(NULL));
while (i < NumberCount) {
randValue = rand() % (maximum - minimum + 1) + minimum;
vecRandomValues.push_back(randValue);
i++;
}
return vecRandomValues;
}
int SumOfDivided::generateRandomNumber(int minimum, int maximum) {
return rand() % (maximum - minimum + 1) + minimum;
}
*和我在Visual Studio中的代码,基本一样,只是用随机数生成随机测试,我可以在10秒内运行5000次,没有出现错误。
我在 Visual Studio 中使用的代码:
//Sum by Factors(unFinished)
//====================
#include <iostream>
#include <vector>
#include <string>
#include <Windows.h>
#include "RandomNumber.h"
#include "Bubblesort.h"
/*
Sum by prime factors:
example:
list of number: 12, 15
answer: (2, 12), (3, 27), (5, 15)
explaination for generating answers:
( (all prime factors from the list of numbers in ascending order),(sum of all the nominators of this prime factor on the left) )
*/
using namespace std;
class SumOfDivided
{
vector<int> SuitablePrimeArray;
string getSumOfNumbersByPrimeFactorAndMakeBracket(int PrimeNumber, vector<int>& I);//for createBrackets
public:
int findLargestNumberInArray(vector<int> Numbers);
vector<int> findPrimeNumbersTill(int End);
vector<int> findPrimeFactorsFromArray(vector<int> PrimeArr, vector<int> Numbers);
void printVectorArray(vector<int> array);
string executeKata(vector<int> Numbers);
string createBrackets(vector<int> FinalPrimeArr, vector<int>& Numbers);
};
int main() {
vector<SumOfDivided> list;
for (int i = 0; i < 5000; i++) {
SumOfDivided test;
list.push_back(test);
cout << "attempt " << i + 1 << endl;
test.executeKata(RandomNumber::generateRandomVector(RandomNumber::generateRandomNumber(2, 10), 100, 1000));
cout << "=========================================" << endl;
cout << endl;
}
/*
for (int i = 0; i < 10; i++) {
SumOfDivided* test = new SumOfDivided;
RandomNumber* a = new RandomNumber;
cout << "attempt " << i + 1 << endl;
test->executeKata(a->generateRandomVector(a->generateRandomNumber(2, 10), 100, 1000));
cout << "=========================================" << endl;
cout << endl;
//delete test, a;
}
*/
//vector<vector<int>> Numbers = { { 12,15,24,18,2525 }, { 44,787,84,75,255,7543,373,6746,473 }, { 360,777,689,426,999 } };
//test.executeKata(Numbers[0]);
////std::this_thread::sleep_for(std::chrono::milliseconds(1000));
//cout << "============" << endl;
//test.executeKata(Numbers[1]);
////std::this_thread::sleep_for(std::chrono::milliseconds(1000));
//cout << "============" << endl;
//test.executeKata(Numbers[2]);
}
string SumOfDivided::executeKata(vector<int> Numbers) {
cout << "The array: " << endl;
printVectorArray(Numbers);
cout << endl;
int TheLargestNumber = findLargestNumberInArray(Numbers);
cout << "The largest number from this array: " << endl << TheLargestNumber << endl;
cout << endl;
cout << "All the prime factors of this array: " << endl;
//printVectorArray(findFactorsFromArray(findPrimeNumbersTill(TheLargestNumber), Numbers));
SuitablePrimeArray = findPrimeFactorsFromArray(findPrimeNumbersTill(TheLargestNumber), Numbers);
printVectorArray(SuitablePrimeArray);
cout << endl;
string Answer = createBrackets(SuitablePrimeArray, Numbers);
cout << "Answer for the 4 kyu Kata(Sum by Factors): " << endl << Answer << endl;
return Answer;
}
int SumOfDivided::findLargestNumberInArray(vector<int> Numbers) {
int largest;
unsigned i, pos;
largest = Numbers[0];
for (i = 1; i < Numbers.size(); i++) {
if (Numbers[i] > largest) {
largest = Numbers[i];
pos = i;
}
}
return largest;
}
vector<int> SumOfDivided::findPrimeNumbersTill(int End) {
vector<int> PrimeArr;
//PrimeArr.reserve(50);
int position = 0;
int n = 2;
while (position < End) {
bool isPrime = true;
for (int i = 2; i <= n / 2; ++i) {
if (n % i == 0) {
isPrime = false; break;
}
}
//if (isPrime){PrimeArr[position] = n; position++;}
if (isPrime) {
PrimeArr.push_back(n);
position++;
}
n++;
}
/*
for (int i = 0; i < position; i++) {
cout << PrimeArr[i] << ",";
}
*/
//printVectorArray(PrimeArr);
//cout << endl;
return PrimeArr;
}
vector<int> SumOfDivided::findPrimeFactorsFromArray(vector<int> PrimeArr, vector<int> Numbers) {
/*
for (int i = 0; i < PrimeArr.size(); i++) {
cout << PrimeArr[i] << ", ";
}
*/
vector<int> FinalPrimeFactorArray;
unsigned position = 0;
for (unsigned i = 0; i < Numbers.size(); i++) {
for (unsigned j = 0; j < PrimeArr.size(); j++) {
if (Numbers[i] % PrimeArr[j] == 0 && find(FinalPrimeFactorArray.begin(), FinalPrimeFactorArray.end(), PrimeArr[j]) == FinalPrimeFactorArray.end()) {
FinalPrimeFactorArray.push_back(PrimeArr[j]); position++;
}
}
}
return Bubblesort::sortAscendingly(FinalPrimeFactorArray);;
}
void SumOfDivided::printVectorArray(vector<int> array) {
for (unsigned i = 0; i < array.size(); i++) {
cout << array[i] << ",";
}
cout << endl;
}
//=======================the code below is from part of my codewar unfinished scripts
string SumOfDivided::createBrackets(vector<int> FinalPrimeArr, vector<int>& Numbers) {
string Answer;
vector<int> I;
for (unsigned i = 0; i < Numbers.size(); i++) {
I.push_back(0);
I[i] = Numbers[i];
//cout << "push back x" << i + 1 << endl;
}
for (unsigned i = 0; i < FinalPrimeArr.size(); i++) {
Answer += getSumOfNumbersByPrimeFactorAndMakeBracket(FinalPrimeArr[i], I);
}
return Answer;
}
string SumOfDivided::getSumOfNumbersByPrimeFactorAndMakeBracket(int PrimeNumber, vector<int>& I) {
int Sum = 0;
for (unsigned i = 0; i < I.size(); i++) {
//add this number to the total if the current number is divisible by the prime number
if (I[i] % PrimeNumber == 0)Sum += I[i];
}
//cout << "The sum of numbers divisible by the prime number " << PrimeNumber << " is " << Sum << endl;
string temp = "(" + to_string(PrimeNumber) + " " + to_string(Sum) + ")";
//cout << temp << endl;
return temp;
}
*最后,当我检查 MSI 加力时,即使连续解决 10000 个解决方案也不会使用太多内存,并且它保持稳定,不会上升,所以我认为它不是内存不足的代码战 :)