算法题来袭

wangyangkobe 2011-04-25 01:54:56
两个三位数相加得到一个四位数,并且这三个数字的每一位都不相同。(也就是要把0-9这10个数全用上)

求高效的算法。
...全文
202 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangyangkobe 2011-04-26
  • 打赏
  • 举报
回复
我该了下,感觉还是不对

// 练习.cpp : 定义控制台应用程序的入口点。


#include <iostream>
#include <set>
using namespace std;

//两个三位数相加得到一个四位数,并且这三个数字的每一位都不相同。(也就是要把0-9这10个数全用上)
bool check(set<int>& s, int n)
{
int first = n%10;
int second = (n/10)%10;
int third = (n/100)%10;

pair<set<int>::iterator, bool> result1, result2, result3;
s.insert(1);
if (1 == s.size())
{
s.insert(first);
s.insert(second);
s.insert(third);
return true;
}
else if (4 == s.size())
{
if (s.insert(first).second && s.insert(second).second && s.insert(third).second)
{
return true;
}
}
else if (7 == s.size())
{
if (s.insert(first).second && s.insert(second).second && s.insert(third).second)
{
return true;
}
}
return false;
}
int main()
{
for (int i=200; i<500; i++)
{

for (int j=500; j<999; j++)
{
set<int> s;

if (check(s, i) && check(s,j) && (i+j-1000 > 0) && check(s, (i+j-1000)))
{
printf("%d + %d = %d\n", i, j, i+j);
}
s.clear();
}
}
system("pause");
}
wangyangkobe 2011-04-26
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 oldmtn 的回复:]

楼主,你的代码,好像有问题啊


#include <iostream>
#include <set>
using namespace std;

