hdu 1019 Least Common Multiple
来源:互联网 发布:淘宝发布宝贝没有规格 编辑:程序博客网 时间:2024/05/21 07:15
WA了好几发,原因是越界。
原来的处理方法是用long long避免越界,思路是对的,但在处理过程中出现了小差错。
例子:
int a,b;long long chengji;chengji=a*b;
这种写法是错的,因为在右边两个int相乘时就越界了。
正确写法是
int a,b;long long chengji;chengji=(long long)a*(long long)b;
或者也可以C语言点滴上介绍的简单防止溢出的方法:
int a,b,gcd,lcm;lcm=a/gcd*b;
下面贴代码
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;int gcd(int x,int y){ if(y==0) return x; else return(gcd(y,x%y));}int lcm(int x,int y){ long long chengji; chengji=(long long)x*(long long)y; return(int)(chengji/gcd(x,y));}int main(void){ int m,n,x,ans=1; scanf("%d",&m); for(int i=0;i<m;i++) { ans=1; scanf("%d",&n); if(n==0) continue; while(n--) { scanf("%d",&x); ans=lcm(ans,x); } if(i!=0) printf("%d\n",ans); else printf("%d\n",ans); } return 0;}
0 0
- HDU 1019 Least Common Multiple
- hdu 1019 Least Common Multiple
- HDU-1019 least common multiple
- HDU 1019 Least Common Multiple
- HDU 1019 Least Common Multiple
- HDU 1019 Least Common Multiple
- HDU 1019 Least Common Multiple
- hdu 1019Least Common Multiple
- hdu 1019 Least Common Multiple
- hdu 1019 Least Common Multiple
- Hdu 1019 Least Common Multiple
- hdu 1019 Least Common Multiple
- HDU 1019Least Common Multiple
- hdu 1019 Least Common Multiple
- hdu 1019 Least Common Multiple
- hdu 1019 Least Common Multiple
- HDU 1019 Least Common Multiple
- hdu 1019 Least Common Multiple
- contacts(通讯录)数据库
- PowerDesigner和Navicat for mysql的基础使用心得
- java打印二维数组
- Perl语言学习(Perl安装+编辑第一个程序)
- 关闭 linux下的umask( )函数、setsid( )函数
- hdu 1019 Least Common Multiple
- MySQL之——mysql 1449 : The user specified as a definer ('root'@'%') does not exist 解决方法
- bzoj 2705: [SDOI2012]Longge的问题
- 如何避免由于使用Handler导致的内存泄露
- 锐捷能够连接上,但是上不了网问题解决。
- Asp.net core mvc里面怎么添加全局的Filter
- sqlite "replace into"的用法
- matlab---调用.exe文件,批量写入读入
- MySql三表联查(Spring+SpringMVC+MyBatis)