Atcode ABC105-C:Base -2 Number

版权声明:本文为博主原创文章,转载请注明出处 https://blog.csdn.net/wang_123_zy/article/details/81591744

C - Base -2 Number


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 and 1.
  • Unless S=S= 0, the initial character of SS is 1.
  • 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:N

Output

Print the base −2 representation of N.


Sample Input 1 Copy

Copy

-9

Sample Output 1 Copy

Copy

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 representation of −9.


Sample Input 2 Copy

Copy

123456789

Sample Output 2 Copy

Copy

11000101011001101110100010101

Sample Input 3 Copy

Copy

0

Sample Output 3 Copy

Copy

0

题意

输入一个数,求该数的负二进制数 

先记下来,代码不是太理解,明天慢慢看

#include <bits/stdc++.h>
using namespace std;
const int maxn=1e6+10;
int a[maxn];
int main(int argc, char const *argv[])
{
	int n;
	cin>>n;
	if(n==1||n==0)
	{
		cout<<n<<endl;
		return 0;
	}
	int k=0;
	while(n)
	{
		int m=n;
		a[k++]=abs(n)%2;
		n/=-2;
		if(m<0&&abs(m)%2)
			n++;
	}	
	for(int i=k-1;i>=0;i--)
		cout<<a[i];
	cout<<endl;
	return 0;
}

 

阅读更多

NUMBER BASE CONVERSION

09-14

DescriptionnnWrite a program to convert numbers in one base to numbers in a second base. There are 62 different digits: n 0-9,A-Z,a-z nHINT: If you make a sequence of base conversions using the output of one conversion as the input to the next, when you get back to the original base, you should get the original number. nInputnnThe first line of input contains a single positive integer. This is the number of lines that follow. Each of the following lines will have a (decimal) input base followed by a (decimal) output base followed by a number expressed in the input base. Both the input base and the output base will be in the range from 2 to 62. That is (in decimal) A = 10, B = 11, ..., Z = 35, a = 36, b = 37, ..., z = 61 (0-9 have their usual meanings). nOutputnnThe output of the program should consist of three lines of output for each base conversion performed. The first line should be the input base in decimal followed by a space then the input number (as given expressed in the input base). The second output line should be the output base followed by a space then the input number (as expressed in the output base). The third output line is blank. nSample Inputnn8n62 2 abcdefghizn10 16 1234567890123456789012345678901234567890n16 35 3A0C92075C0DBF3B8ACBC5F96CE3F0AD2n35 23 333YMHOUE8JPLT7OX6K9FYCQ8An23 49 946B9AA02MI37E3D3MMJ4G7BL2F05n49 61 1VbDkSIMJL3JjRgAdlUfcaWjn61 5 dl9MDSWqwHjDnToKcsWE1Sn5 10 42104444441001414401221302402201233340311104212022133030nSample Outputnn62 abcdefghizn2 11011100000100010111110010010110011111001001100011010010001nn10 1234567890123456789012345678901234567890n16 3A0C92075C0DBF3B8ACBC5F96CE3F0AD2nn16 3A0C92075C0DBF3B8ACBC5F96CE3F0AD2n35 333YMHOUE8JPLT7OX6K9FYCQ8Ann35 333YMHOUE8JPLT7OX6K9FYCQ8An23 946B9AA02MI37E3D3MMJ4G7BL2F05nn23 946B9AA02MI37E3D3MMJ4G7BL2F05n49 1VbDkSIMJL3JjRgAdlUfcaWjnn49 1VbDkSIMJL3JjRgAdlUfcaWjn61 dl9MDSWqwHjDnToKcsWE1Snn61 dl9MDSWqwHjDnToKcsWE1Sn5 42104444441001414401221302402201233340311104212022133030nn5 42104444441001414401221302402201233340311104212022133030n10 1234567890123456789012345678901234567890

NUMBER BASE CONVERSION这个题目怎么做?

09-20

DescriptionnnWrite a program to convert numbers in one base to numbers in a second base. There are 62 different digits: n 0-9,A-Z,a-z nHINT: If you make a sequence of base conversions using the output of one conversion as the input to the next, when you get back to the original base, you should get the original number. nInputnnThe first line of input contains a single positive integer. This is the number of lines that follow. Each of the following lines will have a (decimal) input base followed by a (decimal) output base followed by a number expressed in the input base. Both the input base and the output base will be in the range from 2 to 62. That is (in decimal) A = 10, B = 11, ..., Z = 35, a = 36, b = 37, ..., z = 61 (0-9 have their usual meanings). nOutputnnThe output of the program should consist of three lines of output for each base conversion performed. The first line should be the input base in decimal followed by a space then the input number (as given expressed in the input base). The second output line should be the output base followed by a space then the input number (as expressed in the output base). The third output line is blank. nSample Inputnn8n62 2 abcdefghizn10 16 1234567890123456789012345678901234567890n16 35 3A0C92075C0DBF3B8ACBC5F96CE3F0AD2n35 23 333YMHOUE8JPLT7OX6K9FYCQ8An23 49 946B9AA02MI37E3D3MMJ4G7BL2F05n49 61 1VbDkSIMJL3JjRgAdlUfcaWjn61 5 dl9MDSWqwHjDnToKcsWE1Sn5 10 42104444441001414401221302402201233340311104212022133030nSample Outputnn62 abcdefghizn2 11011100000100010111110010010110011111001001100011010010001nn10 1234567890123456789012345678901234567890n16 3A0C92075C0DBF3B8ACBC5F96CE3F0AD2nn16 3A0C92075C0DBF3B8ACBC5F96CE3F0AD2n35 333YMHOUE8JPLT7OX6K9FYCQ8Ann35 333YMHOUE8JPLT7OX6K9FYCQ8An23 946B9AA02MI37E3D3MMJ4G7BL2F05nn23 946B9AA02MI37E3D3MMJ4G7BL2F05n49 1VbDkSIMJL3JjRgAdlUfcaWjnn49 1VbDkSIMJL3JjRgAdlUfcaWjn61 dl9MDSWqwHjDnToKcsWE1Snn61 dl9MDSWqwHjDnToKcsWE1Sn5 42104444441001414401221302402201233340311104212022133030nn5 42104444441001414401221302402201233340311104212022133030n10 1234567890123456789012345678901234567890

没有更多推荐了,返回首页