//两个三位数相加得到一个四位数,并且这三个数字的每一位都不相同。(也就是要把0-9这10个数全用上)
bool check(set<int>& s, int n)
{
int first = n%10;
……
[/Quote]


确实,确实有问题啊
justrun2005 2011-04-25
  • 打赏
  • 举报
回复
个位,余数不能是1;
十位,余数是0的时候个位只和必须小于10,余数是1的时候,个位只和必须大于10;
百位,余数是0的时候,十位只和必须小于10,余数是1的时候,十位只和必须大于10;
百位只和必须大于等于10;

余数为1(3+8,4+7,5+6)
余数为0(2+8,3+7,4+6,5+5)
大于等于10(2+9,2+8,3+9,3+8,3+7,4+9,4+8,4+7,4+6,5+5)
qq120848369 2011-04-25
  • 打赏
  • 举报
回复
#include <iostream>
#include <algorithm>
#include <set>
using namespace std;

char str[]="0123456789";

typedef struct setPair
{
public:
setPair(int x,int y):a(x),b(y)
{
}

bool operator < (const setPair &obj) const
{
int sum1,sum2;

sum1=a<b ? a*1000+b:b*1000+a;
sum2=obj.a<obj.b ? obj.a*1000+b:obj.b*1000+obj.a;

return sum1<sum2;
}

private:
int a,b;
}SetPair;

void findPerm()
{
set<SetPair> uniSet;

do
{
int first,second,third;

first=second=third=0;

for(int i=0;i<3;++i)
{
first=first*10+str[i]-'0';
second=second*10+str[i+3]-'0';
third=third*10+str[i+6]-'0';
}

third=third*10+str[9]-'0'; //mark...之前写成7了

if(third/1000>=1 && first+second==third)
{
if(uniSet.find(SetPair(first,second))==uniSet.end())
{
uniSet.insert(SetPair(first,second));
cout<<first<<"+"<<second<<"="<<third<<endl;
}
}
}
while(next_permutation(str,str+10));
}


int main()
{
findPerm();

return 0;
}

246+789=1035
249+786=1035
264+789=1053
269+784=1053
284+769=1053
286+749=1035
289+746=1035
324+765=1089
325+764=1089
342+756=1098
346+752=1098
347+859=1206
349+857=1206
352+746=1098
356+742=1098
357+849=1206
359+847=1206
364+725=1089
365+724=1089
423+675=1098
425+673=1098
426+879=1305
429+876=1305
432+657=1089
437+589=1026
439+587=1026
452+637=1089
457+632=1089
473+589=1062
475+623=1098
476+829=1305
479+583=1062
483+579=1062
487+539=1026
489+537=1026
573+489=1062
624+879=1503
625+473=1098
629+874=1503
652+437=1089
674+829=1503
679+824=1503
743+859=1602
749+853=1602
753+849=1602
759+843=1602
764+289=1053
826+479=1305
请按任意键继续. . .



cao_julians 2011-04-25
  • 打赏
  • 举报
回复
1234567890,10个数字分成3段,1234,567,890
视为1234=567+890,不成立,保留1不动,做234567890的全排列,。。。
pathuang68 2011-04-25
  • 打赏
  • 举报
回复

234+856=1090; // 两个0, 没有7,还是不符合要求

小新同学,继续努力。看你的了。

[Quote=引用 10 楼 qq120848369 的回复:]

C/C++ code
#include <iostream>
#include <algorithm>
#include <set>
using namespace std;

char str[]="0123456789";

typedef struct setPair
{
public:
setPair(int x,int y):a(x),b(y)
{
}

……
[/Quote]
LaSt_C_ 2011-04-25
  • 打赏
  • 举报
回复
你得出的数字全部重复了哦!
qq120848369 2011-04-25
  • 打赏
  • 举报
回复
#include <iostream>
#include <algorithm>
#include <set>
using namespace std;

char str[]="0123456789";

typedef struct setPair
{
public:
setPair(int x,int y):a(x),b(y)
{
}

bool operator < (const setPair &obj) const
{
int sum1,sum2;

sum1=a<b ? a*1000+b:b*1000+a;
sum2=obj.a<obj.b ? obj.a*1000+b:obj.b*1000+obj.a;

return sum1<sum2;
}

private:
int a,b;
}SetPair;

void findPerm()
{
set<SetPair> uniSet;

do
{
int first,second,third;

first=second=third=0;

for(int i=0;i<3;++i)
{
first=first*10+str[i]-'0';
second=second*10+str[i+3]-'0';
third=third*10+str[i+6]-'0';
}

third=third*10+str[7]-'0';

if(third/1000>=1 && first+second==third)
{
if(uniSet.find(SetPair(first,second))==uniSet.end())
{
uniSet.insert(SetPair(first,second));
cout<<first<<"+"<<second<<"="<<third<<";"<<endl;
}
}
}
while(next_permutation(str,str+10));
}


int main()
{
findPerm();

return 0;
}

234+856=1090;
236+854=1090;
254+836=1090;
256+834=1090;
304+958=1262;
308+954=1262;
324+756=1080;
326+754=1080;
354+726=1080;
356+724=1080;
358+904=1262;
364+908=1272;
368+904=1272;
403+859=1262;
405+968=1373;
408+965=1373;
409+853=1262;
423+657=1080;
426+957=1383;
427+653=1080;
432+658=1090;
438+652=1090;
452+638=1090;
453+627=1080;
456+927=1383;
457+623=1080;
458+632=1090;
459+803=1262;
463+789=1252;
465+908=1373;
468+905=1373;
469+783=1252;
483+769=1252;
489+763=1252;
504+869=1373;
506+928=1434;
508+926=1434;
509+864=1373;
526+908=1434;
528+906=1434;
564+809=1373;
569+804=1373;
605+829=1434;
607+928=1535;
608+927=1535;
609+825=1434;
624+759=1383;
625+809=1434;
627+908=1535;
628+907=1535;
629+754=1383;
635+789=1424;
637+908=1545;
638+907=1545;
639+785=1424;
654+729=1383;
659+724=1383;
685+739=1424;
689+735=1424;
706+829=1535;
709+826=1535;
726+809=1535;
729+806=1535;
732+954=1686;
734+952=1686;
736+809=1545;
739+806=1545;
752+934=1686;
754+932=1686;
803+469=1272;
805+629=1434;
809+453=1262;
809+463=1272;
836+709=1545;
839+706=1545;
863+409=1272;
869+403=1272;
908+354=1262;
926+457=1383;
937+608=1545;
938+607=1545;
956+427=1383;
964+308=1272;
968+304=1272;
请按任意键继续. . .


这次有没有重复..
qq120848369 2011-04-25
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 pathuang68 的回复:]

引用 2 楼 ouyh12345 的回复:

四位数的千位肯定为1
这样,需要排序的就减少到9个


对。

4L小新同学的答案中有重复的数字了。应该不符合楼主的要求。
[/Quote]

发现了,忘去重了.
pathuang68 2011-04-25
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 ouyh12345 的回复:]

