简单枚举题,0到9全排列

来源:互联网 发布:中俄关系 知乎 编辑:程序博客网 时间:2024/05/21 14:07

输入正整数n,按从小到大的顺序输出所有形如abcde/fghij=n的表达式,其中a~j恰好为数字0~9的一个排列,n为2到79

样例输入:62

样例输出:79546/01283=62

                    94736/01528=62


刚开始想用堆栈来实现0到9全排列,还有五个for循环也可以,但是想到STL中有一个next_permutation的函数,可以实现全排列,于是将其采用过来

程序如下:

#include <stdio.h>
#include<stdlib.h>
#include <algorithm>
#include <string.h>


using namespace std;
 
int main()
{
    int length,a,b,n;
    char str[20]="1234567890";
    scanf("%d",&n);
    
    length = strlen(str);


    while (next_permutation(str, str + length))
    {
//        printf("%s\n",str);
          a=(str[0]-'0')*10000+(str[1]-'0')*1000+(str[2]-'0')*100+(str[3]-'0')*10+(str[4]-'0');
          b=(str[5]-'0')*10000+(str[6]-'0')*1000+(str[7]-'0')*100+(str[8]-'0')*10+(str[9]-'0');
          
          if(b*n==a)
          {
          printf("%c%c%c%c%c/",str[0],str[1],str[2],str[3],str[4]);
          printf("%c%c%c%c%c=%d\n",str[5],str[6],str[7],str[8],str[9],n);
          }
          
    }
    
    system("pause");
    return 0;
}