如题所述
第1个回答 2019-11-08
#include<stdio.h>
#include <vector>
#include <iostream>
using namespace std;
void DFS(int step,vector<int> & v1, vector<int> & v2,int &count)
{
if (step > 9)
{
if (v1[1] * 100 + v1[2] * 10 + v1[3] + v1[4] * 100 + v1[5] * 10 + v1[6] == v1[7] * 100 + v1[8] * 10 + v1[9])
{
count++;
printf("%d%d%d+%d%d%d=%d%d%d\n", v1[1], v1[2], v1[3], v1[4], v1[5], v1[6], v1[7], v1[8], v1[9]);
}
return;
}
for (int i = 1; i <= 9; i++)
{
if (v2[i] == 0)
{
v1[step] = i;
v2[i] = 1;
DFS(step + 1, v1, v2, count);
v2[i] = 0;
}
}
return;
}
int main()
{
vector<int> v1(10,INT_MAX);
vector<int> v2(10, 0);
int count = 0;
DFS(1,v1,v2,count);
printf("Total of %d Expressions.",count/2);
return 0;
}
#include <vector>
#include <iostream>
using namespace std;
void DFS(int step,vector<int> & v1, vector<int> & v2,int &count)
{
if (step > 9)
{
if (v1[1] * 100 + v1[2] * 10 + v1[3] + v1[4] * 100 + v1[5] * 10 + v1[6] == v1[7] * 100 + v1[8] * 10 + v1[9])
{
count++;
printf("%d%d%d+%d%d%d=%d%d%d\n", v1[1], v1[2], v1[3], v1[4], v1[5], v1[6], v1[7], v1[8], v1[9]);
}
return;
}
for (int i = 1; i <= 9; i++)
{
if (v2[i] == 0)
{
v1[step] = i;
v2[i] = 1;
DFS(step + 1, v1, v2, count);
v2[i] = 0;
}
}
return;
}
int main()
{
vector<int> v1(10,INT_MAX);
vector<int> v2(10, 0);
int count = 0;
DFS(1,v1,v2,count);
printf("Total of %d Expressions.",count/2);
return 0;
}