套汇问题 Floyd
问题:套汇。
思路:Floyd
代码:
1 #include <cstdio> 2 #include <cstdlib> 3 #include <ctime> 4 5 #define N 10 6 7 int currencyArbitrage (double rates[][N]); 8 9 int main (int argc, char **argv) 10 { 11 double R[N][N]; 12 srand(time(NULL)); 13 for (int i = 0; i < N; i++) 14 { 15 for (int j = 0; j < N; j++) 16 { 17 R[i][j] = ((rand() % N) + 1) * 1.0 / N; 18 } 19 } 20 R[1][2] = 1; 21 R[2][6] = 0.9; 22 R[6][1] = 1.2; 23 24 printf("%d\n",currencyArbitrage(R)); 25 return 0; 26 } 27 28 int currencyArbitrage (double rates[][N]) 29 { 30 for (int k = 0; k < N; k++) 31 for (int i = 0; i < N; i++) 32 for (int j = 0; j < N; j++) 33 { 34 double tmp = rates[i][k] * rates[k][j]; 35 rates[i][j] = (tmp > rates[i][j]) ? tmp : rates[i][j]; 36 if ((i != j) && (rates[i][j] * rates[j][i] > 1)) 37 { 38 return 1; 39 } 40 } 41 return 0; 42 }
时间复杂度:O(N^3)