2]) m_linePattern[i][1]=1;
if ( ch[1]==ch[2]) m_linePattern[i][2]=1;
}
for ( i = 0; i<26; i++)
{
if (m_letterPos[i].size()>0)
m_posptrVec.push_back(&m_letterPos[i]);//将所有出现的字母向量指针保存起来
}
}
具体判断候选的3个数字是否匹配时,先将这3个数字转换到3个字符串数组中去,这个转换可以使用sprintf函数。然后遍历m_posptrVec中非空的位置向量,判断每个保存在相同字母的位置向量中的位置,在相应的候选数字矩阵的相应位置的数是否相等,若相等则设置该数字以被使用,具体的IfMatch代码如下:
int IfMatch(int *maybe)
{
char numChar[3][4];
int bUse[10]={0};
int i,j;
for ( i = 0 ; i <3; i++)
sprintf(numChar[i],"%d",maybe[i]);//将数字转换成字符串
for ( i = 0; i < m_posptrVec.size(); i++)//遍历所有出现的字母
{
POSVEC &ps= (*m_posptrVec[i]);
int ch = numChar[ps[0].dim1][ps[0].dim2]-''''0'''';//取得该字母第一个位置处的数字
if ( bUse[ch] ) return 0; //如果数字已被使用就返回0
//逐一比较其他位置出的数字是否都相同
j=1;
while( j < ps.size() )
{
int ch2 = numChar[ps[j].dim1][ps[j].dim2]-''''0'''';
if (ch != ch2 ) return 0;
j++;
}
bUse[ch] = 1;//将该数字设置为已被使用
}
return 1;
}
2.4.×?oó???a??ààμ?êμ??′ú????×üè???:
#include <string>
#include <vector>
#include <stdlib.h>
#include <iostream>
using namespace std;
//****************************************************************
//àà??£oSecretSum
//×÷??£oroc£¨txqc4@sohu.com£?
//è??ú£o2005-12-26
//ó?í?: ±?′ú???aêμ??é?ê??oèüìa?ù×÷?£
//×¢òaê???£oè?ó?×a??£??′??±£3?±????μ?÷?£
//****************************************************************
class SecretSum{
????typedef struct{
????????int dim1;
????????int dim2;
????}POS;
????typedef vector<POS> POSVEC;
????typedef vector<POSVEC*> POSPTRVEC;
public :
????int countPossible(string num1, string num2, string result)
????{
????????string *pStr[]={&num1,&num2,&result};
????????init(pStr);
????????int i,j,count=0;
????????for ( i = 100; i<=899; i++)
????????{
????????????if ( ! IfNumOk(0, i)) continue;
????????????for ( j = 100; j <= 999-i; j++)
????????????{
????????????????if ( ! IfNumOk(1, j)) continue;
????????????????int maybe[]={i,j,i+j};
????????????????if ( ! IfNumOk(2, maybe[2])) continue;
????????????????if ( IfMatch (maybe))
????????????????{
????????????????????count++;
????????????????}
????????????}
????????}??
????????return count;
????}
????void init(string **pStr)
????{
????????int i,j;
??
????????memset(m_linePattern,0,sizeof(m_linePattern));
????????for ( i = 0 ; i< 3; i++)
????????{
????????????char ch[3];
????????????for ( j = 0; j<3; j++)
????????????{
????????????????ch[j] = (*pStr[i])[j];
?????
|