用1到9这九个数字变成三位数加三位数等于三位数的加法,例如:173+295 =468,一共有多少种情况?

#include "stdafx.h"

#include <stdio.h>
#include <vector>
#include <algorithm>
using namespace std;

void FindCount(vector<int> &vect,int iPos,int &Count)
{  
	if (iPos>8)
	{
		int i1 = vect[0] * 100 + vect[1] * 10 + vect[2];
		int i2 = vect[3] * 100 + vect[4] * 10 + vect[5];
		int i3 = vect[6] * 100 + vect[7] * 10 + vect[8];
		if(i1+i2==i3)
		{
			printf("%d+%d==%d\n", i1, i2, i3);
			Count++;
		}
		return;
	}

	for (int i = 1; i <= 9;i++)
	{
		if (  find(vect.begin(),vect.end(),i)  ==  vect.end())
		{
			vect[iPos] = i;
			FindCount(vect, iPos + 1, Count);
			vect[iPos] = 0;
		}
	}
}

int _tmain(int argc, _TCHAR* argv[])
{

	vector<int> vt(9);
	int Count = 0;
	FindCount(vt, 0, Count);
	printf("一共%d个!", Count);
	system("pause");
	return 0;
}

  

顺便说说 vector中的find

一个find Vector的例子,注意find不属于vector的成员,而存在于算法中,应加上头文件#include <algorithm>:

#include <vector>
#include <algorithm>
#include <iostream>

int main( )
{
    using namespace std;

    vector<int> L;
    L.push_back( 1 );
    L.push_back( 2 );
    L.push_back( 3 );
    L.push_back( 4 );
    L.push_back( 5 );
    vector<int>::iterator result = find( L.begin( ), L.end( ), 3 ); //查找3
    if ( result == L.end( ) ) //没找到
        cout << "No" << endl;
    else //找到
        cout << "Yes" << endl;
}

  

posted @ 2014-08-19 10:21  hxb316  阅读(2006)  评论(0编辑  收藏  举报