杭电oj平台上的11页题目代码:acm编程题:hdu1018,hdu1019

//1018
//思路:求这个阶乘结果的位数,因为是阶乘结果(乘积)取对数,就相当于对每个对数求和
/* 123456=1.23456*10^5;
log10(123456) = 5.09151;
log10(1.23456 * 10 ^ 5) = log10(1.23456) + log10(10 ^ 5) = 0.09151 + 5;
故int(log10(n)) + 1 就是n的位数*/
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int n, test, i, ans;
double t;
cin >> test;
while (test--)
{
cin >> n;
t = 0;
//乘积取对数相当于对数求和
for ( i = 2; i <= n; i++)
{
t += log10(i*1.0);
}
ans = int(t) + 1;
cout << ans << endl;
}

return 0;
}

 

 

//1019
//思路:先求出其最大公约数,再根据lcm=a*b/gcd求得最小公倍数
#include<iostream>
#include<cmath>
using namespace std;
//递归求最大公约数
int gcd(int a ,int b)
{
if (a%b==0)
{
return b;
}
return gcd(b, a%b);
}
//两数之积除以gcd就是lcm
int lcm(int a, int b)
{
int temp;
//确保a比b小
if (a>b)
{
temp = a;
a = b;
b = temp;
}
int g = gcd(a, b);
return (a*b / g);
}
int main()
{
int test;
cin >> test;
int n;
int i;
int m;
while (test--)
{
cin >> n;
cin >> m;
int l = lcm(1, m);
for (i = 1; i < n; i++)
{
cin >> m;
l = lcm(l, m);
}
cout << l << endl;
}

return 0;
}

 

参考博客:https://www.cnblogs.com/zhourongqing/archive/2012/05/07/2487430.html

转载于:https://www.cnblogs.com/BlueBlue-Sky/p/8530246.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值