[hackerrank]John and GCD list

https://www.hackerrank.com/contests/w8/challenges/john-and-gcd-list

简单题,GCD和LCM。

#include <vector>
#include <iostream>

using namespace std;

int gcd(int a, int b) {
	if (a % b == 0) {
		return b;
	} else {
		return gcd(b, a % b);
	}
}

int lcm(int a, int b) {
	return a * b / gcd(a, b);
}

int main() {
	int T;
	cin >> T;
	while (T--) {
		int N;
		cin >> N;
		vector<int> A(N);
		for (int i = 0; i < N; i++) {
			cin >> A[i];
		}
		vector<int> B(N + 1);
		for (int i = 1; i < N; i++) {
			B[i] = lcm(A[i], A[i - 1]);
		}
		B[0] = A[0];
		B[N] = A[N - 1];
		for (int i = 0; i <= N; i++) {
			cout << B[i] << " ";
		}
		cout << endl;
	}
	
	
    return 0;
}

  

posted @ 2014-08-11 13:46  阿牧遥  阅读(195)  评论(0编辑  收藏  举报