• [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;
    }
    

      

  • 相关阅读:
    MySQL快速入门
    关系模型
    从Qt到PyQt
    Qt 绘图与动画系统
    Django请求响应对象
    Django控制器
    Django模板
    第一个Django项目
    图的存储与遍历
    AOE网与AOV网
  • 原文地址:https://www.cnblogs.com/lautsie/p/3904566.html
Copyright © 2020-2023  润新知