HDU2028Lowest Common Multiple Plus
生活随笔
收集整理的這篇文章主要介紹了
HDU2028Lowest Common Multiple Plus
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
需要注意的是,雖然最后輸出為32位整數,但是a*b可能會超出int范圍(最小公倍數=a*b/a和b最大公約數)
在此提供兩個寫法都可以AC:
| 123456789 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | #include <stdio.h> int gcd(int a,int b) {int temp;if(a<b){temp=a;a=b;b=temp;}if(b==0) return a;else return gcd(b,a%b); } int main() {int n,s;long long t,k;int i;while(scanf("%d",&n)!=EOF){t=0;k=1;for(i=1;i<=n;i++){scanf("%d",&s);t=s*k/gcd(s,k);//s*k可能超出int范圍 k=t; }printf("%lld\n",t);}return 0; } |
第二個就是把計算順序改下,先除再乘,即a*b/c==b/c*a,計算最大公約數的方法相同就不寫了
| 123456789 10 11 12 13 14 15 16 17 18 19 | int main() {int n,s;int t,k;int i;while(scanf("%d",&n)!=EOF){t=0;k=1;for(i=1;i<=n;i++){scanf("%d",&s);t=k/gcd(s,k)*s;k=t; }printf("%d\n",t);}return 0; } |
總結
以上是生活随笔為你收集整理的HDU2028Lowest Common Multiple Plus的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HDU2026 首字母变大写
- 下一篇: HDU2035人见人爱A^B(快速幂求余