고밀도 가감 승제

  • add:
  •  1 #include 
     2 #include 
     3 #include <string>
     4 #include 
     5 
     6 using namespace std;
     7 
     8 vector<int> a, b, c;//c = a + b
     9 
    10 void add(){
    11     int t = 0;//    
    12     for(int i = 0;i < a.size() || i < b.size();++i){
    13         if(i < a.size()) t += a[i];        
    14         if(i < b.size()) t += b[i];
    15         c.push_back(t%10);
    16         t /= 10;
    17     }    
    18     if(t)c.push_back(t);
    19 }
    20 
    21 int main(){
    22     string s1,s2;    
    23     cin >> s1 >> s2;
    24     
    25     for(int i = s1.size()-1;i >= 0;--i)a.push_back(s1[i] - '0');
    26     for(int i = s2.size()-1;i >= 0;--i)b.push_back(s2[i] - '0');
    27     
    28     add();
    29     
    30     for(int i = c.size()-1;i >= 0;--i)cout << c[i];      
    31 }
  • mul:
  •  1 #include 
     2 #include 
     3 
     4 using namespace std;
     5 
     6 //a*b = c
     7 void mul(vector<int>& a,int b,vector<int>& c){
     8     int t = 0;
     9     for(int i = 0;i < a.size();++i){
    10         c.push_back((a[i]*b + t) % 10);
    11         t = (a[i]*b + t) / 10; 
    12     }    
    13     if(t)c.push_back(t);
    14 }
    15 
    16 int main(){
    17     string s;
    18     int b;
    19     cin >> s >> b;
    20     vector<int> a;
    21     for(int i = s.size()-1;i >= 0;--i)a.push_back(s[i] - '0');
    22     
    23     vector<int> c;
    24     mul(a,b,c);
    25     for(int i = c.size()-1;i >= 0;--i)cout << c[i];
    26     
    27     return 0;
    28 }
  • div:

  •  
     1 #include 
     2 #include 
     3 #include <string>
     4 
     5 using namespace std;
     6 
     7 void div(vector<int>& a,int b,vector<int>& c,int& r){
     8     for(int i = 0;i < a.size();++i){
     9         r = r * 10 + a[i];
    10         c.push_back(r/b);
    11         r %= b;
    12     }
    13 }
    14 
    15 int main(){
    16     string s;
    17     cin >> s;
    18     vector<int> a;
    19     for(int i = 0;i < s.size();++i)a.push_back(s[i] - '0');
    20     int b;
    21     cin>>b;
    22     
    23     int r = 0;
    24     vector<int> c;
    25     div(a,b,c,r);
    26     bool flag = true;
    27     for(int i = 0;i < c.size();++i){
    28         if(c[i] != 0)flag = false;
    29         if(flag == false)cout << c[i];
    30     }
    31     cout << endl << r;
    32     return 0;
    33 }

     
  • sub:
  •  1 #include 
     2 #include 
     3 #include <string>
     4 
     5 using namespace std;
     6 
     7 //a >= b return true else return false
     8 bool cmp(vector<int>& a,vector<int>& b){
     9     if(a.size() != b.size()) return a.size() > b.size();
    10     for(int i = a.size()-1;i >= 0;--i)
    11         if(a[i] != b[i]) return a[i] > b[i];
    12     return true;
    13 }
    14 
    15 //a >= b
    16 void sub(vector<int>& a, vector<int>& b, vector<int>& c){
    17     int t = 0;//    
    18     for(int i = 0;i < a.size();++i){
    19         t = a[i] - t;
    20         if(i < b.size())
    21             t -= b[i];
    22         //     t > 0 and t < 0,    t+10 % 10
    23         c.push_back((t+10)%10);
    24         if(t >= 0)t = 0;//       
    25         else t = 1;//  
    26     }
    27     //     0
    28     while(c.size() > 1 && c.back() == 0)c.pop_back();
    29 }
    30 
    31 int main(){
    32     string s1, s2;
    33     cin >> s1 >> s2;
    34     vector<int> a, b, c;
    35     
    36     for(int i = s1.size()-1;i >= 0;--i)a.push_back(s1[i]-'0');
    37     for(int i = s2.size()-1;i >= 0;--i)b.push_back(s2[i]-'0');
    38     
    39     if(cmp(a,b)){
    40         sub(a, b, c);
    41         for(int i = c.size()-1;i >= 0;--i)cout << c[i];
    42     }
    43     else{
    44         sub(b, a, c);
    45         cout << '-';
    46         for(int i = c.size()-1;i >= 0;--i)cout << c[i];
    47     } 
    48     
    49     return 0;
    50 }

    좋은 웹페이지 즐겨찾기