1017是1014反过来进行计算
但比较麻烦的点在于要考虑读入的各种情况:
1.是否有实部
2.是否有虚部
3.实部的正负
3.虚部的正负
4.虚部是否为+/- 1
5.不是个位数的数字要进行进行运算
综合考虑各种情况,然后进行分类讨论
计算过程中可以拿ai+b/-1+i找出统一的规律(进行分母有理化,然后根据奇偶找规律搞定
低配版解法:
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
char s[1000];
long long d[1111],s1[1000];
long long num(char s[],long long begin,long long end)
{
long long ans=0;
for(long long i=begin;i<=end;i++)
{
ans*=10;
ans+=s[i];
}
return ans;
}
int main()
{
long long a=0,b=0,nowa,nowb;
long long ans1=0,ans2=0,flag=1;
//int pa=-1,pb=-1;
gets(s);
long long l=strlen(s);
for(long long i=0;i<l;i++)
{
if(s[i]>='0'&&s[i]<='9') s[i]=s[i]-'0';
}
if(s[l-1]=='i')
{
if(s[l-2]=='+'||s[l-2]=='-')
{
if(s[l-2]=='+') b=1;
if(s[l-2]=='-') b=-1;
if(l-2!=0)
{
if(s[0]=='-') a=-num(s,1,l-3);
else a=num(s,0,l-3);
}
}
else
{
for(long long j=l-1;j>=0;j--)
{
if(s[j]=='+')
{
if(s[0]=='-')
{
a=-num(s,1,j-1);
b=num(s,j+1,l-2);
}
else a=num(s,0,j-1);
b=num(s,j+1,l-2);
flag=2;
}
if(s[j]=='-'&&flag==1)
{
if(j==0)
{
a=0;
b=-num(s,1,l-2);
}
else
{
if(s[0]=='-') a=-num(s,1,j-1);
else a=num(s,0,j-1);
b=-num(s,j+1,l-2);
}
flag=3;
}
}
if(flag==1) b=num(s,0,l-2);
}
}
else
{
if(s[0]=='-') a=-num(s,1,l-1);
else a=num(s,0,l-1);
}
if(l==1&&s[0]=='i') b=1;
if(a==0&&b==0) cout<<"0"; //cout<<"a="<<a<<"b="<<b<<endl;
long long m=1;
while(a!=0||b!=0)
{
nowa=a%2;//cout<<"nowa="<<nowa<<endl;
nowb=b%2;//cout<<"nowb="<<nowb<<endl; cout<<endl;
d[m]=(nowa&1)^(nowb&1);//cout<<m<<"="<<d[m]<<endl;
long long tempa=-a+b,tempb=-a-b;
a=tempa;
b=tempb;
if(a%2==1||a%2==-1)
{
a++;
b++;
}
a/=2;
b/=2;//cout<<"a="<<a<<"b="<<b<<endl;
m++;
}
for(long long i=m-1;i>0;i--) cout<<d[i];
cout<<endl;
return 0;
}
高配版1.
#include <cstdio>
#include <iostream>
#include <cstring>
#include<algorithm>
using namespace std;
char s[1111];
struct CP
{
long long x,y;
CP operator+(const CP &b)
{
CP res;
res.x=x+b.x;
res.y=y+b.y;
return res;
}
CP operator-(const CP &b)
{
CP res;
res.x=x-b.x;
res.y=y-b.y;
return res;
}
CP operator/(const CP &b)
{
if (b.x!=-1||b.y!=1) return {-9999,-9999};//wrong
int r=*this%b;
CP res;
res.x=(y-x+r)/2;
res.y=-(x+y-r)/2;
return res;
}
int operator%(const CP &b)
{
if (b.x!=-1||b.y!=1) return -9999;//wrong
if ((x+y)&1) return 1;
else return 0;
}
bool notzero()
{
return x!=0||y!=0;
}
};
int ans[111111];
int main()
{
gets(s);
long long x=0,y=0;
if (s[0]=='i') {y=1;}//directly output
else
{
if (sscanf(s,"%lld",&x)!=1) y=s[0]=='-'?-1:1;
else
{
int l=strlen(s),pos=0;int neg=1;
for (int i=1;i<l;i++) if (s[i]=='+'||s[i]=='-') {pos=i;if (s[i]=='-') neg*=-1;break;}//find the second +/-
if (!pos) {if (s[l-1]=='i') {y=x;x=0;}}//only i
else if (!isdigit(s[l-2])) y=neg; //x+i or x-i
else sscanf(s+pos,"%lld",&y);//common
}
}
// cout<<x<<'@'<<y<<endl;
if (x==0&&y==0) {cout<<0<<endl;return 0;}
CP now;
now.x=x;now.y=y;
const CP p={-1,1};
int cnt=0;
// cout<<x<<' '<<y<<endl;
while (now.notzero())//not zero
{
ans[++cnt]=now%p;
// cout<<now%p;
now=now/p;
}
// cout<<endl;
for (int i=cnt;i>=1;i--) cout<<ans[i];
cout<<endl;
return 0;
}
高配版2:
#include <cstdio>
#include <iostream>
#include <cstring>
#include<algorithm>
#define int long long
using namespace std;
char s[1111];
int stringtoint(char *bn,char *ed)//begin,end
{
int res=0;
for (char *i=bn;i!=ed;i++)
{
res*=10;
res+=(*i)-'0';
}
return res;
}
signed main()
{
gets(s);
int l=strlen(s);
bool neg=false,neg2=false;
int pos=0;
if (s[0]=='-') {neg=true;pos++;}
for (;pos<l;pos++)
{
if (s[pos]=='+') break;
if (s[pos]=='-') {neg2=true;break;}
}
int x=0,y=0;
if (s[l-1]=='i'&&pos==l)//only i
{y=(neg==pos-1?1:stringtoint(s+neg,s+l-1));if (neg) y=-y;}
else if (pos==l)//only real
{x=stringtoint(s+neg,s+pos);if (neg) x=-x;}
else
{
x=stringtoint(s+neg,s+pos);
if (neg) x=-x;
y=(pos+1==l-1?1:stringtoint(s+pos+1,s+l-1));
if (neg2) y=-y;
} //cout<<x<<' '<<y<<endl;
long long a=x,b=y,nowa,nowb;
int d[1111];
if(a==0&&b==0) cout<<"0"; //cout<<"a="<<a<<"b="<<b<<endl;
long long m=1;
while(a!=0||b!=0)
{
nowa=a%2;//cout<<"nowa="<<nowa<<endl;
nowb=b%2;//cout<<"nowb="<<nowb<<endl; cout<<endl;
d[m]=(nowa&1)^(nowb&1);//cout<<m<<"="<<d[m]<<endl;
long long tempa=-a+b,tempb=-a-b;
a=tempa;
b=tempb;
if(a%2==1||a%2==-1)
{
a++;
b++;
}
a/=2;
b/=2;//cout<<"a="<<a<<"b="<<b<<endl;
m++;
}
for(long long i=m-1;i>0;i--) cout<<d[i];
cout<<endl;
return 0;
}