zoukankan      html  css  js  c++  java
  • Atcode ABC105-C:Base -2 Number

    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;
    }
  • 相关阅读:
    SGU 495 Kids and Prizes 概率DP 或 数学推理
    poj 2799 IP Networks 模拟 位运算
    uva 202 Repeating Decimals 模拟
    poj 3158 Kickdown 字符串匹配?
    uva 1595 Symmetry 暴力
    uva 201 Squares 暴力
    uva 1594 Ducci Sequence 哈希
    uva 1368 DNA Consensus String 字符串
    数字、字符串、列表的常用操作
    if条件判断 流程控制
  • 原文地址:https://www.cnblogs.com/Friends-A/p/10324400.html
Copyright © 2011-2022 走看看