手写 大整数

 

 http://codevs.cn/problem/1166/

区间dp, 手写大整数 

 

  1 #include
  2 #define mt(a,b) memset(a,b,sizeof(a))
  3 using namespace std;
  4 typedef long long LL;
  5 const int M=1e2+10;
  6 
  7 class BigInteger{
  8     static const int M=1e2+10;
  9     char s[M];
 10     int length;
 11 public:
 12     void init(LL x){
 13         if(x==0){
 14             s[0]='0';
 15             length=1;
 16             return ;
 17         }
 18         length=0;
 19         while(x){
 20             s[length]=x%10+'0';
 21             x/=10;
 22             length++;
 23         }
 24     }
 25     bool operator <(const BigInteger &b) const{
 26         if(lengthreturn true;
 27         if(length>b.length) return false;
 28         for(int i=length-1;i>=0;i--){
 29             if(s[i]return true;
 30             if(s[i]>b.s[i]) return false;
 31         }
 32         return false;
 33     }
 34     BigInteger operator +(const BigInteger &b) const{
 35         int buffer1[M];
 36         int buffer2[M];
 37         for(int i=0;i){
 38             buffer1[i]=0;
 39             buffer2[i]=0;
 40         }
 41         for(int i=0;i){
 42             buffer1[i]=s[i]-'0';
 43         }
 44         for(int i=0;i){
 45             buffer2[i]=b.s[i]-'0';
 46         }
 47         int carry=0;
 48         for(int i=0;i){
 49             int value=carry+buffer1[i]+buffer2[i];
 50             buffer1[i]=value%10;
 51             carry=value/10;
 52         }
 53         BigInteger result;
 54         result.length=1;
 55         for(int i=0;i){
 56             result.s[i]=buffer1[i]+'0';
 57             if(buffer1[i]) result.length=i+1;
 58         }
 59         return result;
 60     }
 61     BigInteger operator *(const BigInteger &b) const{
 62         int buffer1[M];
 63         int buffer2[M];
 64         int buffer3[M];
 65         for(int i=0;i){
 66             buffer1[i]=0;
 67             buffer2[i]=0;
 68             buffer3[i]=0;
 69         }
 70         for(int i=0;i){
 71             buffer1[i]=s[i]-'0';
 72         }
 73         for(int i=0;i){
 74             buffer2[i]=b.s[i]-'0';
 75         }
 76         for(int i=0;i){
 77             for(int j=0;j){
 78                 buffer3[i+j]+=buffer1[i]*buffer2[j];
 79             }
 80         }
 81         int carry=0;
 82         for(int i=0;i){
 83             int value=carry+buffer3[i];
 84             buffer3[i]=value%10;
 85             carry=value/10;
 86         }
 87         BigInteger result;
 88         result.length=1;
 89         for(int i=0;i){
 90             result.s[i]=buffer3[i]+'0';
 91             if(buffer3[i]) result.length=i+1;
 92         }
 93         return result;
 94     }
 95     void output(){
 96         for(int i=length-1;i>=0;i--){
 97             putchar(s[i]);
 98         }
 99     }
100 };
101 int n,m;
102 LL a[M][M];
103 BigInteger dp[M][M];
104 BigInteger two[M];
105 bool flag[M][M];
106 void init(){
107     two[0].init(1);
108     BigInteger number;
109     number.init(2);
110     for(int i=1;i){
111         two[i]=two[i-1]*number;
112     }
113 }
114 BigInteger dfs(int r,int L,int R){
115     BigInteger &d=dp[L][R];
116     if(flag[L][R]) return d;
117     flag[L][R]=true;
118     if(L==R){
119         BigInteger number;
120         number.init(a[r][L]);
121         d=number*two[m];
122         return d;
123     }
124     int time=m-(R-L+1)+1;
125     BigInteger number;
126     number.init(a[r][L]);
127     d=number*two[time]+dfs(r,L+1,R);
128     number.init(a[r][R]);
129     BigInteger buffer=number*two[time]+dfs(r,L,R-1);
130     if(dbuffer;
131     return d;
132 }
133 void solve(){
134     BigInteger answer;
135     answer.init(0);
136     for(int i=0;i){
137         mt(flag,false);
138         answer=answer+dfs(i,0,m-1);
139     }
140     answer.output();
141     puts("");
142 }
143 int main(){
144     init();
145     while(~scanf("%d%d",&n,&m)){
146         for(int i=0;i){
147             for(int j=0;j){
148                 scanf("%lld",&a[i][j]);
149             }
150         }
151         solve();
152     }
153     return 0;
154 }
View Code

 

 http://codevs.cn/problem/3116/

大整数 计算 

  1 #include
  2 #define mt(a,b) memset(a,b,sizeof(a))
  3 using namespace std;
  4 typedef long long LL;
  5 const int M=1e3+10;
  6 class BigInteger{
  7     static const int M=1e3+10;
  8     char s[M];
  9     int length;
 10 public:
 11     void init(char c[]){
 12         int lc=strlen(c);
 13         for(int i=0;i){
 14             s[i]=c[lc-i-1];
 15         }
 16         length=lc;
 17     }
 18     void init(LL x){
 19         if(x==0){
 20             s[0]='0';
 21             length=1;
 22             return ;
 23         }
 24         length=0;
 25         while(x){
 26             s[length]=x%10+'0';
 27             x/=10;
 28             length++;
 29         }
 30     }
 31     BigInteger operator +(const BigInteger &b) const{
 32         int buffer1[M];
 33         int buffer2[M];
 34         for(int i=0;i){
 35             buffer1[i]=0;
 36             buffer2[i]=0;
 37         }
 38         for(int i=0;i){
 39             buffer1[i]=s[i]-'0';
 40         }
 41         for(int i=0;i){
 42             buffer2[i]=b.s[i]-'0';
 43         }
 44         int carry=0;
 45         for(int i=0;i){
 46             int value=carry+buffer1[i]+buffer2[i];
 47             buffer1[i]=value%10;
 48             carry=value/10;
 49         }
 50         BigInteger result;
 51         result.length=1;
 52         for(int i=0;i){
 53             result.s[i]=buffer1[i]+'0';
 54             if(buffer1[i]) result.length=i+1;
 55         }
 56         return result;
 57     }
 58     BigInteger operator *(const BigInteger &b) const{
 59         int buffer1[M];
 60         int buffer2[M];
 61         int buffer3[M];
 62         for(int i=0;i){
 63             buffer1[i]=0;
 64             buffer2[i]=0;
 65             buffer3[i]=0;
 66         }
 67         for(int i=0;i){
 68             buffer1[i]=s[i]-'0';
 69         }
 70         for(int i=0;i){
 71             buffer2[i]=b.s[i]-'0';
 72         }
 73         for(int i=0;i){
 74             for(int j=0;j){
 75                 buffer3[i+j]+=buffer1[i]*buffer2[j];
 76             }
 77         }
 78         int carry=0;
 79         for(int i=0;i){
 80             int value=carry+buffer3[i];
 81             buffer3[i]=value%10;
 82             carry=value/10;
 83         }
 84         BigInteger result;
 85         result.length=1;
 86         for(int i=0;i){
 87             result.s[i]=buffer3[i]+'0';
 88             if(buffer3[i]) result.length=i+1;
 89         }
 90         return result;
 91     }
 92     void output(){
 93         for(int i=length-1;i>=0;i--){
 94             putchar(s[i]);
 95         }
 96     }
 97 }A,B,C;
 98 char a[M];
 99 char b[M];
100 int main(){
101     while(~scanf("%s%s",a,b)){
102         A.init(a);
103         B.init(b);
104         C=A+B;
105         C.output();
106         puts("");
107     }
108     return 0;
109 }
View Code

 

 

 http://codevs.cn/problem/3117/

  1 #include
  2 #define mt(a,b) memset(a,b,sizeof(a))
  3 using namespace std;
  4 typedef long long LL;
  5 const int M=1e3+10;
  6 class BigInteger{
  7     static const int M=1e3+10;
  8     char s[M];
  9     int length;
 10 public:
 11     void init(char c[]){
 12         int lc=strlen(c);
 13         for(int i=0;i){
 14             s[i]=c[lc-i-1];
 15         }
 16         length=lc;
 17     }
 18     void init(LL x){
 19         if(x==0){
 20             s[0]='0';
 21             length=1;
 22             return ;
 23         }
 24         length=0;
 25         while(x){
 26             s[length]=x%10+'0';
 27             x/=10;
 28             length++;
 29         }
 30     }
 31     BigInteger operator +(const BigInteger &b) const{
 32         int buffer1[M];
 33         int buffer2[M];
 34         for(int i=0;i){
 35             buffer1[i]=0;
 36             buffer2[i]=0;
 37         }
 38         for(int i=0;i){
 39             buffer1[i]=s[i]-'0';
 40         }
 41         for(int i=0;i){
 42             buffer2[i]=b.s[i]-'0';
 43         }
 44         int carry=0;
 45         for(int i=0;i){
 46             int value=carry+buffer1[i]+buffer2[i];
 47             buffer1[i]=value%10;
 48             carry=value/10;
 49         }
 50         BigInteger result;
 51         result.length=1;
 52         for(int i=0;i){
 53             result.s[i]=buffer1[i]+'0';
 54             if(buffer1[i]) result.length=i+1;
 55         }
 56         return result;
 57     }
 58     BigInteger operator *(const BigInteger &b) const{
 59         int buffer1[M];
 60         int buffer2[M];
 61         int buffer3[M];
 62         for(int i=0;i){
 63             buffer1[i]=0;
 64             buffer2[i]=0;
 65             buffer3[i]=0;
 66         }
 67         for(int i=0;i){
 68             buffer1[i]=s[i]-'0';
 69         }
 70         for(int i=0;i){
 71             buffer2[i]=b.s[i]-'0';
 72         }
 73         for(int i=0;i){
 74             for(int j=0;j){
 75                 buffer3[i+j]+=buffer1[i]*buffer2[j];
 76             }
 77         }
 78         int carry=0;
 79         for(int i=0;i){
 80             int value=carry+buffer3[i];
 81             buffer3[i]=value%10;
 82             carry=value/10;
 83         }
 84         BigInteger result;
 85         result.length=1;
 86         for(int i=0;i){
 87             result.s[i]=buffer3[i]+'0';
 88             if(buffer3[i]) result.length=i+1;
 89         }
 90         return result;
 91     }
 92     void output(){
 93         for(int i=length-1;i>=0;i--){
 94             putchar(s[i]);
 95         }
 96     }
 97 }A,B,C;
 98 char a[M];
 99 char b[M];
100 int main(){
101     while(~scanf("%s%s",a,b)){
102         A.init(a);
103         B.init(b);
104         C=A*B;
105         C.output();
106         puts("");
107     }
108     return 0;
109 }
View Code

 

 

 

http://codevs.cn/problem/3115/

减法 就 没 考虑进class了 ,那就复杂了 。

 1 //#define txtout
 2 //#define debug
 3 #include
 4 #define mt(a,b) memset(a,b,sizeof(a))
 5 using namespace std;
 6 typedef long long LL;
 7 const double pi=acos(-1.0);
 8 const double eps=1e-8;
 9 const int inf=0x3f3f3f3f;
10 const int M=1e3+10;
11 char a[M];
12 char b[M];
13 char answer[M];
14 void Reverse(char s[]){
15     int ls=strlen(s);
16     for(int i=0,j=ls-1;i){
17         swap(s[i],s[j]);
18     }
19 }
20 bool isBig(char s1[],char s2[]){
21     int l1=strlen(s1);
22     int l2=strlen(s2);
23     if(l1>l2) return true;
24     if(l1return false;
25     return strcmp(s1,s2)>0;
26 }
27 void solve(){
28     if(!strcmp(a,b)){
29         answer[0]='0';
30         answer[1]=0;
31         return ;
32     }
33     bool big=isBig(a,b);
34     if(!big) swap(a,b);
35     int la=strlen(a);
36     int lb=strlen(b);
37     Reverse(a);
38     Reverse(b);
39     for(int i=lb;i){
40         b[i]='0';
41     }
42     int carry=0;
43     for(int i=0;i){
44         if(a[i]-carry>=b[i]){
45             answer[i]=a[i]-carry-b[i]+'0';
46             carry=0;
47             continue;
48         }
49         int need=1;
50         while(need*10+a[i]-carry<b[i]){
51             need++;
52         }
53         answer[i]=need*10+a[i]-carry-b[i]+'0';
54         carry=need;
55     }
56     int len=0;
57     for(int i=la-1;i>=0;i--){
58         if(answer[i]>='1'){
59             len=i+1;
60             break;
61         }
62     }
63     if(!big){
64         answer[len]='-';
65         len++;
66     }
67     answer[len]=0;
68     Reverse(answer);
69 }
70 int main(){
71     #ifdef txtout
72     #endif // txtout
73     while(~scanf("%s%s",a,b)){
74         solve();
75         puts(answer);
76     }
77     return 0;
78 }
View Code

 

 

end

 

转载于:https://www.cnblogs.com/gaolzzxin/p/5792743.html

你可能感兴趣的:(手写 大整数)