일부 알고리즘을 테스트하려고하는데 시간이가는 동안 너무 오래 걸리는 경우 (정확하게하려면 60 초) 중지하고 싶습니다. 나는 시계 기능으로 주변을 수리하려고 시도했지만 멈추고 다음 시험으로 넘어갈 수는없는 것 같습니다. 나는 isUnique 함수 자체를 편집하지 않고 이것을하고 싶다. 작업을 처음부터 타이밍을 정하고 60 초가 경과하면 중지하는 방법이 있습니까? 여기에 지금까지 프로그램 .. 효과와 순환, 그 괜찮아요 그, 그 그런 식으로 있어야하기 때문에설정된 시간이 지난 후 기능을 중지하고 다음 함수로 이동
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <ctime>
#include <chrono>
#include <cstdlib>
#include <random>
#include <algorithm>
using namespace std;
bool isUnique(const vector<int>& arr, int start, int end) {
if (start >= end) return true;
if (!isUnique(arr, start, end - 1))
return false;
if (!isUnique(arr, start + 1, end))
return false;
return (arr[start] != arr[end]);
}
bool isUniqueLoop(const vector<int>& arr, int start, int end) {
if (start >= end) return true;
for (int i = start; i < end; i++)
for (int j = i + 1; j <= end; j++)
if (arr[i] == arr[j])return false;
return true;
}
bool isUniqueSort(const vector<int>& arr, int start, int end) {
if (start <= end) return true;
vector<int> buf(arr);
sort(buf.begin() + start, buf.begin() + end);
for (int i = start; i < end; i++)
if (buf[i] == buf[i + 1]) return false;
return true;
}
int main() {
int max = 0;
cout << "Enter a number for the Max range: ";
cin >> max;
default_random_engine randGen(time(0));
uniform_int_distribution<int> randNum(0, max);
int i;
int j;
int n = randNum(randGen);
int m = n;
double timeout = 60.0;
vector<int> myVect;
for (i = 0; i <= m; i++) {
myVect.push_back(randNum(randGen));
//cout << myVect[i] << endl;
}
cout << "Recursive Algorithm Test... " << endl;
cout << endl;
// recursive algorithm
clock_t start = clock();
isUnique(myVect, 0, m);
if (isUnique(myVect, 0, m) == true) {
cout << "The Vector is Unique! " << endl;
}
else {
cout << "The Vector is not Unique! " << endl;
}
clock_t end = clock();
double time = (double)(end - start)/CLOCKS_PER_SEC * 1000.0;
cout << "CPU Time used for this algorithm: " << time << " ms" << endl;
if (time > 60000) {
cout << "This function takes too long! " << endl;
}
cout << "------------------------------------" << endl;
cout << "Iterative Algorithm Test... " << endl;
cout << endl;
// iterative algorithm
clock_t start2 = clock();
isUniqueLoop(myVect, 0, n);
if (isUniqueLoop(myVect, 0, n) == true) {
cout << "The Vector is Unique! " << endl;
}
else {
cout << "The Vector is not Unique! " << endl;
}
clock_t end2 = clock();
double time2 = (double)(end2 - start2)/CLOCKS_PER_SEC * 1000.0;
cout << "CPU time used for this algorithm: " << time2 << " ms. " << endl;
if (time2 > 60000) {
cout << "This function takes too long! " << endl;
}
cout << "------------------------------------" << endl;
cout << "Sort Algorithm Test... " << endl;
cout << endl;
// sort algorithm
clock_t start3 = clock();
isUniqueSort(myVect, 0, n);
if (isUniqueSort(myVect, 0, n) == true) {
cout << "The Vector is Unique! " << endl;
}
else {
cout << "The Vector is not Unique " << endl;
}
clock_t end3 = clock();
double time3 = (double)(end3 - start3)/CLOCKS_PER_SEC * 1000.0;
cout << "CPU time used for this algorithm: " << time3 << " ms. " << endl;
if (time3 > 60000) {
cout << "This function takes too long! " << endl;
}
cout << endl;
system("pause");
return 0;
첫 isUnique에() 함수는 항상 오래 걸립니다이다. 그러나, 나는 특정 기능을 종료하는 방법을 모르고 너무 오래 걸리면 다음으로 이동합니다. 멍청한 게시물에 대한 죄송합니다. 어떤 제안?
무엇이 문제입니까? – amit
알고리즘을 사용하여 1 분 이내에 알고리즘이 실행될 수 있도록 n의 가장 큰 값을 찾는 방법은 무엇입니까? –
(1) 다양한 값의 'n'을 시도하십시오. (2) "1 분 또는 그 이하를 실행합니다"는 약간의 기계 (및 컴파일러) 특정 ... 012 초에 대한 – amit