Time Limit: 2 sec / Memory Limit: 1024 MB
Score : 300300 points
Problem Statement
Given an integer NN, find the base −2−2 representation of NN.
Here, SS is the base −2−2 representation of NN when the following are all satisfied:
- SS is a string consisting of
0
and1
. - Unless S=S=
0
, the initial character of SS is1
. - Let S=SkSk−1...S0S=SkSk−1...S0, then S0×(−2)0+S1×(−2)1+...+Sk×(−2)k=NS0×(−2)0+S1×(−2)1+...+Sk×(−2)k=N.
It can be proved that, for any integer MM, the base −2−2 representation of MM is uniquely determined.
Constraints
- Every value in input is integer.
- −109≤N≤109−109≤N≤109
Input
Input is given from Standard Input in the following format:
NN
Output
Print the base −2−2 representation of NN.
Sample Input 1
-9
Sample Output 1
1011
As (−2)0+(−2)1+(−2)3=1+(−2)+(−8)=−9(−2)0+(−2)1+(−2)3=1+(−2)+(−8)=−9, 1011
is the base −2−2 representation of −9−9.
Sample Input 2
123456789
Sample Output 2
11000101011001101110100010101
Sample Input 3
0
Sample Output 3
0
#include<iostream>
#include<cmath>
using namespace std;
int c[2]={0,1};
int main()
{
int n,i=0,k,t;
int s[3000]={0};
cin>>n;
if(n==0)
{
cout<<"0"<<endl;
return 0;
}
while(n!=0)
{
k=n%(-2);
t=n/(-2);
if(k<0)
{
k=k-(-2);
t++;
}
i++;
n=t;
s[i]=c[k];
}
for(;i>=1;i--)
cout<<s[i];
return 0;
}
总结:开始想开个大数组一位一位算过去= =,后来仔细一想发现只要像做二进制题一样,模拟-2进制运算就可以轻松解出了。