记一个函数:sprintf

问题描述

从键盘输入一个数字,输出所有的形如ABC*DE的一个算式,且满足ABCDE里面的所有数字都在我们输入的数字集合中。

在这个算式中,每行数字都属于2357中的一个数字。


 

Algorithm

如果是在以前,我肯定会这么做:

首先枚举这两个数ABC和DE,然后判断他们算式相乘的两个数以及结果,不断模10来判断是否每一个数字都属于输入的数字。

但是我发现了一个很厉害的函数:sprintf

简单来说,sprintf可以将数字储存到一个字符串里面。

此外,还有另一个函数:strchr

它的作用是检查某一个元素在一个字符串里第一次出现的位置,如果有,返回一个指针,反之返回NULL。

 

现在看完整代码。


 

AC

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 
 5 using namespace std;
 6 
 7 int fun()
 8 {
 9     char a[11], temp[99];
10     int c = 0;
11     cin>>a;
12     for(int i=100;i<1000;i++){
13         for(int j=10;j<100;j++){
14             sprintf(temp, "%d%d%d%d%d", i, j, (j%10)*i, (j/10)*i, i*j);
15             bool f = true;
16             for(int k=0;k<strlen(temp);k++){
17                 if(strchr(a, temp[k]) == NULL){
18                     f = false;
19                     break;
20                 }
21             }
22             if(f){
23                 printf("<%d>\n", ++c);
24                 printf("%5d\n", i);
25                 printf("X%4d\n", j);
26                 printf("-----\n");
27                 printf("%5d\n", i*(j%10));
28                 printf("%4d\n", i*(j/10));
29                 printf("-----\n");
30                 printf("%5d\n", i*j);
31             }
32         }
33     }
34     if(!c) cout<<"NULL, pleas input next..."<<'\n';
35     return 0;
36  } 
37  
38 int main()
39 {
40     while(1)
41     {
42         fun();
43     }
44     
45     return 0;
46 }
View Code

转载于:https://www.cnblogs.com/mabeyTang/p/10356107.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值