四位数的千位肯定为1
这样,需要排序的就减少到9个
[/Quote]

对。

4L小新同学的答案中有重复的数字了。应该不符合楼主的要求。
oldmtn 2011-04-25
  • 打赏
  • 举报
回复
楼主,你的代码,好像有问题啊


#include <iostream>
#include <set>
using namespace std;

//两个三位数相加得到一个四位数,并且这三个数字的每一位都不相同。(也就是要把0-9这10个数全用上)
bool check(set<int>& s, int n)
{
int first = n%10;
int second = (n/10)%10;
int third = (n/100)%10;

pair<set<int>::iterator, bool> result1, result2, result3;
s.insert(1);
if (1 == s.size())
{
s.insert(first);
s.insert(second);
s.insert(third);
return true;
}
else if (4 == s.size())
{
if (s.insert(first).second && s.insert(second).second && s.insert(third).second)
{
return true;
}
}
else if (7 == s.size())
{
if (s.insert(first).second && s.insert(second).second && s.insert(third).second)
{
return true;
}
}
return false;
}
int main()
{
for (int i=200; i<500; i++)
{

for (int j=500; j<999; j++)
{
set<int> s;

if (check(s, i) && check(s,j) && (i+j-1000 > 100) && check(s, (i+j-1000)))
{
printf("%d + %d = %d\n", i, j, i+j);
}
s.clear();
}
}
system("pause");
}
oldmtn 2011-04-25
  • 打赏
  • 举报
回复
928+607=1535;
这个是问题~~~~~~
qq120848369 2011-04-25
  • 打赏
  • 举报
