题目描述
找出所有形如abc*dc(三位数乘以两位数)的算式,使得在完整的竖式中,所有数字都属于一个特定的数字集合。
例如,当该数字集合为2357时,只有一个竖式满足条件,该竖式中出现的所有数字都属于集合2357,该竖式为(*表示空格):
775
X33
…-----
*2325
2325
…-----
25575
输入
输入包括多行数据,每行一个正整数,表示该数字集合
输出
对每行输入,输出一个整数,即满足条件的竖式个数,然后输出换行
样例输入 Copy
2357
样例输出 Copy
1
解析:
记录下数字集合中的数字,枚举三位数和两位数,模拟整个竖式乘法,若满足要求则结果计数器+1,注意数字集合可能为0开头,故使用字符串进行读入。
#include<bits/stdc++.h>
#define ll long long
using namespace std;
set<int>st;
int main()
{
string str;
while(cin>>str){
int ans =0;
st.clear();
for(int i=0;i<str.size();i++)
st.insert(str[i]-'0');
for(int i=100;i<1000;i++){
int a1 = i/100,a2 = i%100/10,a3 = i%10;
if(st.find(a1) != st.end() && st.find(a2) != st.end() && st.find(a3) != st.end())
{
for(int j = 10;j<100;j++){
int cnt = i*j,flag =1;
while(cnt != 0){
if(st.find(cnt %10) == st.end()){
flag = 0;
break;
}
cnt /= 10;
}
if(!flag)
continue;
int b1 = j/10,b2=j%10;
int b3 = b1*i,b4 = b2 *i;
if(st.find(b1) != st.end() && st.find(b2) != st.end() )
{
while(b3 !=0){
if(st.find(b3%10) == st.end()){
flag = 0;
break;
}
b3 /= 10;
}
while(b4 !=0){
if(st.find(b4%10) == st.end()){
flag = 0;
break;
}
b4 /= 10;
}
if(flag){
ans++;
}
}
}
}
}
printf("%d\n",ans);
}
return 0;
}