* 竖式问题:找出形如abc * de(三位数乘以两位数)的算式,使得在完整的竖式中,所有数字都属于一个特定的数字集合.输入数字集合(相邻数字之间没有空格),输出所有竖式.每个竖式前应有编号 ,之后应有个空行,最后输出解的总数,具体格式见样例输出,为了便于观察,竖式中的空格改用小数点*表示,但所写程序中应该输出空格,而非小数点
样例输入:
2357
输出
<1>
..775
..33
-----
.2325
2325
-----
25575
* The number of solutions = 1
*/
这题主要运用的是枚举发,全部理出输出符合条件的。
需要学习三个<string.h>中的三个函数
strchr strchr函数功能为在一个串中查找给定字符的第一个匹配之处。函数原型为:char *strchr(const char *str, int c),即在参数 str 所指向的字符串中搜索第一次出现字符 c(一个无符号字符)的位置。https://blog.csdn.net/weixin_43831728/article/details/106783220?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522166230116716782390575712%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=166230116716782390575712&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~top_positive~default-1-106783220-null-null.142^v46^pc_rank_34_1&utm_term=strchr&spm=1018.2226.3001.4187
strlen
sprintf
具体代码如下
#include<stdio.h>
#include<string.h>
int main(){
char s[20],buf[124];
scanf("%s",s);
int i,j,k,count=0,result,x,y,abc,de;
for(abc=100;abc<=999;abc++)
for(de=10;de<=99;de++)
{
result=abc*de;
i=de%10;j=i%10;
x=abc*i;y=abc*j;
sprintf(buf,"%d%d%d%d%d",abc,de,x,y,result);//输出到字符串sprintf
int yn=1; //为了后续控制输出
for(i=0;i<strlen(buf);i++) //字符串实际长度strlen
if(strchr(s,buf[i])==NULL) yn=0;
/*strchr函数在s字符串查找buf[i],
只要有一个不在则不满足题意,则yn变0*/
if(yn){
printf("满足题意的第%d个竖式\n",++count);
printf("%5d\n%5d\n----\n%5d\n%4d\n----\n%5d\n\n",abc,de,x,y,result);
}
}
printf("一共%d个式子\n",count);
}