• 转负二进制


     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 }
     1 //POJ 3191
     2 #include<stdio.h>
     3 #include<string.h>
     4 #include<algorithm>
     5 using namespace std;
     6 int ans[1000];
     7 int main()
     8 {
     9     int a;
    10     while(~scanf("%d",&a)){
    11         if(a==0){
    12             printf("0
    ");
    13             continue;
    14         }
    15         int cont=0;
    16         while(a){
    17             ans[cont++]=abs(a%(-2));
    18             a-=abs(a%(-2));
    19             a/=(-2);
    20         }
    21         for(int i=cont-1;i>=0;i--)
    22             printf("%d",ans[i]);
    23         printf("
    ");
    24     }
    25     return 0;
    26 }
  • 相关阅读:
    python之模块cmath
    python之模块chunk,了解即可
    基于python2【重要】怎么自行搭建简单的web服务器
    【重要】python之模块CGI 通用网关接口
    python之模块calendar(汇集了日历相关的操作)
    python之模块base64
    python之模块array
    python之函数用法setdefault()
    python之函数用法fromkeys()
    python之函数用法get()
  • 原文地址:https://www.cnblogs.com/shixinzei/p/7286497.html
Copyright © 2020-2023  润新知