要养成写子程序习惯,使得代码不会很长
写的时候忘记用bool标记一下,然后每个都在数组里面找很麻烦,优化一下好看多啦
#include<bits/stdc++.h>
using namespace std;
#define Max (int)(1e5+10)
#define INF 0x3f3f3f3f3f
typedef long long ll;
int n;
bool a[100];
bool f(int x)
{
while(x)
{
if(a[x%10])
x/=10;
else
return 0;
}
return 1;
}
bool check(int x,int y)
{
int a=x*(y%10),b=x*(y/10),test=x*y;
if(a>999||b>999||test>9999)
return 0;
if(f(x)&&f(y)&&f(a)&&f(b)&&f(test))
return 1;
return 0;
}
int main() {
scanf("%d",&n);
int x;
for(int i=1; i<=n; i++)
{
scanf("%d",&x);
a[x]=1;
}
int num=0;
for(int i=100;i<1000;i++)
for(int j=10;j<100;j++)
if(check(i,j))
num++;
printf("%d\n",num);
}
本文介绍了一种通过编写子程序来优化代码的方法,并利用布尔变量进行条件检查,确保数值运算满足特定条件。通过示例代码展示了如何实现这一优化过程。

199

被折叠的 条评论
为什么被折叠?



