HDU1002 Sum Problem 大数 Java&&C
来源:互联网 发布:鼠标质量排行 知乎 编辑:程序博客网 时间:2024/05/22 06:17
题目链接:HDU1002
套用大数模板或者用JAVA写就行,很裸的题目。
Ccode:
/*2017年9月20日16:18:17HDU1002AC 大数模板 15MS 比JAVA快很多 */# include<stdio.h> # include<string.h> # include<malloc.h> #include <iostream>#include <map>#include <set>#include <string>#include <cstring>#include <cstdio>#include <algorithm>#include <cmath>#include <queue>#include <vector> using namespace std;typedef long long ll;void add(char* a,char* b,char* c) { int i,j,k,max,min,n,temp; char *s,*pmax,*pmin; max=strlen(a); min=strlen(b); if (max<min) { temp=max; max=min; min=temp; pmax=b; pmin=a; } else { pmax=a; pmin=b; } s=(char*)malloc(sizeof(char)*(max+1)); s[0]='0'; for (i=min-1,j=max-1,k=max;i>=0;i--,j--,k--) s[k]=pmin[i]-'0'+pmax[j]; for (;j>=0;j--,k--) s[k]=pmax[j]; for (i=max;i>=0;i--) if (s[i]>'9') { s[i]-=10; s[i-1]++; } if (s[0]=='0') { for (i=0;i<=max;i++) c[i-1]=s[i]; c[i-1]='\0'; } else { for (i=0;i<=max;i++) c[i]=s[i]; c[i]='\0'; } free(s); } /*大数减法*/ void subtract(char* a,char* b,char* c) { int i,j,ca,cb; ca=strlen(a); cb=strlen(b); if (ca>cb||(ca==cb&&strcmp(a,b)>=0)) { for (i=ca-1,j=cb-1;j>=0;i--,j--) a[i]-=(b[j]-'0'); for (i=ca-1;i>=0;i--) if (a[i]<'0') { a[i]+=10; a[i-1]--; } i=0; while (a[i]=='0') i++; if (a[i]=='\0') { c[0]='0'; c[1]='\0'; } else { for (j=0;a[i]!='\0';i++,j++) c[j]=a[i]; c[j]='\0'; } } else { for (i=ca-1,j=cb-1;i>=0;i--,j--) b[j]-=(a[i]-'0'); for (j=cb-1;j>=0;j--) if (b[j]<'0') { b[j]+=10; b[j-1]--; } j=0; while (b[j]=='0') j++; i=1; c[0]='-'; for (;b[j]!='\0';i++,j++) c[i]=b[j]; c[i]='\0'; } } /* 大数乘法*/ void multiply(char* a,char* b,char* c) { int i,j,ca,cb,* s; ca=strlen(a); cb=strlen(b); s=(int*)malloc(sizeof(int)*(ca+cb)); for (i=0;i<ca+cb;i++) s[i]=0; for (i=0;i<ca;i++) for (j=0;j<cb;j++) s[i+j+1]+=(a[i]-'0')*(b[j]-'0'); for (i=ca+cb-1;i>=0;i--) if (s[i]>=10) { s[i-1]+=s[i]/10; s[i]%=10; } i=0; while (s[i]==0) i++; for (j=0;i<ca+cb;i++,j++) c[j]=s[i]+'0'; c[j]='\0'; free(s); } /*大数除法,返回余数*/ int dividor(char* a,int b,char* c) { int i,j,temp=0,n; char* s; n=strlen(a); s=(char*)malloc(sizeof(char)*(n+1)); for (i=0;a[i]!=0;i++) { temp=temp*10+a[i]-'0'; s[i]=temp/b+'0'; temp%=b; } s[i]='\0'; for (i=0;s[i]=='0'&&s[i]!='\0';i++); if (s[i]=='\0') { c[0]='0'; c[1]='\0'; } else { for (j=0;s[i]!='\0';i++,j++) c[j]=s[i]; c[j]='\0'; } free(s); return temp; } const int maxn=1010;char a[maxn],b[maxn],c[maxn];int main(){int t;scanf("%d",&t);//int kase=0;for(int i=1;i<=t;i++){scanf("%s%s",&a,&b);printf("Case %d:\n",i) ;add(a,b,c);printf("%s + %s = %s\n",a,b,c);if(i!=t) printf("\n");}return 0;}
/* * 2017年9月20日16:19:28 * HDU1002 * 大 数 327ms * AC*/import java.io.*;import java.util.*;import java.math.*;public class Main {public static void main(String []args){Scanner cin=new Scanner(new BufferedInputStream(System.in));PrintWriter cout=new PrintWriter(System.out);int t=cin.nextInt();for(int i=1;i<=t;i++){cout.println("Case "+i+":");BigInteger a,b,c;a=cin.nextBigInteger();b=cin.nextBigInteger();c=a.add(b);cout.println(a+" + "+b+" = "+c);if(i!=t)cout.println("");}cout.flush();}}
阅读全文
0 0
- HDU1002 Sum Problem 大数 Java&&C
- HDU1002 Sum Problem
- hdu1002 JAVA大数
- java大数处理hdu1002
- hdu1002 A + B Problem II(大数相加)
- hdu1002 A + B Problem II(大数题)
- hdu1002 A + B Problem II 大数
- HDU1002 A + B Problem II【大数】
- HDU1002(大数)
- 哈哈,第一个Java的大数HDU1002
- HDU1002 A + B Problem II--大数相加的应用
- ACM-大数之A + B Problem II——hdu1002
- A + B Problem II(hdu1002,大数加法)
- HDU1002 A + B Problem II JAVA实现
- HDU1002 大数相加
- 大数相加hdu1002
- HDU1002 大数相加
- HDU1002大数相加问题
- 设计模式之观察者模式
- spring中BeanFactory源码解读
- spring boot 测试单元修改数据库不成功
- 680. Valid Palindrome II
- Java 函数可变参数
- HDU1002 Sum Problem 大数 Java&&C
- Sqlite特点
- 【CSS揭秘】案例3、灵活的背景定位
- hive select报错 Unable to determine if hdfs
- 2017-09-20表格
- java中的DBUtils的使用
- 抽象类和接口的区别
- touch事件解析
- Linux GPIO 的注册与申请