java 大数加法

import java.math.*;//含有大数包
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
while(sc.hasNext()){
BigInteger a=sc.nextBigInteger();//创建大数 a,b;
BigInteger b=sc.nextBigInteger();
System.out.print(a.add(b));//大数方法用add()
}
}
}

大数加法

04-14

package Others;rnrnpublic class TestBignum rn public static void main(String args[]) rn Print p = new Print();rn Bignum a = new Bignum("123456");rn Bignum b = new Bignum("123456");rn Bignum c = a.add(b);rn /*rn for(int i = 0;i < c.length();i++) rn System.out.println((int)c.ch[i]);rn rn */rn p.printBignum(c);rn rnrnrnrnpackage Others;rnpublic class Print rn void print(Object s) rn System.out.print(s + "");rn rn void println(Object s) rn System.out.println(s + "");rn rn rn public String toString() rn return "Ok";rn rn void println() rn System.out.println();rn rn void printBignum(Bignum a) rn for(int i = 0;i < a.length();i++) rn print(a.getChar(i) + " ");rn rn println();rn rnrnrnrnpackage Others;rnrnpublic class Bignum rn //chInt接收字符串参数rn private char[] chInt;rn //ch接受chInt的字符rn char[] ch;rn Bignum(String s) rn chInt = s.toCharArray();rn ch = new char[chInt.length];rn for(int i = 0;i < chInt.length;i++) rn //把char类型看做intrn ch[i] = (char)(chInt[i] - 48);rn //System.out.println((int)ch[i]);rn rn //chInt = null;rn rn //返回ch的长度rn int length() rn return ch.length;rn rn //返回数组字符rn int getChar(int i) rn //System.out.println((int)ch[i]);rn return (int)ch[i];rn rn //实现加法rn Bignum add(Bignum b) rn int max = length() > b.length() ? length() : b.length();rn boolean flag = false;rn char[] plus = new char[max + 1];rn plus[0] = ' ';rn for(int i = max - 1;i >= 0;i--) rn plus[i + 1] = (char)(ch[i] + b.ch[i]);rn //System.out.print((int)plus[i + 1] + " ");rn if(flag) rn plus[i + 1] += 1;rn rn if(plus[i + 1] >= 10) rn plus[i + 1] -= 10;rn flag = true;rn else rn flag = false;rn rn rn if(flag) rn plus[0] = 1;rn rn /*rn for(int i = 0;i < plus.length;i++) rn if(' ' != (int)plus[i])rn System.out.print(" " + (int)plus[i]);rn rn */rn String str = "";rn for(int i = 0;i < plus.length;i++) rn if(' ' == plus[i]) rn continue;rn rn str += plus[i];rn rn char[] ch = str.toCharArray();rn for(int i = 0;i < ch.length;i++) rn System.out.print((int)ch[i]);rn rn rn return new Bignum(str);rn //return new Bignum("123");rn rn rnrn这三段代码是一个大数加法的运算,打印的结果十分的诡异,不知道错到哪了??望各位大牛指点一下

大数加法的实现

12-11

自己写了一个大数相加的程序,rn程序可以运行,但是遇到如下问题,rn#include rn#include rn#include rn# define DIGIT 100rnrnchar * AddOperation( char * a1, char * a2 )rnrn int next = 0;rn int i = 0;rn int count1 = strlen( a1 );rn int count2 = strlen( a2 );rnrn char result[ DIGIT ];rnrn while( *a1 != '\0' )rn a1 ++;rn while( *a2 != '\0' )rn a2 ++;rnrn a1 --;rn a2 --;rn rn while( ( count1 > 0 ) && ( count2 > 0 ) )rn rn if( ( * a1 - 48 ) + ( * a2 - 48 ) + next > 9 )rn rn result[ i ] = (char)( ( * a1 - 48 ) + ( * a2 - 48 ) + next - 10 ) + '0';rn next = 1;rn rn elsern rn result[ i ] = (char)( ( * a1 - 48 ) + ( * a2 - 48 ) + next ) + '0';rn next = 0;rn rnrn rn count1 --;rn a1 --;rn count2 --;rn a2 --;rn i ++;rn rnrn while( count1 > 0 )rn rn if( ( * a1 - 48 ) + next > 9 )rn rn result[ i ] = (char)( ( * a1 - 48 ) + next - 10 ) + '0';rn next = 1;rn rn elsern rn result[ i ] = (char)( ( * a1 - 48 ) + next ) + '0';rn next = 0;rn rn count1 --;rn a1 --;rn i ++;rn rnrn while( count2 > 0 )rn rn if( ( * a2 - 48 ) + next > 9 )rn rn result[ i ] = (char)( ( * a2 - 48 ) + next - 10 ) + '0';rn next = 1;rn rn elsern rn result[ i ] = (char)( ( * a2 - 48 ) + next ) + '0';rn next = 0;rn rnrn count2 --;rn a2 --;rn i ++;rn rnrn result[ i ] = '\0';rnrn for( i = strlen( result ) - 1; i >= 0; i-- )rn printf( "%c", result[ i ] );rn printf( "\n" );rn //printf( "%s\n", result );rnrn return result;rnrnrnint main( void )rnrn char * add1;rn char * add2;rn char * result;rnrn char adder1[ DIGIT ];rn char adder2[ DIGIT ];rnrn puts( "Please input adder1" );rn add1 = gets_s( adder1, 100 );rnrn puts( "Please input adder2" );rn add2 = gets_s( adder2, 100 );rnrn printf( "The result of %s + %s is\n", add1, add2 );rn rn result = AddOperation( add1, add2 );rn //printf( "%s\n", result );rn system( "pause" );rn return 0;rnrnrn如果在主函数中通过注释掉的这一行来输出的话,当输入的加数位数大于3位时,会出现乱码情况,请问为什么?rn目前是反向输出,还没改。rn[img=http://img.my.csdn.net/uploads/201212/11/1355164655_6372.png][/img]

一个大数加法计算器的问题

03-30

普通的计算器和计算机只能实现一定位数的加减法,超出了范围就会溢出,大数计算器就是要使用链表来实现一个可以计算好多位的整数的加减法rn大数计算器,实现一个大数加法、减法计算器,用单向链表或者双向链表实现,每个节点存储一个3位数,并且实现打印一个大数的函数,rn输入一个运算数:123,456,789,123,456,789,123,456,789rn输入运算符:+rn输入另一个运算数:123,456,789,123,456rn结果:123,456,789,580,245,912,580,245rnrn我的代码如下: 可是出现了p指针没有初始化的问题,请各位大神多多指点~rn#includernusing namespace std;rnrnstruct numberrnrn int n;rnrn number*next1;rn number*next2;rn;rnrnnumber*AddNode(number*&head)rnrn number*s;rn number*p;rn s=new number;rn cin>>s->n;rn if(s->n<100||s->n>999)rn rn s->n=0;rn cout<<"请输入一个三位数";rn cin>>s->n;rn rn while(s->n!=0)rn rn if(head=NULL)rn head=s;rn elsern rn p->next1=s;rn s->next2=p;rn rn p=s;rn s=new number;rn cin>>s->n;rn if(s->n<100||s->n>999)rn rn s->n=0;rn cout<<"请输入一个三位数";rn cin>>s->n;rn rn rn p->next1=NULL;rn delete s;rn return p;rnrnrnvoid Add(number*&head1,number*&head2)rnrn while(head1||head2)rn rn int r=head1->n+head2->n;rn if(r>1000)rn rn head1->n=r-1000;rn head1=head1->next2;rn head1->n=head1->n+1;rn head2=head2->next2;rn rn elsern rn head1->n=r;rn head1=head1->next2;rn head2=head2->next2;rn rn rnrnrnint CountNode(number*head)rnrn int count=0;rn while(head)rn rn head=head->next1;rn count++;rn rn return count;rnrnrnrnrnrnvoid main()rnrn number*head11;rn number*head12;rn head12=AddNode(head11);//建立一个双向链表 两头分别为head11,head12rnrn number*head21;rn number*head22;rn head22=AddNode(head21);//建立一个双向链表 两头分别为head21,head22rnrn int c1,c2;rn c1=CountNode(head11);//比较两个链表的长度,并且将长的链表设为head12,短的设为head22rn c2=CountNode(head21);rn if(c1n<<" ";rn head11=head11->next1;rn rnrnrn rnrnrn

c大数加法问题,求救!

10-18

老板要求你和 Tom 一起编写一个可以进行大数(不超过500位数字)四则运算的计算器。很显然,这样的大数已经无法用 C 语言中的常规变量直接表示,而你们必须要实现高精度计算的算法。 rnrn考虑到加法是各种运算的基础,你们决定从加法开始写起。你和 Tom 进行了分工,他负责编写程序的主函数,而你要编写 add 和 revnum 两个子函数。你们约定了函数的接口如下: rnrnvoid add(char a[], char b[], char r[]);其中,a 表示被加数、b 表示 加数,而 r 中应当存放 a 和 b 两个函数的和。这三个数字都是使用字符数组来表示的,该大数的各位数字的字符由低至高存储在数组中。譬如被加数 a 为 6894 时, rna[0] = '4', a[1] = '9', a[2] = '8', a[3] = '6', a[4...500] = '\0'为了将数字由高位在前的存储方式转变为低位在前的存储方式,方便模拟加法的计算,Tom 请你再编写一个 revnum 函数,该函数的接口如下: rnrnvoid revnum(char *num);这个函数应当可以把字符数组 num 中存储的字符颠倒,即把 "1234" 转换成 "4321",参数 num 是一个以 '\0' 结尾的字符串。 rnrn输入 rn该程序的输入包括 n (n >= 0) 对大数,每对大数一行,两个大数之间以空白符分割。 rnrn输出 rn针对每一对大数,输出运算结果。 rnrn注意 rn程序的主函数Tom已经编写好,你只需提交编写好的两个子函数即可。rn问题补充:预设代码如下 rnrn/* PRESET CODE BEGIN - NEVER TOUCH CODE BELOW */ rnrn#include rn#include rnrnvoid revnum(char *num); rnvoid add(char a[], char b[], char c[]); rnrnint main() rn rnchar a[501], b[501], r[501]; rnint n; rnrnfor(;;) rnmemset(a, 0, sizeof(a)); rnmemset(b, 0, sizeof(b)); rnmemset(r, 0, sizeof(r)); rnrnn = scanf("%s %s\n", a, b); rnif (n != 2) break; rnrnrevnum(a); rnrevnum(b); rnadd(a, b, r); rnrevnum(r); rnrnprintf("%s\n", r); rn rnrnreturn 0; rn rnrn/* PRESET CODE END - NEVER TOUCH CODE ABOVE*/

新人大数加法求解

12-20

