Advertisement
Kali_prasad

add to binary strings (solved)

Jul 11th, 2022
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. #pragma GCC optimize ("O3")
  2. #pragma GCC target ("sse4")
  3.  
  4. #include <bits/stdc++.h>
  5. using namespace std;
  6.  
  7. typedef long long ll;
  8. typedef pair<int, int> pii;
  9. typedef pair<string,int> psi;
  10. typedef vector<int> vi;
  11. typedef vector<string> vs;
  12. typedef vector<ll> vll;
  13. typedef vector<vector<int>> vvi;
  14. typedef vector<vector<string>> vvs;
  15. typedef vector<vector<ll>> vvll;
  16.  
  17. #define FOR(i, a, b) for (auto i=a; i<=(b); i++)
  18. #define FORd(i,b,a) for (int i =b; i >= a; i--)
  19. #define sz(x) (int)(x).size()
  20. #define mp make_pair
  21. #define pb push_back
  22. #define f first
  23. #define s second
  24. #define ins insert
  25.  
  26. const int MOD = 1000000007;
  27. //type functions here
  28.  
  29. int m(ll k)
  30. {
  31. return k%MOD;
  32. }
  33. string resize(string x,int count)
  34. {
  35. while(count--)
  36. {
  37. x="0"+x;
  38. }
  39. return x;
  40. }
  41. int main() {
  42.  
  43. int tc=1;
  44. //cin>>tc;
  45. FOR(i,1,tc){
  46. string a="1",b="11";
  47. int m=a.length()-1,n=b.length()-1;
  48. if(m<n) {a=resize(a,n-m);m=n;}
  49. if(n<m) {b=resize(b,m-n);n=m;}
  50. int carry=0;
  51. string ans="";
  52. while(m!=-1||n!=-1)
  53. {
  54. if(a[m]=='0'&&b[n]=='0')
  55. {
  56. ans=to_string(0+carry)+ans;
  57. carry=0;
  58. }
  59. else if(a[m]=='1'&&b[n]=='1')
  60. {
  61. ans=to_string(0+carry)+ans;
  62. carry=1;
  63. }
  64. else
  65. {
  66. if(carry)
  67. {
  68. ans=to_string(0)+ans;
  69. }
  70. else
  71. {
  72. ans=to_string(1)+ans;
  73. }
  74. }
  75.  
  76. cout<<ans<<endl;
  77.  
  78. m--;
  79. n--;
  80. }
  81. if(carry) ans=to_string(carry)+ans ;
  82. cout<<ans<<endl;
  83. }
  84. return 0;
  85. }
  86.  
  87.  
  88.  
  89.  
  90.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement