Блог пользователя TheAnonymousAMGR

Автор TheAnonymousAMGR, история, 8 месяцев назад, По-английски

Method: Adding the digits and any carry-over value at the right end of each binary integer, we store the result in a new string. The procedure is now repeated for the following digit on the left until all of the digits in both binary numbers have been reached.

After adding all the digits, if there is any remaining value, add it to the end of the new string. The new string is then returned as the total of the two binary numbers after being inverted.

Your code here...
#include <bits/stdc++.h>
using namespace std;
int main()
{
    string a, b;
    cin >> a >> b;
    string ans;
    int carry = 0;
    int i = a.length() &mdash; 1;
    int j = b.length() &mdash; 1;

    while (i >= 0 || j >= 0 || carry)
    {
        if (i >= 0)
            carry += a[i--] - '0';
        if (j >= 0)
            carry += b[j--] - '0';
        ans += carry % 2 + '0';
        carry /= 2;
    }
    reverse(begin(ans), end(ans));
    for (int i = 0; i < ans.size(); i++)
    {
        cout << ans[i];
    }
}

Explain: _To hold the total and two integer variables, "carry" and "i," which represent the carry-over value and the current position in the first binary number (a), respectively, we first initialize a StringBuilder object. _In order to maintain track of the current location in the second binary number (b), we further initialize another integer variable called "j."Here is how the code initializes these variables

StringBuilder sb = new StringBuilder();
int carry = 0;
int i = a.length() &mdash; 1;
int j = b.length() &mdash; 1;

_After that, a while loop is inserted and iterates until all the digits in both binary numbers have been processed and there is no more carry-over value. _ In each iteration, we add the digits from both numbers at the current position and the carry-over value. _We also update the carry-over value based on the sum of the digits. _In each iteration, the current position in each binary number is moved one digit to the left (if there are any digits left to process) by decrementing the value of i and j. _f there is a carry-over value from the previous iteration or the addition of the two digits produces a carry-over value, we set the value of 'carry' to 1; otherwise, we set it to 0. _We also append the sum of the digits to the StringBuilder object by computing the remainder of 'carry' divided by 2 (which is either 0 or 1). _Finally, we update the value of 'carry' by dividing it by 2 (which gives either 0 or 1) so that we can carry over any remaining value to the next iteration. _After the while loop completes, we reverse the StringBuilder object and convert it to a string using the toString() method. This gives us the sum of the two binary numbers in binary format.

[Learn from YT and Leetcode]

Полный текст и комментарии »

  • Проголосовать: нравится
  • -16
  • Проголосовать: не нравится

Автор TheAnonymousAMGR, 8 месяцев назад, По-английски

100! is a huge number (around 158 digits). "long long" can store at max 19 digits. We going to get overflow. In Java there is a way to do this but in c++ you can't do this. So we should think another way to solve this problem. Now I explain how to multiply numbers avoid overflow using array. So the maximum possible value for an unsigned is 2 ^ 32 -1.In this case we get in overflow. So I use an array with length 200. here the code

#include <bits/stdc++.h>
using namespace std;
~~~~
int main() { 
               int t;
               cin>>t;
               while(t--)
               {
                // read a number
                int n;
		cin>>n;
                //create a array for 200 digits
		int a[200];
		a[0]=1;
		int digits=1;
                // create a for loop for multiply 1 upto n.
		for(int i=1;i<=n;i++)
		{
			// temporary variable which gonna carry all value
                        int temp=0;

			// multiplying every digits in our initial value by the number i.
                        for(int j=0;j<digits;j++)
			{
				int x=i*a[j]+temp;
                                //Storing the last digit
				a[j]=x%10;
                                //for carray over 
				temp=x/10;
			}
                        //if carry leftover
			while(temp>0)
			{
 				// add to the array
                                a[digits++]=temp%10;
				temp/=10;
			}
		}
                //print the array
		for(int i=digits-1;i>=0;i--)
		{
			cout<<a[i];
		}
		cout<<endl;
	}
	
	return 0;
} 

[this is my first blog, hopefully anyone learn from here]

Полный текст и комментарии »

  • Проголосовать: нравится
  • +11
  • Проголосовать: не нравится