单点时限: 2.0 sec
内存限制: 512 MB
问题描述
复数整数 c (实部和虚部均为绝对值不超过 1 000 的整数)和整数 n (0≤n≤1 000),一个空格分隔。
输入格式
给定一个复数 c 和一个整数 n,计算 c^n,特别的我们补充定义 0^0=1。
输出格式
在一行中输出计算结果的“复数整数” 。
保证至少 65% 的数据不需要实现大整数运算就可以得到正确答案。
样例:
input:
2+i 2
output:
3+4i
问题分析:
提示:
1、大整数的定义需要使用cnt来记录位数,sign记录符号。
2、涉及大整数加减乘。不是很难但很烦。
3、我写这道题的时候不小心返回一个临时参数导致没抛出结果,要注意。
4、因为string转换成char*的时候,要注意c_str()返回的是一个const char*,正确的string转换char*是使用strcpy
5、大整数的运算很重要,多记记就好了。
代码解决部分:
#include <bits/stdc++.h>
#include <string>
using namespace std;
#define N 5000
typedef struct
{
int cnt,v[N],sign;
}BIGINT;
class Complex
{
private:
BIGINT re,im;
friend ostream& operator <<(ostream&,Complex&);
public:
Complex (BIGINT,BIGINT);
Complex (string);
Complex& pow(int);
void mul(Complex);
};
Complex::Complex(string c)
{
re.cnt=0;re.sign=1;
im.cnt=0;im.sign=1;
memset(re.v,0,sizeof(re.v));
memset(im.v,0,sizeof(im.v));
char s[5000];
strcpy(s,c.c_str());//因为数组方便,所以转化了
int len =strlen(s);
int j=len-1;
if (s[j]=='i')
{
if (j==0)
{
this->im.v[0]=1;this->im.cnt=1;this->im.sign=1;return;
}
j=j-1;
if(s[j]=='-'||s[j]=='+') this->im.v[this->im.cnt++]=1;
else
{
while(j>=0&&s[j]>='0'&&s[j]<='9')
{
this->im.v[this->im.cnt++]=s[j--]-'0';
}
}
if(j>=0)
{
if (s[j]=='-') this->im.sign=-1;
j=j-1;
}
}
while(j>=0&&s[j]>='0'&&s[j]<='9')
{
this->re.v[this->re.cnt++]=s[j--]-'0';
}
if(j>=0)
{
if(s[j]=='-')
this->re.sign=-1;
}
}
ostream& operator<<(ostream& out,Complex& result)
{
if (result.re.cnt!=0)
{
if(result.re.sign==-1)
out<<"-";
for(int i=(result.re.cnt-1);i>=0;i--) out<<result.re.v[i];
if(result.im.cnt!=0&&result.im.sign==1) out<<"+";
}
if (result.im.cnt!=0)
{
if (result.im.sign==-1) out<<"-";
if(result.im.cnt==1&&result.im.v[0]==1) out<<"i";
else
{
for(int i=result.im.cnt-1;i>=0;i--)
{
out<<result.im.v[i];
}
out<<"i";
}
}
return out;
}
BIGINT BIGMUL(BIGINT S,BIGINT T)
{
BIGINT R={0,{0},1};
if(S.cnt==0||T.cnt==0) return R;
R.cnt = S.cnt+T.cnt;
R.sign =S.sign*T.sign;
for(int i=0;i<T.cnt;i++)
{
int j,t,k;
int carry=0;
for(j=0;j<S.cnt;j++)
{
t=S.v[j]*T.v[i]+carry+R.v[i+j];
R.v[i+j] = t%10;
carry=t/10;
}
k=i+j;
while (carry>0)
{
t=carry+R.v[k];
R.v[k]=t%10;
carry=t/10;
k++;
}
}
if (R.v[S.cnt+T.cnt-1]==0) R.cnt--;
return R;
}
void SUB(BIGINT* s,BIGINT* t,BIGINT* result)
{
int n=(s->cnt>t->cnt)?s->cnt:t->cnt;
result->cnt=n;
int carry=0,i;
for(i=0;i<n;i++)
{
if((*(s->v+i)+carry)<(*(t->v+i)))
{
*(result->v+i)=*(s->v+i)+10+carry-*(t->v+i);
carry=-1;
}
else {
*(result->v+i)=*(s->v+i)+carry-*(t->v+i);
carry=0;
}
}
for (int i=n-1;i>=0&&result->v[i]==0;i--) result->cnt--;
}
int cmp(BIGINT s,BIGINT t)
{
int n =(s.cnt>t.cnt)?s.cnt:t.cnt;
for(int i=n-1;i>=0;i--)
{
if(*(s.v+i)>*(t.v+i)) return 1;
else if(*(s.v+i)<*(t.v+i)) return -1;
}
return 0;
}
BIGINT BIGSUB (BIGINT s,BIGINT t)
{
BIGINT R={0,{0},1};
if (cmp(s,t)>=0)
{
R.sign=1;
SUB(&s,&t,&R);
}
else
{
R.sign=-1;
SUB(&t,&s,&R);
}
return R;
}
BIGINT BIGADD(BIGINT s,BIGINT t)
{
if(s.cnt==0) return t;
if(t.cnt==0) return s;
BIGINT R={0,{0},1};
if(s.sign*t.sign<0)
{
if(s.sign==-1) R=BIGSUB(t,s);
else R=BIGSUB(s,t);
}
else
{
R.sign =s.sign;
int i,carry=0;
for(i=0 ;i<s.cnt&&i<t.cnt;i++)
{
int temp = s.v[i]+t.v[i]+carry;
R.v[i]=temp%10;
carry=temp/10;
}
while(i<t.cnt)
{
int temp =t.v[i]+carry;
R.v[i++] =temp%10;
carry = temp/10;
}
while (i<s.cnt)
{
int temp =s.v[i]+carry;
R.v[i++]=temp%10;
carry = temp/10;
}
if(carry)
{
R.v[i++]=carry%10;
}
R.cnt=i;
}
return R;
}
void Complex::mul(Complex a)
{
BIGINT re1=BIGMUL(a.re,re);
BIGINT re2=BIGMUL(a.im,im);re2.sign=-re2.sign;
BIGINT im1=BIGMUL(a.re,im);
BIGINT im2=BIGMUL(a.im,re);
re=BIGADD(re1,re2);
im=BIGADD(im1,im2);
}
Complex& Complex::pow(int n)
{
Complex p=*this;
// cout<<p;
for(int i=2;i<=n;i++)
{
// cout<<"pass"<<i;
// cout<<p<<*this<<"!!";
this->mul(p);
// cout<<*this<<"!!";
}
return *this ;
}
int main()
{
string s;int n;
cin>>s>>n;
Complex data{s};
if(n==0) cout<<"1"<<endl;
else if(n==1) cout<<data<<endl;
else
{
cout<<data.pow(n)<<endl;
}
return 0;
}