Problem DescriptionrnI have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.rn rnInputrnThe first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line consists of two positive integers, A and B. Notice that the integers are very large, that means you should not process them by using 32-bit integer. You may assume the length of each integer will not exceed 1000.rn rnOutputrnFor each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line is the an equation "A + B = Sum", Sum means the result of A + B. Note there are some spaces int the equation. Output a blank line between two test cases.rn rnSample Inputrn2rn1 2rn112233445566778899 998877665544332211rn rnSample OutputrnCase 1:rn1 + 2 = 3rnrnCase 2:rn112233445566778899 + 998877665544332211 = 1111111111111111110rn ----------------------------------------------------------------------------------------------------------------------------------------rn以上题目,以下是我写的代码rnrn[code=c]#include rn#include rnint main()rnrn char a[1001],b[1001]; //a b 两个数 rn int c,i,j,k,s[1001]; //c 进位 i,j,k 下标 s[1001] 相加后的数 rn int n,count=1; //n 需输入组数 count 当前组数 rn scanf("%d%*c",&n);rn while(n-->0)rn rn scanf("%s%s",&a,&b);rn k=0;rn c=0;rn for(i=strlen(a)-1,j=strlen(b)-1;i>=0&&j>=0;i--,j--)rn rn s[k++]=(a[i]-'0'+b[j]-'0'+c)%10;rn c=(a[i]-'0'+b[j]-'0'+c)/10;rn rn if(i>=0) //如果a数长 rn for(;i>=0;i--)rn rn s[k++]=(a[i]-'0'+c)%10;rn c=(a[i]-'0'+c)/10;rn rn else //如果b数长rn for(;j>=0;j--)rn rn s[k++]=(b[j]-'0'+c)%10;rn c=(b[j]-'0'+c)/10;rn rn if(c) //最后进位 rn s[k++]=c;rn printf("\n#Case %d:\n%s + %s = ",count++,a,b);rn for(k=k-1;k>=0;k--)rn printf("%d",s[k]);rn printf("\n");rn rn return 0;rn[/code]rnrn请问各位前辈哪里错了,实在找不出,先谢谢各位前辈了~~~~~~~~

杭电ACM大数加法题目

09-23

A + B Problem IIrnTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)rnTotal Submission(s): 124156 Accepted Submission(s): 23887rnrnrnProblem DescriptionrnI have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.rnrn rnrnInputrnThe first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line consists of two positive integers, A and B. Notice that the integers are very large, that means you should not process them by using 32-bit integer. You may assume the length of each integer will not exceed 1000.rnrn rnrnOutputrnFor each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line is the an equation "A + B = Sum", Sum means the result of A + B. Note there are some spaces int the equation. Output a blank line between two test cases.rnrn rnrnSample Inputrn2rn1 2rn112233445566778899 998877665544332211rn rnrnSample OutputrnCase 1:rn1 + 2 = 3rnrnCase 2:rn112233445566778899 + 998877665544332211 = 1111111111111111110rn rnrnAuthorrnIgnatius.Lrn我写的代码:请各位帮忙看一下为什么是Wrong Answer……rn [code=C/C++]rn#includern#includernusing namespace std;rn#define Max 1000rnint num1[Max];rnint num2[Max];rnrnstruct bigaddrnrn string str1;rn string str2;rn int * result;rn int len;rn;rnrnint main()rnrn string str1,str2;rn int i;rn bool flag=false;rn int casenumber,len1,len2,len,hold=0;rn bigadd * arr;rn memset(num1,0,sizeof(num1));rn memset(num2,0,sizeof(num2));rn cin>>casenumber;rn arr = new bigadd[casenumber];rn for(i=0;i>str1>>str2;rn arr[i].str1=str1;rn arr[i].str2=str2;rn len1=str1.length();rn len2=str2.length();rn for(int k=len1-1;k>=0;k--)rn rn num1[len1-1-k]=str1[k]-'0';rn rn for(int j=len2-1;j>=0;j--)rn rn num2[len2-1-j]=str2[j]-'0';rn rn len=len1>len2?len1:len2;rn arr[i].len=len;rn arr[i].result=new int[len+1];rn for(int k=0;k<=len;k++)rn rn num1[k]+=num2[k]+hold;rn if(num1[k]>=10)rn rn hold=num1[k]/10;rn num1[k]=num1[k]%10;rn rn elsern hold=0;rn rn for(int l=0;l<=len;l++)rn rn arr[i].result[l]=num1[l];rn rn memset(num1,0,sizeof(num1));rn memset(num2,0,sizeof(num2));rn rn for( i=0;i=0;j--)rn rn if(arr[i].result[j]||flag)rn rn flag=true;rn cout<

杭电 ACM 1047题 大数加法

07-20

杭州电子科技大学ACM网站上的1047题无法提交成功。题目如下:rnrnrnProblem DescriptionrnOne of the first users of BIT's new supercomputer was Chip Diller. He extended his exploration of powers of 3 to go from 0 to 333 and he explored taking various sums of those numbers. rn``This supercomputer is great,'' remarked Chip. ``I only wish Timothy were here to see these results.'' (Chip moved to a new apartment, once one became available on the third floor of the Lemon Sky apartments on Third Street.) rnrn rnrnInputrnThe input will consist of at most 100 lines of text, each of which contains a single VeryLongInteger. Each VeryLongInteger will be 100 or fewer characters in length, and will only contain digits (no VeryLongInteger will be negative). rnrnThe final input line will contain a single zero on a line by itself.rnrn rnrnOutputrnYour program should output the sum of the VeryLongIntegers given in the input. rnrnrnThis problem contains multiple test cases!rnrnThe first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.rnrnThe output format consists of N output blocks. There is a blank line between output blocks.rnrn rnrnSample Inputrn1rnrnrn123456789012345678901234567890rn123456789012345678901234567890rn123456789012345678901234567890rn0rn rnrnSample Outputrn370370367037037036703703703670rnrn我的代码如下:rnrn#includernrn#includernchar s[101];rnint sum[110];rnint main()rnrn int i,j,k;rn scanf("%d",&k); rn while(k>0)rn rn for(i=0;i<110;i++)rn sum[i]=0;rn while(gets(s),strcmp(s,"0"))rn rn for(i=(j=strlen(s))-1;i>=0;i--)rn sum[j-1-i]+=(s[i]-'0');rn sum[j-i]+=sum[j-1-i]/10; rn sum[j-1-i]=sum[j-1-i]%10; rn rn rn j=110; rn while(!sum[j])rn j--;rn if(j<0)rn printf("0");rn for(i=j;i>=0;i--)rn printf("%d",sum[i]);rn printf("\n");rn if(k!=1)printf("\n");rn k--; rn rn return 1;rnrn请教各位高手为何无法成功提交?

杭电oj的1002(大数加法),哪错了?

03-21

题目:rnrnProblem DescriptionrnI have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.rn rnrnInputrnThe first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line consists of two positive integers, A and B. Notice that the integers are very large, that means you should not process them by using 32-bit integer. You may assume the length of each integer will not exceed 1000.rn rnrnOutputrnFor each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line is the an equation "A + B = Sum", Sum means the result of A + B. Note there are some spaces int the equation. Output a blank line between two test cases.rn rnrnSample Inputrn2rn1 2rn112233445566778899 998877665544332211rn rnrnSample OutputrnCase 1:rn1 + 2 = 3rnrnCase 2:rn112233445566778899 + 998877665544332211 = 1111111111111111110rn rn我的程序:rn[code=C/C++]rn#include rn#include rn#define N 1000rnint main(void)rnrn char a[N], b[N];rn int c[N+1]=0;rn int t;rn scanf("%d", &t);rn int loops;rn for(loops = 1; loops<=t; loops++) rn scanf("%s %s", a, b);rn int la = strlen(a);rn int lb = strlen(b);rn int length = (la > lb ? la : lb)+1;rn int i, j, k=0;rn for(i=la-1, j=lb-1; i>=0&&j>=0; i--, j--) rn c[k++] = a[i]-'0'+b[j]-'0';rn rn while(i>=0)rn c[k++] = a[i--]-'0';rn while(j>=0)rn c[k++] = b[j--]-'0';rn int carrier = 0;rn for(i=0; i 9) rn c[i] %= 10;rn carrier = 1;rn rn rn printf("Case %d:\n", loops);rn printf("%s + %s = ", a, b);rn if(carrier == 1)rn printf("1");rn for(i=k-1; i>=0; i--)rn printf("%d", c[i]);rn printf("%s", loops==t ? "\n" : "\n\n");rn rn return 0;rnrn[/code]

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试