重工电子论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 活动 交友 discuz
查看: 5859|回复: 3
打印 上一主题 下一主题

[分享] CMD窗口下,算出X^Y的每一位

[复制链接]

287

主题

668

帖子

5636

积分

学生管理组

Rank: 8Rank: 8

积分
5636
跳转到指定楼层
楼主
发表于 2016-1-19 08:11:59 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
给出一个算法,是我很早之前写的了,大家可以拿来测试下CPU的单核处理能力,哈哈
该算法,算出 任何数的任何次方。
编译环境VC6.0
理论上说,只要在VS2010之前的编译器都可以通过。任何C++编译器也可以通过。

[C++] syntaxhighlighter_viewsource syntaxhighlighter_copycode
#include<iostream>
#include<string>
#include<stdlib.h>
#include <memory.h>
using namespace std;
#define N 3
class add{
public:
add(string y="N",string x="0",int z=0);

int add_fun();
int stradd_fun(string );
int strmul_fun();
string A_stradd_fun(string ,string);
int abc_fun(int );
int judgement();
string retrun_b(){return b;};
void display();
private:
string a;
string b;
string a1;
int c;
};
add::add(string x,string y,int z){
b=x;
a=x;
c=z;
a1=b;
}
int add::strmul_fun()
{
int v1,a2,length_a,length_b;
int c=0,i,d,d1,time;
string x1,x2="";

length_b=b.length();
length_a=a.length();
for(d=0;d<length_b;d++)
{
d1=d;
x1=a;
v1=b[length_b-1-d]-48; //get the last element
time=length_a-1;
for(i=0;i<length_a;i++)
{
x1[time]=a[time]-'0'; //conversion the date
if(c>0)
{
x1[time]=v1*x1[time]+c;
c=0;
}
else
{
x1[time]=v1*x1[time];
}
c=x1[time]/10; //get the carried date c
x1[time]=x1[time]-c*10;
x1[time]=x1[time]+48; //conversion the date to char
time--;
}
if(c>0){x1.insert(0,c+48);c=0;}
for(d1--;d1>=0;d1--)
{
a2=x1.length();
x1.insert(a2,"0");
}
if(d>0){x2=A_stradd_fun(x2,x1);}
else{x2=x1;}
}
a=x2;
return 0;
}
string add::A_stradd_fun(string x,string y)
{
int i,x_last,y_last,length;
x_last=x.length()-1;
y_last=y.length()-1;
c=0;
for(i=0;i<x.length();i++)
{

if(c==1){y[y_last]=y[y_last]+x[x_last]+1-48;} //有进位的情况
else{y[y_last]=y[y_last]+x[x_last]-48;}
c=0;
if(y[y_last]>57)
{
y[y_last]-=10;
c=1;
}
x_last--;
y_last--;
}
if(x_last==-1&&c==1)
{
length=y.length()-x.length(); //判断加数最高位给被加数进位的影响
if(length==0){y.insert(0,"1");c=0;} //如果加数和被加数位数相等 则执行
else
{
length--;
for(;c==1&&length>=0;length--)
{
c=0; //进位标志设0
y[length]++;
if(y[length]>57)
{
y[length]-=10;
c=1; //有进位
}
}
if(length<0&&c==1){y.insert(0,"1");c=0;}
}
}
return y;
}
void add::display(){

cout<<"结果为: "<<a<<endl;
}
int main()
{


int n=0,i,u;
int n1=0,n2=0;
string c,b;
char p[255];
cout<<"基数"<<endl;
cin >> c;
cout<<"指数"<<endl;
cin>>b;
strcpy(p,b.c_str());

u=atoi(p);
add a(c);

for(i=0;i<u-1;i++)
{
a.strmul_fun();
}
a.display();
return 0;
}


运行结果:


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏
回复

使用道具 举报

287

主题

668

帖子

5636

积分

学生管理组

Rank: 8Rank: 8

积分
5636
推荐
 楼主| 发表于 2016-1-19 18:02:57 | 只看该作者
张金权-11级电子 发表于 2016-1-19 10:11
5的666次方在你的电脑上面算出来跑了多久啊


3的10000次方  跑了12秒 光标闪烁12下
回复 支持 1 反对 0

使用道具 举报

12

主题

66

帖子

538

积分

高级会员

Rank: 4

积分
538
QQ
沙发
发表于 2016-1-19 10:11:18 | 只看该作者
5的666次方在你的电脑上面算出来跑了多久啊
回复 支持 反对

使用道具 举报

12

主题

66

帖子

538

积分

高级会员

Rank: 4

积分
538
QQ
地板
发表于 2016-1-20 10:05:41 | 只看该作者
强                                                                              
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|cqutlab ( 渝ICP备15004556号

GMT+8, 2024-4-29 14:32 , Processed in 0.188126 second(s), 34 queries .

Powered by Discuz! X3.1

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表