这个程序是我在网上找到的:
// w.cpp : Defines the entry point for the console application.
//
#include <iostream.h>
#include <iomanip.h>
const int times=15; //总的次数(可变动数值范围)
const int Max=10; //可选的数字
const int len=4; //数字的选择量
const int tot=210; //组合情况的总数
const int position_cord=24; //每组数字的排列情况总数
const int position_Max=4; //每次待排数字总量
const int position_len=4; //每次排的数字量
int number[1][Max]; //组合设为二维数组以便日后改进
int cord[len]; //记录求组合时的中间变量
int total[tot][len]; //用于记录组合数
int maybe[tot]; //用于记录可能是答案的组合
int sum[tot]; //用于记录所有组合的结果
int p=0; //total的一维指针
int pmaybe=0; //maybe的一维指针
int count=0; //对任意组合的数量进行统计
int guess[times][len]; //记录每次猜的数字
int pguess=0; //guess的一维指针
int A[times]; //记录每次游戏的A指示
int position[position_Max]; //待排变量
int position_case[position_cord][position_len];//待排情况
int pcase=0; //情况指针
void Init()
{
p=0;//total的一维指针
pmaybe=0; //maybe的一维指针
count=0;//对任意组合的数量进行统计
pguess=0; //guess的一维指针
pcase=0;//情况指针
}
void Init_guess()
{
for (int i=0; i<times; i++)
{
for (int j=0; j<len; j++)
guess[i][j]=-1;
A[i]=-1;
}
}
void Init_maybe()
{
for (int i=0; i<tot; i++)
maybe[i]=-1;
pmaybe=0;
}
void Init_number()
{
for (int i=0; i<Max; i++)
number[0][i]=0;
}
void set(int m, int n)
{
int i;
if (m == len )
{
for (i=0; i<len; i++)
total[p][i]=cord[i];
count++;
p++;
}
else if ( n < Max)
{
for (i=0; i<2; i++)
{
if (i==0) { cord[m]=n; set(m+1,n+1); }
else set(m,n+1);
}
}
}
void order(int n)
{
int i, temp;
if (n == position_len)
{
for (i=0; i<position_len ;i++)
position_case[pcase][i]=position[i];
pcase++;
}
else
{
for (i=n; i<position_Max; i++)
{
temp=position[n]; position[n]=position[i]; position[i]=temp;
order(n+1);
temp=position[n]; position[n]=position[i]; position[i]=temp;
}
}
}
void condition(int sum, int pnum)
{
int sumk;
int temp;
Init_maybe();
for (int i=0; i<tot; i++)
{
sumk=0;
for (int j=0; j<len; j++)
{
temp=total[i][j];
sumk+=number[pnum][temp];
}
if (sumk == sum) { maybe[pmaybe]=i; pmaybe++; }
}
}
int answer(int sum, int pnum)
{
condition(sum, pnum);
pmaybe=0;
int i=0,j=0;
int temp;
while(1)
{
if (maybe[pmaybe] == -1) break;
i=maybe[pmaybe];
for (j=0; j<len; j++)
{
temp=total[i][j];
cout<<temp<<" ";
}
cout<<endl;
pmaybe++;
}
if (pmaybe >= 1) return 1;
return 0;
}
void sum_fun(int pnum)
{
int sumk, temp;
for (int i=0; i<tot; i++)
{
sumk=0;
for (int j=0; j<len; j++)
{
temp=total[i][j];
sumk+=number[pnum][temp];
}
sum[i]=sumk;
}
}
void set_number(int(* a)[len], int line)
{
int i, temp;
for (i=0; i<Max; i++)
number[0][i]=number[0][i]*10;
for (i=0; i<len; i++)
{
temp=a[line][i];
if (pguess < times ) guess[pguess][i]=a[line][i];
number[0][temp]++;
}
pguess++;
}
int position_order(int n, int pguess)
{
if (pguess>=0)
{
int c=0;
for (int i=0; i<len; i++)
if ( position_case[n][i] == guess[pguess][i]) c++;
if (c == 4) return 0;
if (c > A[pguess]) return 0;
return position_order(n,pguess-1);
}
else return 1;
}
int set_maybe(int sum)
{
condition(sum, 0);
int i=0, right=0;
int k=0;
for (pmaybe=0; pmaybe<tot; pmaybe++)
{
i=maybe[pmaybe];
for (int po=0; po<position_len; po++)
position[po]=total[i][po];
pcase=0;
order(0);
for (right=0; right<position_cord; right++)
if ( position_order(right,pguess-1) ) { k=1; break; }
if ( k==1 ) break;
}
if (pmaybe == tot) { cout<<"该题目无解"<<endl; return 0; }
set_number(position_case, right);
cout<<"请向游戏输入:";
for (int kk=0; kk<len; kk++)
cout<<position_case[align=right][kk]<<" ";
cout<<endl;
return 1;
}
void first()
{
int a[1][len], limit=0;
while(limit<len)
{
cout<<"请输入你第一次猜时的第"<<limit+1<<"个数字:";
cin>>a[0][limit];
if (a[0][limit] >=0 && a[0][limit]<Max) limit++;
else cout<<"输入数字不在范围内,请重输!"<<endl;
}
set_number(a, 0);
}
int fun()
{
Init();
Init_guess();
Init_number();
Init_maybe();
first();
int b=0;
int result=0;
set(0,0);
cout<<"总组合情况: "<<count<<endl;
cout<<"请输入游戏的第一次时A的提示情况:";
cin>>A[pguess-1];
if (A[pguess-1] == 4)
{
cout<<"\n谢谢使用本程序!"<<endl;
return 1;
}
cout<<"请输入游戏的第一次时B的提示情况:";
cin>>result;
result=result+A[pguess-1];
b=b*10+result;
cout<<"第一次提示情况下可能的组合:"<<endl;
for (int kk=2; kk<=times; kk++)
{
if (!answer(b,0) ) { cout<<"该题目无解"<<endl; break; }
cout<<endl;
set_maybe(b);
cout<<"请输入游戏的第"<<kk<<"次的A的提示情况:";
cin>>A[pguess-1];
if (A[pguess-1] == 4) break;
cout<<"请输入游戏的第"<<kk<<"次的B的提示情况:";
cin>>result;
result=result+A[pguess-1];
b=b*10+result;
cout<<"第"<<kk<<"次提示情况下可能的组合:"<<endl;
}
cout<<"\n谢谢使用本程序!"<<endl;
return 1;
}
void main()
{
char a;
do
{
fun();
cout<<"若输入#退出本程序:";
cin>>a;
cout<<endl<<endl;
}while (a != '#');
}
但是我不知道这个程序的思想,各位能不能解释这个程序的思路或提供自己的代码