帮忙测试下代码?结果总是不对

funnyone 2010-09-22 05:39:48
题:
给一个整数n,重复做他每个子数的平方和。开心数是当你重复这个过程他最后会得到1。例如,82
8*8 + 2*2 = 64 + 4 = 68, repeat:
6*6 + 8*8 = 36 + 64 = 100, repeat:
1*1 + 0*0 + 0*0 = 1 + 0 + 0 = 1 (happy! :)
注意数字在意些基数时可能是开心数,另一些基数时可能不是开心数。例如基数82在基数3的情况下不是开心数。(10001)
你是世界上顶尖的数字侦探。一些基数混在一起。(他们被组织)并且雇用你完成一个任务—找出最小的比一大的整数并且在所有的基数是开心数。
你是世界上顶尖的数字侦探。一些基数混在一起。(他们被组织)并且雇用你完成一个任务—找出最小的比一大的整数并且在所有的基数是开心数。
Input

Output

3
2 3
2 3 7
9 10
Case #1: 3
Case #2: 143
Case #3: 91


#include<iostream>
#include<vector>
#include<string>
#include<stdlib.h>
#include<sstream>
#include<time.h>
#include<list>
#include<algorithm>
using namespace std;
class Test{
private:
vector<int> receive;
int result;
public:
Test::Test(vector<int>& it):result(0),receive(it){};
bool happynum(string);
void process();
friend ostream& operator<<( ostream& out,const Test& test);
};

bool Test::happynum(string rec) //判断是否开心数
{
if(rec.size()==1&&rec[0]==1)
return 1;
else if(rec.size()==1&&rec[0]!=1)
return 0;
else
{ int result=0;
for(string::size_type size=0;size!=rec.size();++size)
result+=rec[size]*rec[size];
happynum(itoa(result,const_cast<char *>(rec.c_str()),10));}
}

void Test::process()
{
result=receive.back();
string str;
while(1)
{
int test1=0;
for(vector<int>::iterator itor=receive.begin();itor!=receive.end();++itor)
{
if(!happynum(itoa(result,const_cast<char *>(str.c_str()),*itor)))
break;
else ++test1;
}
if(test1==receive.size())
break;
else ++result;
}
}
ostream& operator<<( ostream& out,const Test& test)
{
out<<test.result<<flush;
return out;
}


int main()
{
int time;
cin>>time;
vector<Test> test;
cin.get();
for(int a=0;a!=time;++a)
{
string rec;
int x;
vector<int> receive;
getline(cin,rec);
istringstream stream(rec);
while(stream>>x)
receive.push_back(x);
test.push_back(Test(receive));
cin.get();
}
for(vector<Test>::iterator itor=test.begin();itor!=test.end();++itor)
{
itor->process();
cout<<"case #"<<itor-test.begin()+1<<": "<<*itor<<endl;
}

system("pause");
return 0;
}

...全文
98 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
logiciel 2010-09-22
  • 打赏
  • 举报
回复
主要问题:happynum用递归将嵌套调用太深导致崩溃,返回0的条件错误。

#include<iostream>
#include<vector>
#include<string>
#include<stdlib.h>
#include<sstream>
#include<time.h>
#include<list>
#include<algorithm>
#include <string.h>
using namespace std;
class Test{
private:
vector<int> receive;
int result;
public:
Test::Test(vector<int>& it):result(0),receive(it){};
bool happynum(int data, int base); //bool happynum(string);
void process();
friend ostream& operator<<( ostream& out,const Test& test);
};

/*
bool Test::happynum(string rec, int base) //错 bool Test::happynum(string rec) //判断是否开心数
{
if(rec.size()==1&&rec[0]=='1') //错 if(rec.size()==1&&rec[0]==1)
return 1;
else if(rec.size()==1&&rec[0]!='1') //错 else if(rec.size()==1&&rec[0]!=1)
return 0;
else
{
int result=0;
for(string::size_type size=0;size!=rec.size();++size)
result+=(rec[size]-'0') * (rec[size]-'0'); //错 result+=rec[size]*rec[size];
//错 happynum(itoa(result,const_cast<char *>(rec.c_str()),10));}
return happynum(itoa(result,const_cast<char *>(rec.c_str()), base), base);
}
}
*/

bool Test::happynum(int init, int base)
{
char rec[100];
bool used[1000];
for (int i=0; i<1000; i++) used[i]=false;
int result = init;
for (;;)
{
if (result==1) return true;
itoa(result, rec, base);
result=0;
for(int j=0; rec[j]; ++j)
result+=(rec[j]-'0') * (rec[j]-'0');
if (used[result]) return false;
used[result]=true;
}
}

void Test::process()
{
result=receive.back();
//错 string str;
while(1)
{
int test1=0;
for(vector<int>::iterator itor=receive.begin();itor!=receive.end();++itor)
{
//错 if(!happynum(itoa(result,const_cast<char *>(str.c_str()),*itor)))
if(!happynum(result, *itor))
break;
else ++test1;
}
if(test1==receive.size())
break;
else ++result;
}
}
ostream& operator<<( ostream& out,const Test& test)
{
out<<test.result<<flush;
return out;
}


int main()
{
int time;
cin>>time;
vector<Test> test;
cin.get();
for(int a=0;a!=time;++a)
{
string rec;
int x;
vector<int> receive;
getline(cin,rec);
istringstream stream(rec);
while(stream>>x)
receive.push_back(x);
test.push_back(Test(receive));
//错 cin.get();
}
for(vector<Test>::iterator itor=test.begin();itor!=test.end();++itor)
{
itor->process();
cout<<"case #"<<itor-test.begin()+1<<": "<<*itor<<endl;
}

system("pause");
return 0;
}
zhaofaquan 2010-09-22
  • 打赏
  • 举报
回复
基数什么意思啊,求赐教,谢谢!
xhjyhappy 2010-09-22
  • 打赏
  • 举报
回复
解释下输入吧

第一个是test的次数

之后呢?
iambic 2010-09-22
  • 打赏
  • 举报
回复
你期待换一个编译器就能得到“正确”的结果了?

64,651

社区成员

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

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