借书方案知多少

发布时间 2023-04-15 11:56:14作者: jmhyyds

 

源码:

#include<iostream>
using namespace std;
int main()
{
int a,b,c,count=0;
for(a=1;a<=5;a++){
for(b=1;b<=5;b++)
{
for(c=1;c<=5;c++)
{

if(a!=b&&a!=c&&b!=c)
count++;
}
}
}cout<<count;
return 0;}

运行结果

60

 

优化:

#include<iostream>
using namespace std;
int main()
{
int a,b,c,count=0;
for(a=1;a<=5;a++){
for(b=1;b<=5;b++)
{
for(c=1;c<=5&&a!=b;c++)
{

if(a!=c&&b!=c)
count++;
}
}
}cout<<count;
return 0;}