zoukankan      html  css  js  c++  java
  • 十进制转二进制,倒置后输出十进制数

    C练习: 十进制转二进制,倒置后输出十进制数

    /*
     ============================================================================
     Name        : Test1.c
     Author      : lixiaolong
     Version     : v1.0
     Copyright   : Your copyright notice
     Description : Hello World in C, Ansi-style
     ============================================================================
     */
    
    #include <stdio.h>
    #include <math.h>
    void main(){
     int a[32]={0},i,j,k,len,tem,number=0;
     long num;i=0;
     printf("请输入一个十进制数:");
     scanf("%ld",&num);
     printf("%ld的二进制数为:",num);
     while(num>=1){
      a[i]=num%2;
      num=num/2;
      i++;
     }
     len=i;
     j=i;
     k=i;
     for(i=i-1;i>=0;i--){
      printf("%d",a[i]);
     }
     printf("
    ");
    
     //进行倒置
     for(;i<len/2;i++){
      tem = a[i];
      a[i] = a[j-i-1];
      a[j-i-1] = tem;
    
     }
    
     for(k=k-1;k>=0;k--){
      printf("%d",a[k]);
     }
    
     //计算倒置后的十进制数
     for(;k<len;k++){
      number += a[k]*(pow(2,k));
     }
     printf("二进制数倒置后的十进制数为:%d
    ",number);
    }
  • 相关阅读:
    [JSOI2015]最小表示
    [洛谷2002]消息扩散
    [洛谷1726]上白泽慧音
    [CodeVS2822]爱在心中
    [POJ2186]Popular Cows
    [洛谷1991]无线通讯网
    [CQOI2009]跳舞
    [洛谷1342]请柬
    [USACO07JAN]Balanced Lineup
    [NOIp2003提高组]神经网络
  • 原文地址:https://www.cnblogs.com/kingshow123/p/3361691.html
Copyright © 2011-2022 走看看