POJ 2845 01000001 二进制加法套用高精度加法运算代码

    技术2022-07-03  142

     

    http://poj.org/problem?id=2845、 刚刚开始用的转换方法,例子都过了但是总是WA,现在还不清楚哪里的问题 然后用了高精度加法AC,但是注意输出的时候前导0和结果为0的情况 01000001 Time Limit: 1000MS Memory Limit: 65536K

    Description

    Adding binary numbers is a very simple task, and very similar to the longhand addition of decimal numbers. As with decimal numbers, you start by adding the bits (digits) one column at a time, from right to left. Unlike decimal addition, there is little to memorize in the way of rules for the addition of binary bits:

    0 + 0 = 0 1 + 0 = 1 0 + 1 = 1 1 + 1 = 10 1 + 1 + 1 = 11

    Just as with decimal addition, when the sum in one column is a two-bit (two-digit) number, the least significant figure is written as part of the total sum and the most significant figure is “carried” to the next left column. Consider the following examples:

    11 1 <-- Carry bits --> 1 11 1001101 1001001 1000111+ 0010010 + 0011001 + 1010110 -------- --------- --------- 1011111 1100010 10011101

    The addition problem on the left did not require any bits to be carried, since the sum of bits in each column was either 1 or 0, not 10 or 11. In the other two problems, there definitely were bits to be carried, but the process of addition is still quite simple.

    Input

    The first line of input contains an integer  N, ( 1 ≤ N ≤ 1000), which is the number of binary addition problems that follow. Each problem appears on a single line containing two binary values separated by a single space character. The maximum length of each binary value is 80 bits (binary digits). Note: The maximum length result could be 81 bits (binary digits).

    Output

    For each binary addition problem, print the problem number, a space, and the binary result of the addition. Extra leading zeroes must be omitted.

    Sample Input

    3 1001101 10010 1001001 11001 1000111 1010110

    Sample Output

    1 1011111 2 1100010 3 10011101 高精度加法代码 /* Author : yan * Question : POJ 2845 01000001 * Data && Time : Monday, January 10 2011 01:00 PM * Compiler : gcc (Ubuntu 4.4.3-4ubuntu5) 4.4.3 */ #include<stdio.h> #define MAX 83 char add1[MAX]; char add2[MAX]; char result[MAX+1]; void add(char *s1,char *s2) { char a[MAX],b[MAX]; int i,Len,Len1,Len2,k; memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); memset(result,0,sizeof(result)); Len1=strlen(s1); Len2=strlen(s2); if(Len1>Len2) Len=Len1; else Len=Len2; k=0; for(i=Len1-1;i>=0;i--) a[++k]=s1[i]-'0'; k=0; for(i=Len2-1;i>=0;i--) b[++k]=s2[i]-'0'; for(i=1;i<=Len;i++) { a[i]=a[i]+b[i]; if (a[i]>=2) { a[i]=a[i]-2; a[i+1]++; } } if(a[Len+1]==0) Len--; k=0; for(i=Len+1;i>=1;i--) result[k++]=a[i]+'0'; } int main() { //freopen("input","r",stdin); int test; int cnt=1; int index; scanf("%d",&test); while(test--) { index=0; scanf("%s %s",add1,add2); add(add1,add2); printf("%d ",cnt++); while(result[index]=='0') index++; if(result[index]=='/0') printf("0"); else { while(result[index]!='/0') printf("%c",result[index++]); } printf("/n"); } return 0; } WA代码,帮忙看看是哪里的原因 /* Author : yan * Question : POJ 2845 01000001 * Data && Time : Sunday, January 09 2011 01:20 PM * Compiler : gcc (Ubuntu 4.4.3-4ubuntu5) 4.4.3 */ #include<stdio.h> //#define long long long long long long cache1; long long cache2; char bits1[85]; char bits2[85]; void binaty_to_decimal(const char *bit,long long *value) { int len=strlen(bit); int i; for(i=0;i<len;i++) *value+=(bit[i]-'0')*pow(2,len-i-1); } void decimal_to_binary(long long value,char *bit) { short cnt=0; if(value==0) { bit[0]='0'; bit[1]='/0'; return; } while(value) { bit[cnt++]=value%2+'0'; //printf("%d",value%2); value=value/2; } int i; char c; for(i=0;i<cnt/2;i++) { c=bit[i]; bit[i]=bit[cnt-i-1]; bit[cnt-i-1]=c; } bit[cnt]='/0'; } int main() { freopen("input","r",stdin); int test; int cnt=1; int index; scanf("%d",&test); while(test--) { scanf("%s%s",bits1,bits2); cache1=cache2=0; binaty_to_decimal(bits1,&cache1); binaty_to_decimal(bits2,&cache2); decimal_to_binary(cache1+cache2,bits1); printf("%d ",cnt++); index=0; if(strlen(bits1)==1) { printf("%s/n",bits1); continue; } while(bits1[index]=='0') index++; while(bits1[index]!='/0') printf("%c",bits1[index++]); printf("/n"); //printf("%d/n",cnt++); } return 0; }

     


    最新回复(0)