Hangzhou Electric HDU ACM 2028 Lowest Common multiple Plus

Source: Internet
Author: User
Tags greatest common divisor

Lowest Common Multiple Plus Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 39183 Accepted Submission (s): 16144


Problem description to find the number of n least common multiple.
Input inputs contain multiple test instances, and each test instance starts with a positive integer n, followed by n positive integers.
Output outputs their least common multiple for each set of test data, with one row for each test instance output. You can assume that the final output is a 32-bit integer.
Sample Input
2 4 63 2 5 7

Sample Output
1270

Authorlcy Alas this question dozen heart lies right did not think WA so many times. A previous program error is a greatest common divisor that begins with a run error, dividing the total product by the number of N. Intermediate results may overflow. again the program error is a logic error, the thinking is wrong. Finally, the first two numbers of least common multiple are determined. Use this least common multiple and the next one to beg least common multiple, in turn ... but! This is the first way to be more troublesome. The second type: Find out the number of n the largest M, and then the number of times N to retrieve whether all of them divide it, otherwise m++; During the entire cycle If all the numbers cannot be integers at the same time, then the loop does not jump out, and K is the request when it jumps out. F:
#include <iostream> #include <algorithm>using namespace std;int gcd (int k,int b) {    if (!b) return  K; GCD (b,k%b);} int main () {int n;int *ls=new int [10000000];while (cin>>n) {for (int i=0;i<n;i++) {cin>>ls[i];} int g=ls[0],m=ls[0];for (int j=1;j<n;j++) {G=GCD (ls[j],m), if (m<m* (ls[j]/g)) m=m* (ls[j]/g);}    Cout<<m<<endl;} return 0;}

S:
#include <iostream> #include <algorithm>using namespace Std;int main () {int N;int ls[100000];while (cin> >n) {for (int m=0;m<n;m++) cin>>ls[m];sort (ls,ls+n);       int k=ls[n-1];for (int i=0;i<n;i++) {if (k%ls[i]!=0) {k++;i=-1;}} Cout<<k<<endl;} return 0;}

Hangzhou Electric HDU ACM 2028 Lowest Common multiple Plus

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.