几个算法问题2

2、福彩3D是3位数字,可以重复,给出3位数的全排列,并且列出全部重复的数字,有两个重复的数字。

#include <stdio.h>

std::vector<string>  v3dAllNum;

template <class Type>
Type stringToNum(const string& str)
{
	istringstream iss(str);
	Type num;
	iss >> num;
	return num;
}

template <class T>
string to_string(T _Val)
{
	ostringstream __os;

	__os<<_Val; 
	string str = __os.str();   //得到 string
	
	return str;
}

int IsABBA(int* num, int len)
{
	int b = -1;
//	int j = 0;


	if ( num[0] == num[1] )
	{

		if ( num[1] != num[2] ) {
			b = 1;   //AAB
			return b;
		}
		else
		{
		   //AAA
			b = 2;
			return b;
		}

	}
	else
	{
		if ( num[1] == num[2] )
		{
		   //ABB
		   b = 3;
		   return b;
		}
		else  if ( num[0] == num[2] )
		{
		   //ABA
		   b = 4;
		   return b;
		}
		else
		{
        	//ABC
			b = 5;
			return b;
        }
    }

}



int main(int argc, char *argv[])
{

	for (unsigned int i = 0; i < 9; i++) {


			for (unsigned int j = 0; j < 9; j++) {

				for (unsigned int n = 0; n < 9; n++) {

					v3dAllNum.push_back( to_string(i) + to_string(j) + to_string(n) );
				}

			}


		}
	}

	string strResult;
	
	for (std::vector<String>::iterator it = v3dAllNum.begin();it!=v3dAllNum.end();) {

    	int one = stringToNum(*it)/100;
		int two = stringToNum((*it)/10%10;
		int three =  stringToNum(*it)%10;

		int sNum[3] = {0};
		sNum[0] = one;
		sNum[1] = two;
		sNum[2] = three;

		int iRet = IsABBA(sNum, 3);

		if ( 2 == iRet ) {
			strResult = "豹子";
		}
		else if (5 == iRet) {
			strResult = "无重复号";
		}
		else
		{
			strResult = "有重复号";
		}
	}

	return 0;
}


 

 


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值