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

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

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
  • Проголосовать: не нравится

»
9 месяцев назад, # |
  Проголосовать: нравится +41 Проголосовать: не нравится

Nice blog, but next time when writing code put it in between a block to make it readable as a c++ code

like this for example

vector<int>v = {1, 1, 2, 3, 5, 8, 13};

This is how you make a block :

~~~~
(Put your code here)
~~~~

»
3 месяца назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

<3