竖式问题

 1 /*
 2 竖式问题 输入一个特定的数字集合,计算并输出所有满足要求的竖式。要求是abc*de(一个三位数乘以一个二位数)的竖式中
 3 出现的所有数字均在集合中。输出时要个按照格式。 
 4 解题思路 首先读入数字集合,枚举每个三位数乘以每个二位数,如果满足条件输出并计数。 
 5 
 6 另外读书总结
 7     int cou=0;
 8     printf("%d %d %d\n",cou++,cou++,cou++);//输出结果为2 1 0,编译原理something..  
 9 */
10 #include<cstdio>
11 #include<cstring>
12 int ok(char *s,char *buf);
13 
14 int main()
15 {
16     char s[25],buf[50];
17     int abc,de,d,e,x,y,z,t=1,cou;
18     while(scanf("%s",s) != EOF){
19         //puts(s);
20         printf("<%d>\n",t);
21         cou=0;
22         for(abc=100; abc<=999; abc++){
23             for(de=10;de<=99;de++){
24                 d=de/10;
25                 e=de%10;
26                 x=abc*e;
27                 y=abc*d;
28                 z=abc*de;
29                 sprintf(buf,"%d%d%d%d",abc,de,x,y,z);//sprintf函数的使用 
30                 if(ok(s,buf)){
31                     cou++;
32                     //格式控制 
33                     printf("%5d\nX%4d\n-----\n%5d\n%4d\n-----\n%5d\n\n",abc,de,x,y,z);
34                 }
35             }
36         }
37         printf("在该数字集合下总共有%d种结论\n",cou);
38         t++;
39     } 
40     return 0;    
41 } 
42 
43 int ok(char *s,char *buf)
44 {
45     int i,l=strlen(buf);
46     for(i=0;i<l;i++){
47         if(strchr(s,buf[i]) == NULL)//strchr的使用 
48             return 0;
49     }
50     return 1;
51 }

 

posted @ 2018-04-10 20:12  Reqaw  阅读(493)  评论(0编辑  收藏  举报