求简单C++语言程序 多谢帮忙

hycscn 2009-06-22 10:58:12
设计一个类,判定输入的数据是否是Armstrong数 (若一个m位正整数n等于它的各位数字的m次方之和,则称n是一个Armstrong数 )
...全文
51 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
ysysbaobei 2009-06-23
  • 打赏
  • 举报
回复
顶下
gmajygah 2009-06-23
  • 打赏
  • 举报
回复
........学习中......
MichaelBomb 2009-06-23
  • 打赏
  • 举报
回复
有意思 , 值得研究
lambert_s 2009-06-22
  • 打赏
  • 举报
回复
这种不是和水仙花数的算法类似嘛,楼主还是模仿着写写看吧,有问题大家会很乐意帮你改的!
字云逸 2009-06-22
  • 打赏
  • 举报
回复
暂时不会,先学好再想
andylovecandy 2009-06-22
  • 打赏
  • 举报
回复
严重顶,这不难,好好想想,楼主不会连C++都不会吧。
wind_breez 2009-06-22
  • 打赏
  • 举报
回复
作业题,建议楼主自己写,不要走我当年的老路如今后悔至极啊!
lingyin55 2009-06-22
  • 打赏
  • 举报
回复
/*Armstrong数具有如下特征:一个n位数等于其个位数的n次方之和。如:
*153=1*1*1+5*5*5+3*3*3
*1634=1*1*1*1+6*6*6*6+3*3*3*3+4*4*4*4
*找出2、3、4、5位的所有Armstrong数。
*/
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
main()
{
int i,j,len,temp1,temp2,sum;
//len用于存储一个数是几位数,temp1、temp2用于存储i的值
for(i=10;i<=99999;i++)
{
len=0,temp1=i,temp2=i,sum=0;
do
{
++len;
temp1=temp1/10;
}while(temp1);
for(j=1;j<=len;j++)
{
sum+=(int)(pow(temp2%10,len));
temp2=temp2/10;
}
if(i==sum)
printf("%8ld",i);
}
}


http://bbs.chinaunix.net/viewthread.php?tid=1422118
lingyin55 2009-06-22
  • 打赏
  • 举报
回复
http://www.kuqin.com/tiku/20080424/7547.html
http://bbs.cfan.com.cn/thread-849393-1-9.html
sunfuxue 2009-06-22
  • 打赏
  • 举报
回复
我知道 可是实在是不会了吗 所以请大家帮个忙啊
eTouX 2009-06-22
  • 打赏
  • 举报
回复
作业要独立完成 不然毕业了后悔莫及
sunfuxue 2009-06-22
  • 打赏
  • 举报
回复
我要源代码~~~~
光宇广贞 2009-06-22
  • 打赏
  • 举报
回复
有意思……研究一下。
wcwcok 2009-06-22
  • 打赏
  • 举报
回复
你是要算法还是源代码?

先给你算法吧:

while循环将n各位数值用取模的方式求出来,同时也能得到m的值

将各位数值的m次方和求出来,与n比较

相同则返回ture,不同返回false
sunfuxue 2009-06-22
  • 打赏
  • 举报
回复
给你顶一下
weidong0210 2009-06-22
  • 打赏
  • 举报
回复
coolyama 2009-06-22
  • 打赏
  • 举报
回复
哪里不明白,可以问出来。
LZ不会是让大家全部帮你把这个类全部写出来吧。
罪过罪过...
T技术沙龙 2009-06-22
  • 打赏
  • 举报
回复
#include"iostream" 
#include"cstdlib"
#include"cmath"
using namespace std;

class Armstrong
{
public:
void show_info();
Armstrong();
Armstrong(long new_data);
private:
long data;
bool equal();
};

Armstrong::Armstrong():data(0)
{
//Body intentionally empty
}

Armstrong::Armstrong(long new_data):data(new_data)
{
//Body intentionally empty
}

bool Armstrong::equal()
{
long temp=data,sum=0;
int count=0,i;
while(temp)
{
count++;
temp/=10;
}
temp=data;
while(temp)
{
i=temp%10;
sum+=pow(i,count);
temp=temp/10;
}
if(data==sum)
return true;
else
return false;
}

void Armstrong::show_info()
{
if(equal())
cout<<"The number your enter is a Armstrong"<<endl;
else
cout<<"The number your enter is not a Armstrong"<<endl;
}

int main()
{
long a;
cout<<"Please enter the number"<<endl;
cin>>a;
Armstrong check(a);
check.show_info();
return 0;
}

运行成功,希望对楼主有所帮助

65,211

社区成员

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

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