> 2009/05/27 (水) 23:36:21 ◆ ▼ ◇ [qwerty]> > 10+10-11ってのもあるんじゃない?(;´Д`)
> 整列していって小さい方から足していくんだよ(;´Д`)
> 対象になる数を超えればそれ以上やる必要ない
試してみたが劇的に早くなった(;´Д`)
#include "stdafx.h"
#include <stdlib.h>
#include <string>
#include <vector>
#include <algorithm>
__int64 table[]={1,5,3,2,8,4,9,0,1,2,3,4,1,5,3,2,8,4,9,0,1,2,3,4};
int ntable = sizeof(table)/sizeof(__int64);
const __int64 target = 9;
__int64 func( std::string &str, __int64 total, __int64 *tbl, int ntbl)
{
if(total>target) return total;
for(int i=0;i<ntbl; ++i){
std::string s = str+",";
char buf[0x40];
_ltoa_s((long)tbl[i],buf,0x40,10);
s += buf;
if( total+tbl[i]==9){
printf("みつけたよ(%s)\n", s.c_str());
} else {
func( s, total+tbl[i], &tbl[i+1], (ntbl-(i+1) ) );
}
}
return total;
}
int _tmain(int argc, _TCHAR* argv[])
{
std::vector<__int64> data;
data.resize(ntable);
for(int i=0;i<ntable;++i){
data[i]=table[i];
}
std::sort(data.begin(),data.end());
std::string str;
func(str, 0, &data[0], ntable);
return 0;
}
参考:2009/05/27(水)23時20分35秒