Codeforces Round #146 (Div. 2) C. LCM Challenge

本文分享了一道来自Codeforces的有趣题目及其解决方案。该题要求找出三个数的最大最小公倍数(LCM),通过使用辗转相除法求最大公约数(GCD)来辅助计算。对于大于3的情况,采用迭代的方式寻找可能的最大LCM。

http://codeforces.com/contest/236/problem/C

    觉得这题挺有意思,贴一下代码。if(a<=0)or(b<=0)or(c<=0)then continue;这句一开始没写,WA了。注意多个数的LCM求法。

Codeprogram LCMchallenge;
uses math;
var i,j,k:longint;
    n,a,b,c,ans,temp:int64;
function GCD(a,b:int64):int64;
         var r:int64;
         begin
         while a mod b<>0 do begin
           r:=a mod b;
           a:=b;b:=r;
           end;
         GCD:=b;
         end;
function LCM(a,b:int64):int64;
begin  LCM:=a div GCD(a,b) *b;end;
begin
readln(n);ans:=0;
case n of
  1:ans:=1;
  2:ans:=2;
  3:ans:=6;
  else begin
       if(n and 1)=1
         then ans:=n*(n-1)*(n-2)
         else begin
              for i:=0 to 50 do
                for j:=i+1 to 50 do
                  for k:=j+1 to 50 do
                    begin
                    a:=n-i;b:=n-j;c:=n-k;
                    if(a<=0)or(b<=0)or(c<=0)then continue;
                    ans:=max(ans,LCM(LCM(a,b),c));
                    end;
              end;
       end;
  end;
writeln(ans); readln;
end.

转载于:https://www.cnblogs.com/lijianlin1995/archive/2012/10/23/2735795.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值