zoukankan      html  css  js  c++  java
  • 计蒜客---N的-2进制表示

    对于十进制整数N,试求其-2进制表示。 例如,因为  1*1  +  1*-2  +  1*4  +  0*-8  +1*16  +  1*-32  =  -13  ,所以(-13)_10  =  (110111)_-2。

    输入一个整数,代表要转换的十进制数。

    输出一个整数,代表N的-2进制表示。

    |N|  < =  2000000000

    样例输入

    -13
    

    样例输出

    110111

     1 #include <iostream>
     2 #include <algorithm>
     3 #include <cstring>
     4 #include <cstdio>
     5 #include <vector>
     6 #include <cstdlib>
     7 #include <iomanip>
     8 #include <stdio.h>
     9 #include <cmath>
    10 #include <ctime>
    11 #include <map>
    12 #include <set>
    13 #include <stack>
    14 #include <queue>
    15 #include <stdlib.h>
    16 using namespace std;
    17 #define lowbit(x) (x&(-x))
    18 #define max(x,y) (x>y?x:y)
    19 #define min(x,y) (x<y?x:y)
    20 #define MAX 100000000000000000
    21 #define MOD 1000000007
    22 #define pi acos(-1.0)
    23 #define ei exp(1)
    24 #define PI 3.141592653589793238462
    25 #define INF 0x3f3f3f3f3f
    26 #define mem(a) (memset(a,0,sizeof(a)))
    27 typedef long long ll;
    28 ll gcd(ll a,ll b){
    29     return b?gcd(b,a%b):a;
    30 }
    31 bool cmp(int x,int y)
    32 {
    33     return x>y;
    34 }
    35 const int N=10005;
    36 const int mod=1e9+7;
    37 
    38 int main(){
    39     int n;
    40     cin>>n;
    41     stack<int> s;
    42     if(n==0){
    43         cout<<0;
    44         return 0;
    45     }
    46     while(n){
    47         if(n%-2<0){
    48             s.push(n%-2+ 2);
    49             n=n/-2+1;
    50         }else{
    51             s.push(n%-2);
    52             n/=-2;
    53         }
    54     }
    55     while(!s.empty()){
    56         cout<<s.top();
    57         s.pop();
    58     }
    59     return 0;
    60 }
  • 相关阅读:
    如果int x=20, y=5,则语句System.out.println(x+y +""+(x+y)+y); 的输出结果是()
    子父类存在同名成员时super的使用条件
    7mysql高级查询
    1udp编程
    6mysql外键
    4mysql数据表增删改查
    5mysql数据类型
    3mysql数据库操作
    2mysql基本使用
    1mysql安装
  • 原文地址:https://www.cnblogs.com/shixinzei/p/7281921.html
Copyright © 2011-2022 走看看