#24109: 顯示TLE


hisotensoku (胡錦掏出來)

學校 : 國立臺灣海洋大學
編號 : 96105
來源 : [123.194.157.248]
最後登入時間 :
2023-08-25 21:07:06
a158. 11827 - Maximum GCD -- UVa11827 | From: [123.194.156.153] | 發表日期 : 2021-01-20 00:55

測試執行都顯示沒問題

但是跑題目卻TLE

求解

#include<iostream>

#include<string>

#include<algorithm>

using namespace std;

int gcd(int a,int b) {

if (a == 0)

return b;

if (a > b)

return gcd(a % b, b);

else

return gcd(b, a);

}

int main() {

int num;

cin >> num;

cin.ignore();

for (int i = 0; i < num; i++) {

int A[100];

int count = 0;

string input2;

getline(cin,input2);

int temp = 0;

for (int j = 0; j < input2.length(); j++) {

if (input2[j] >= '0' && input2[j] <= '9') {

temp = temp * 10 + input2[j] - '0';

}

else {

A[count] = temp;

temp = 0;

count++;

}

}

A[count] = temp;

temp = 0;

count++;

int maxi = 1;

for (int j = 0; j < count; j++)

for (int k = j + 1; k < count; k++)

if (A[j] != 0 && A[k] != 0)

maxi = max(maxi, gcd(A[j], A[k]));

cout << maxi << endl;

}

}

 

 
#24141: Re:顯示TLE


fire5386 (becaidorz)

學校 : 國立清華大學
編號 : 115822
來源 : [140.114.217.8]
最後登入時間 :
2024-04-13 22:06:23
a158. 11827 - Maximum GCD -- UVa11827 | From: [61.230.48.164] | 發表日期 : 2021-01-22 19:13

測試執行都顯示沒問題

但是跑題目卻TLE

求解

#include

#include

#include

using namespace std;

int gcd(int a,int b) {

if (a == 0)

return b;

if (a > b)

return gcd(a % b, b);

else

return gcd(b, a);

}

int main() {

int num;

cin >> num;

cin.ignore();

for (int i = 0; i < num; i++) {

int A[100];

int count = 0;

string input2;

getline(cin,input2);

int temp = 0;

for (int j = 0; j < input2.length(); j++) {

if (input2[j] >= '0' && input2[j] <= '9') {

temp = temp * 10 + input2[j] - '0';

}

else {

A[count] = temp;

temp = 0;

count++;

}

}

A[count] = temp;

temp = 0;

count++;

int maxi = 1;

for (int j = 0; j < count; j++)

for (int k = j + 1; k < count; k++)

if (A[j] != 0 && A[k] != 0)

maxi = max(maxi, gcd(A[j], A[k]));

cout << maxi << endl;

}

}

 

會TLE代表測資的數字很多的時候會超過時間限制喔!可能是演算法的部分不夠快,可以參考其他解題報告的演算法

 
ZeroJudge Forum