2016-09-10 Problem Solving►UVa UVa 11827 - Maximum GCD Contents 1. Problem2. Solution3. Code Problem題目網址 找某一對數字其 GCD 為最大的。 Solution用 std::stringstream 來處理輸入,接著暴力做 GCD 找最大的即可。 CodeUVa 118271234567891011121314151617181920212223242526272829303132333435363738394041424344#include<cstdio>#include<iostream>#include<string>#include<sstream>using namespace std;inline int gcd(int a, int b){ while (b) { int temp = a%b; a = b; b = temp; } return a;}int main(){ int n, num[100]; string str; scanf("%d", &n); getchar(); while (n--) { int max = 0, i = 0, temp; getline(cin, str); stringstream ss(str); while (ss >> temp) num[i++] = temp; for (int j = 0; j < i; j++) for (int k = j + 1; k < i; k++) { int g = gcd(num[j], num[k]); max = max < g ? g : max; } printf("%d\n", max); } return 0;} Newer UVa 755 - 487--3279 Older Machine Learning Foundations - L6 Notes(下)