蓝桥杯试题,不懂哪里错了,运行了没有结果。
#include
#include
#include
#include
using namespace std;
int main()
{
freopen("output.out","w",stdout);
int count=0;
double a,b,c,d;
for(a=1;a<=9;a++)
{
for(b=1;b<=9;b++)
{
for(c=1;c<=9;c++)
{
for(d=1;d<=9;d++)
{
if(a==b || c==d) continue;
else
{
if((a*c)/(b*d)==(a*10+c)/(b*10+d))
{
printf("%.0lf/%.0lf ",a,b);
printf("%.0lf/%.0lf\n",c,d);
count++;
}
}
}
}
}
}
printf("answer:\n");
printf("%d\n",count);
return 0;
}
如果还有问题请私聊我~谢谢!
计算机要知道你这样写代码会被活活累死的。:)
不要嵌套这么多层循环
a,b,c,d的类型你设定的是int,这样除法“/”运算的时候结果就是去尾后的int,所以只要把int a,b,c,d改成double a,b,c,d结果就是14了
行了采纳一下谢谢
#include
using namespace std;
int main()
{
int ans = 0;
for(int a = 1; a <= 9; a ++)
for(int b = 1; b <= 9; b ++)
for(int c = 1; c <= 9; c ++)
for(int d = 1; d <= 9; d ++)
{
if(a == b || c == d) continue;
if(a * c * (b * 10 + d) == b * d * (a * 10 + c)) ans ++;
}
cout << ans << endl;
return 0;
}