UVa 446 - Kibbles "n" Bits "n" Bits "n" Bits
来源:互联网 发布:河南软件开发公司 编辑:程序博客网 时间:2024/05/21 19:38
题目:给你两个十六进制数字的加法或减法计算式,把他们转化成二进制,并输出十进制结果。
分析:模拟、字符串。十六进制和十进制是c语言内置的数据类型,可以直接应用,不过没有二进制。
所以,二进制要自己转化。
方法一:可以利用16进制读取数据(%x),然后,转化成二进制,可直16进制计算10进制输出。
方法二:可以利用字符串读入16进制数据,转化成10进制,然后再转化成二进制。
因为转化成二进制,如果是负数,存在补码转化问题,所以这里采用方案二。
注意:负数二进制要用补码的方式表示。
#include <iostream>#include <cstdlib>#include <cstdio>using namespace std;char c,A[5],B[5];int temp[20];void to2( int a ){int flag = 0;if ( a < 0 ) {a = -a;flag = 1;}for ( int i = 0 ; i <= 12 ; ++ i )temp[i] = 0;int count = 0;while ( a ) {temp[count ++] = a%2;a /= 2;}if ( flag ) {for ( int i = 0 ; i <= 12 ; ++ i )temp[i] = !temp[i];temp[0] ++;for ( int i = 0 ; i <= 12 ; ++ i )if ( temp[i] == 2 ) {temp[i] = 0;temp[i+1] ++;}}count = 12;while ( count >= 0 )printf("%d",temp[count --]);return;}int ctoi( char a ){if ( a >= '0' && a <= '9' )return a-'0';if ( a >= 'A' && a <= 'Z' )return a-'A'+10;if ( a >= 'a' && a <= 'z' )return a-'a'+10; }int to10( char *A ){int i = 0,flag = 1,number = 0;if ( A[0] == '-' ) {flag = -1;i = 1;}for ( ; A[i] ; ++ i ) {number *= 16;number += ctoi(A[i]);}return flag*number;}int main(){int n,a,b;while ( cin >> n )while ( n -- ) {cin >> A >> c >> B;a = to10(A);to2(a);printf(" %c ",c);b = to10(B);to2(b);if ( c == '+' )printf(" = %d\n",a+b);else printf(" = %d\n",a-b);}return 0;}
0 0
- UVa 446 - Kibbles "n" Bits "n" Bits "n" Bits
- UVA_446_Kibbles "n" Bits "n" Bits "n" Bits
- Number of 1 Bits n&(n-1)的妙用
- Bits
- Bits*
- #leetcode#191 Number of 1 Bits n&n-1的用处
- uva 11645 Bits
- UVA 11645 Bits
- UVA - 11645 Bits
- Uva - 12545 - Bits Equalizer
- uva 12545Bits Equalizer
- uva 12545 - Bits Equalizer
- UVA 12545 - Bits Equalizer
- UVA 12545 Bits Equalizer
- Bits Equalizer UVA
- Bits Equalizer UVA
- 编写函数实现:clear all but least signficant n bits of x
- Count total set bits in all numbers from 1 to n
- “bash: fork: Resource temporarily unavailable”的解决方案
- TestStackOfInteger
- 安装mysql5.6.17遇到安装意外终止 1067
- hdu 1264 Counting Squares(扫描线)
- 老三篇原文
- UVa 446 - Kibbles "n" Bits "n" Bits "n" Bits
- Android-Popupwindow和Dialog做弹出窗口
- mybatis学习笔记之trim标签---小笔记
- AVR之BOOTLOADER技术详解
- KMP字符串匹配算法学习总结
- Eclipse配置SVN
- HTML5的Canvas画图模拟太阳系运转
- JAVA中JButton常用设置
- 黑马程序员-----------------7K面试-银行调度