回复
234+856=1090;
236+854=1090;
254+836=1090;
256+834=1090;
304+958=1262;
304+968=1272;
308+954=1262;
308+964=1272;
324+756=1080;
326+754=1080;
354+726=1080;
354+908=1262;
356+724=1080;
358+904=1262;
364+908=1272;
368+904=1272;
403+859=1262;
403+869=1272;
405+968=1373;
408+965=1373;
409+853=1262;
409+863=1272;
423+657=1080;
426+957=1383;
427+653=1080;
427+956=1383;
432+658=1090;
438+652=1090;
452+638=1090;
453+627=1080;
453+809=1262;
456+927=1383;
457+623=1080;
457+926=1383;
458+632=1090;
459+803=1262;
463+789=1252;
463+809=1272;
465+908=1373;
468+905=1373;
469+783=1252;
469+803=1272;
483+769=1252;
489+763=1252;
504+869=1373;
506+928=1434;
508+926=1434;
509+864=1373;
526+908=1434;
528+906=1434;
564+809=1373;
569+804=1373;
605+829=1434;
607+928=1535;
607+938=1545;
608+927=1535;
608+937=1545;
609+825=1434;
623+457=1080;
624+759=1383;
625+809=1434;
627+453=1080;
627+908=1535;
628+907=1535;
629+754=1383;
629+805=1434;
632+458=1090;
635+789=1424;
637+908=1545;
638+452=1090;
638+907=1545;
639+785=1424;
652+438=1090;
653+427=1080;
654+729=1383;
657+423=1080;
658+432=1090;
659+724=1383;
685+739=1424;
689+735=1424;
706+829=1535;
706+839=1545;
709+826=1535;
709+836=1545;
724+356=1080;
724+659=1383;
726+354=1080;
726+809=1535;
729+654=1383;
729+806=1535;
732+954=1686;
734+952=1686;
735+689=1424;
736+809=1545;
739+685=1424;
739+806=1545;
752+934=1686;
754+326=1080;
754+629=1383;
754+932=1686;
756+324=1080;
759+624=1383;
763+489=1252;
769+483=1252;
783+469=1252;
785+639=1424;
789+463=1252;
789+635=1424;
803+459=1262;
803+469=1272;
804+569=1373;
805+629=1434;
806+729=1535;
806+739=1545;
809+453=1262;
809+463=1272;
809+564=1373;
809+625=1434;
809+726=1535;
809+736=1545;
825+609=1434;
826+709=1535;
829+605=1434;
829+706=1535;
834+256=1090;
836+254=1090;
836+709=1545;
839+706=1545;
853+409=1262;
854+236=1090;
856+234=1090;
859+403=1262;
863+409=1272;
864+509=1373;
869+403=1272;
869+504=1373;
904+358=1262;
904+368=1272;
905+468=1373;
906+528=1434;
907+628=1535;
907+638=1545;
908+354=1262;
908+364=1272;
908+465=1373;
908+526=1434;
908+627=1535;
908+637=1545;
926+457=1383;
926+508=1434;
927+456=1383;
927+608=1535;
928+506=1434;
928+607=1535;
932+754=1686;
934+752=1686;
937+608=1545;
938+607=1545;
952+734=1686;
954+308=1262;
954+732=1686;
956+427=1383;
957+426=1383;
958+304=1262;
964+308=1272;
965+408=1373;
968+304=1272;
968+405=1373;
请按任意键继续. . .

#include <iostream>
#include <algorithm>
using namespace std;

char str[]="0123456789";

void findPerm()
{
do
{
int first,second,third;

first=second=third=0;

for(int i=0;i<3;++i)
{
first=first*10+str[i]-'0';
second=second*10+str[i+3]-'0';
third=third*10+str[i+6]-'0';
}

third=third*10+str[7]-'0';

if(third/1000>=1 && first+second==third)
{
cout<<first<<"+"<<second<<"="<<third<<";"<<endl;
}
}
while(next_permutation(str,str+10));
}


int main()
{
findPerm();

return 0;
}

wangyangkobe 2011-04-25
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 ouyh12345 的回复:]

四位数的千位肯定为1
这样,需要排序的就减少到9个
[/Quote]

但是效率感觉还是不高,需要蛮力解决吧?
ouyh12345 2011-04-25
  • 打赏
  • 举报
回复
四位数的千位肯定为1
这样,需要排序的就减少到9个
wangyangkobe 2011-04-25
  • 打赏
  • 举报
回复
自己代码

#include <iostream>
#include <set>
using namespace std;

//两个三位数相加得到一个四位数,并且这三个数字的每一位都不相同。(也就是要把0-9这10个数全用上)
bool check(set<int>& s, int n)
{
int first = n%10;
int second = (n/10)%10;
int third = (n/100)%10;

pair<set<int>::iterator, bool> result1, result2, result3;
s.insert(1);
if (1 == s.size())
{
s.insert(first);
s.insert(second);
s.insert(third);
return true;
}
else if (4 == s.size())
{
if (s.insert(first).second && s.insert(second).second && s.insert(third).second)
{
return true;
}
}
else if (7 == s.size())
{
if (s.insert(first).second && s.insert(second).second && s.insert(third).second)
{
return true;
}
}
return false;
}
int main()
{
for (int i=200; i<500; i++)
{

for (int j=500; j<999; j++)
{
set<int> s;

if (check(s, i) && check(s,j) && (i+j-1000 > 100) && check(s, (i+j-1000)))
{
printf("%d + %d = %d\n", i, j, i+j);
}
s.clear();
}
}
system("pause");
}

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