chenkuiyu @ 2025-01-11 18:22:56
请各为大老看看为啥输出0:
#include <bits/stdc++.h>
using namespace std;
int a,b,sum;
int sss(int x,int y)
{
int z;
while(y!=0){
z=x%y;
x=y;
y=z;
}
return x;
}
int main() {
cin>>a>>b;
for(int i=b;i<=a;i++){
if(b%a%i==0&&sss(i,a*b/i)==b){
sum++;
}
}
cout<<sum;
return 0;
}
by nieqiuran @ 2025-01-11 18:34:45
@chenkuiyu
很显然,没进入你判定的条件啊,sum的值自然没有改变所以输出0
有点弄不明白你的思路
by be_the_person @ 2025-01-11 18:35:22
@chenkuiyu AC代码
#include <bits/stdc++.h>
using namespace std;
//最大公约数
int gcd(int x, int y) {
return y ? gcd(y, x % y) : x;
}
//最小公倍数
int lcm(int x, int y) {
return y / gcd(x, y) * x; //注意顺序,防止乘法爆int
}
int cnt;
int x, y;
int main() {
//y的范围1e6,双重循环妥妥的TLE
cin >> x >> y;
//双重循环,傻查法,最基本,最朴素
for (int i = x; i <= y; i++)
for (int j = x; j <= y; j++)
if (gcd(i, j) == x && lcm(i, j) == y) cnt++;
cout << cnt << endl;
return 0;
}
by jiguanlin @ 2025-01-11 18:35:30
16行a,b反了,b比a大
by _299817_ @ 2025-01-11 18:37:00
我帮你改了改,你自己对着查一下吧
#include <bits/stdc++.h>
using namespace std;
int a,b,sum;
int sss(int x,int y)
{
int z;
while(y!=0){
z=x%y;
x=y;
y=z;
}
return x;
}
int main() {
cin>>a>>b;
for(int i=a;i<=b;i++){
if((b*a)%i==0&&sss(i,a*b/i)==a){
sum++;
}
}
cout<<sum;
return 0;
}
by jiguanlin @ 2025-01-11 18:38:13
你方法不太对。 这题考的是两数积=两数的最大公因数和最小公倍数的积。你要先枚举积,再统计数量。
by jiguanlin @ 2025-01-11 18:45:13
注意数据范围,这题会爆int
by chenkuiyu @ 2025-01-11 19:44:21
谢谢各位大佬!!!谢谢