mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-05-11 11:07:27 +08:00
style: format code
This commit is contained in:
@@ -3,15 +3,15 @@
|
||||
using namespace std;
|
||||
int main()
|
||||
{
|
||||
int n,t;
|
||||
int n, t;
|
||||
cin >> t;
|
||||
while(t--)
|
||||
while (t--)
|
||||
{
|
||||
cin >> n;
|
||||
if((n%7==0)||(n%10==7))
|
||||
cout << n << " is a buzz number" << endl;
|
||||
else
|
||||
cout << n << " is not a buzz number" << endl;
|
||||
cin >> n;
|
||||
if ((n % 7 == 0) || (n % 10 == 7))
|
||||
cout << n << " is a buzz number" << endl;
|
||||
else
|
||||
cout << n << " is not a buzz number" << endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// This function convert decimal to binary number
|
||||
// This function convert decimal to binary number
|
||||
//
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
@@ -10,13 +10,14 @@ int main()
|
||||
cin >> number;
|
||||
int remainder, binary = 0, var = 1;
|
||||
|
||||
do {
|
||||
remainder = number % 2;
|
||||
number = number / 2;
|
||||
binary = binary + (remainder*var);
|
||||
do
|
||||
{
|
||||
remainder = number % 2;
|
||||
number = number / 2;
|
||||
binary = binary + (remainder * var);
|
||||
var = var * 10;
|
||||
|
||||
} while (number>0);
|
||||
} while (number > 0);
|
||||
cout << "the binary is :";
|
||||
cout << binary;
|
||||
cout << endl;
|
||||
|
||||
@@ -2,25 +2,27 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main(void){
|
||||
int main(void)
|
||||
{
|
||||
int valueToConvert = 0; //Holds user input
|
||||
int hexArray[8]; //Contains hex values backwards
|
||||
int i = 0; //counter
|
||||
int hexArray[8]; //Contains hex values backwards
|
||||
int i = 0; //counter
|
||||
char HexValues[] = "0123456789ABCDEF";
|
||||
|
||||
cout << "Enter a Decimal Value" << endl; //Displays request to stdout
|
||||
cin >> valueToConvert; //Stores value into valueToConvert via user input
|
||||
cin >> valueToConvert; //Stores value into valueToConvert via user input
|
||||
|
||||
while (valueToConvert > 15){ //Dec to Hex Algorithm
|
||||
hexArray[i++] = valueToConvert % 16; //Gets remainder
|
||||
while (valueToConvert > 15)
|
||||
{ //Dec to Hex Algorithm
|
||||
hexArray[i++] = valueToConvert % 16; //Gets remainder
|
||||
valueToConvert /= 16;
|
||||
}
|
||||
hexArray[i] = valueToConvert; //Gets last value
|
||||
|
||||
hexArray[i] = valueToConvert; //Gets last value
|
||||
|
||||
cout << "Hex Value: ";
|
||||
while (i >= 0)
|
||||
cout<<HexValues[hexArray[i--]];
|
||||
|
||||
cout << HexValues[hexArray[i--]];
|
||||
|
||||
cout << endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -8,50 +8,77 @@
|
||||
using namespace std;
|
||||
|
||||
//This functions fills a string with character c, n times and returns it
|
||||
string fill( char c, int n )
|
||||
string fill(char c, int n)
|
||||
{
|
||||
string s = "";
|
||||
while( n-- ) s += c;
|
||||
while (n--)
|
||||
s += c;
|
||||
return s;
|
||||
}
|
||||
|
||||
//to convert to lowercase Roman Numeral
|
||||
// the function works recursively
|
||||
string tolowerRoman( int n )
|
||||
string tolowerRoman(int n)
|
||||
{
|
||||
if( n < 4 ) return fill( 'i', n );
|
||||
if( n < 6 ) return fill( 'i', 5 - n ) + "v";
|
||||
if( n < 9 ) return string( "v" ) + fill( 'i', n - 5 );
|
||||
if( n < 11 ) return fill( 'i', 10 - n ) + "x";
|
||||
if( n < 40 ) return fill( 'x', n / 10 ) + tolowerRoman( n % 10 );
|
||||
if( n < 60 ) return fill( 'x', 5 - n / 10 ) + 'l' + tolowerRoman( n % 10 );
|
||||
if( n < 90 ) return string( "l" ) + fill( 'x', n / 10 - 5 ) + tolowerRoman( n % 10 );
|
||||
if( n < 110 ) return fill( 'x', 10 - n / 10 ) + "c" + tolowerRoman( n % 10 );
|
||||
if( n < 400 ) return fill( 'c', n / 100 ) + tolowerRoman( n % 100 );
|
||||
if( n < 600 ) return fill( 'c', 5 - n / 100 ) + 'd' + tolowerRoman( n % 100 );
|
||||
if( n < 900 ) return string( "d" ) + fill( 'c', n / 100 - 5 ) + tolowerRoman( n % 100 );
|
||||
if( n < 1100 ) return fill( 'c', 10 - n / 100 ) + "m" + tolowerRoman( n % 100 );
|
||||
if( n < 4000 ) return fill( 'm', n / 1000 ) + tolowerRoman( n % 1000 );
|
||||
if (n < 4)
|
||||
return fill('i', n);
|
||||
if (n < 6)
|
||||
return fill('i', 5 - n) + "v";
|
||||
if (n < 9)
|
||||
return string("v") + fill('i', n - 5);
|
||||
if (n < 11)
|
||||
return fill('i', 10 - n) + "x";
|
||||
if (n < 40)
|
||||
return fill('x', n / 10) + tolowerRoman(n % 10);
|
||||
if (n < 60)
|
||||
return fill('x', 5 - n / 10) + 'l' + tolowerRoman(n % 10);
|
||||
if (n < 90)
|
||||
return string("l") + fill('x', n / 10 - 5) + tolowerRoman(n % 10);
|
||||
if (n < 110)
|
||||
return fill('x', 10 - n / 10) + "c" + tolowerRoman(n % 10);
|
||||
if (n < 400)
|
||||
return fill('c', n / 100) + tolowerRoman(n % 100);
|
||||
if (n < 600)
|
||||
return fill('c', 5 - n / 100) + 'd' + tolowerRoman(n % 100);
|
||||
if (n < 900)
|
||||
return string("d") + fill('c', n / 100 - 5) + tolowerRoman(n % 100);
|
||||
if (n < 1100)
|
||||
return fill('c', 10 - n / 100) + "m" + tolowerRoman(n % 100);
|
||||
if (n < 4000)
|
||||
return fill('m', n / 1000) + tolowerRoman(n % 1000);
|
||||
return "?";
|
||||
}
|
||||
|
||||
//to convert to uppercase Roman Numeral
|
||||
// the function works recursively
|
||||
string toupperRoman( int n )
|
||||
string toupperRoman(int n)
|
||||
{
|
||||
if( n < 4 ) return fill( 'I', n );
|
||||
if( n < 6 ) return fill( 'I', 5 - n ) + "V";
|
||||
if( n < 9 ) return string( "V" ) + fill( 'I', n - 5 );
|
||||
if( n < 11 ) return fill( 'I', 10 - n ) + "X";
|
||||
if( n < 40 ) return fill( 'X', n / 10 ) + toupperRoman( n % 10 );
|
||||
if( n < 60 ) return fill( 'X', 5 - n / 10 ) + 'L' + toupperRoman( n % 10 );
|
||||
if( n < 90 ) return string( "L" ) + fill( 'X', n / 10 - 5 ) + toupperRoman( n % 10 );
|
||||
if( n < 110 ) return fill( 'X', 10 - n / 10 ) + "C" + toupperRoman( n % 10 );
|
||||
if( n < 400 ) return fill( 'C', n / 100 ) + toupperRoman( n % 100 );
|
||||
if( n < 600 ) return fill( 'C', 5 - n / 100 ) + 'D' + toupperRoman( n % 100 );
|
||||
if( n < 900 ) return string( "D" ) + fill( 'C', n / 100 - 5 ) + toupperRoman( n % 100 );
|
||||
if( n < 1100 ) return fill( 'C', 10 - n / 100 ) + "M" + toupperRoman( n % 100 );
|
||||
if( n < 4000 ) return fill( 'M', n / 1000 ) + toupperRoman( n % 1000 );
|
||||
if (n < 4)
|
||||
return fill('I', n);
|
||||
if (n < 6)
|
||||
return fill('I', 5 - n) + "V";
|
||||
if (n < 9)
|
||||
return string("V") + fill('I', n - 5);
|
||||
if (n < 11)
|
||||
return fill('I', 10 - n) + "X";
|
||||
if (n < 40)
|
||||
return fill('X', n / 10) + toupperRoman(n % 10);
|
||||
if (n < 60)
|
||||
return fill('X', 5 - n / 10) + 'L' + toupperRoman(n % 10);
|
||||
if (n < 90)
|
||||
return string("L") + fill('X', n / 10 - 5) + toupperRoman(n % 10);
|
||||
if (n < 110)
|
||||
return fill('X', 10 - n / 10) + "C" + toupperRoman(n % 10);
|
||||
if (n < 400)
|
||||
return fill('C', n / 100) + toupperRoman(n % 100);
|
||||
if (n < 600)
|
||||
return fill('C', 5 - n / 100) + 'D' + toupperRoman(n % 100);
|
||||
if (n < 900)
|
||||
return string("D") + fill('C', n / 100 - 5) + toupperRoman(n % 100);
|
||||
if (n < 1100)
|
||||
return fill('C', 10 - n / 100) + "M" + toupperRoman(n % 100);
|
||||
if (n < 4000)
|
||||
return fill('M', n / 1000) + toupperRoman(n % 1000);
|
||||
return "?";
|
||||
}
|
||||
|
||||
@@ -60,11 +87,11 @@ string toupperRoman( int n )
|
||||
int main()
|
||||
{
|
||||
|
||||
int n;
|
||||
cout << "\t\tRoman numbers converter\n\n";
|
||||
cout << "Type in decimal number between 0 up to 4000 (exclusive): ";
|
||||
cin >> n;
|
||||
cout << n << " in Upper Roman Numerals is " << toupperRoman(n) << "\n";
|
||||
cout << n << " in Lower Roman Numerals is " << tolowerRoman(n) << "\n";
|
||||
return 0;
|
||||
int n;
|
||||
cout << "\t\tRoman numbers converter\n\n";
|
||||
cout << "Type in decimal number between 0 up to 4000 (exclusive): ";
|
||||
cin >> n;
|
||||
cout << n << " in Upper Roman Numerals is " << toupperRoman(n) << "\n";
|
||||
cout << n << " in Lower Roman Numerals is " << tolowerRoman(n) << "\n";
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -3,21 +3,21 @@
|
||||
using namepsace std;
|
||||
int main()
|
||||
{
|
||||
cout <<"Enter value of n:"<<endl;
|
||||
cout << "Enter value of n:" << endl;
|
||||
cin >> n;
|
||||
int a[n];
|
||||
int i,j,gcd;
|
||||
int i, j, gcd;
|
||||
cout << "Enter the n numbers:" << endl;
|
||||
for(i=0;i<n;i++)
|
||||
for (i = 0; i < n; i++)
|
||||
cin >> a[i];
|
||||
j=1; //to access all elements of the array starting from 1
|
||||
gcd=a[0];
|
||||
while(j<n)
|
||||
j = 1; //to access all elements of the array starting from 1
|
||||
gcd = a[0];
|
||||
while (j < n)
|
||||
{
|
||||
if(a[j]%gcd==0) //value of gcd is as needed so far
|
||||
j++; //so we check for next element
|
||||
if (a[j] % gcd == 0) //value of gcd is as needed so far
|
||||
j++; //so we check for next element
|
||||
else
|
||||
gcd=a[j]%gcd; //calculating GCD by division method
|
||||
gcd = a[j] % gcd; //calculating GCD by division method
|
||||
}
|
||||
cout << "GCD of entered n numbers:" << gcd;
|
||||
}
|
||||
|
||||
@@ -5,24 +5,25 @@ using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
int n,k,s=0,d;
|
||||
int n, k, s = 0, d;
|
||||
cout << "Enter a number:";
|
||||
cin >> n;
|
||||
s=0;k=n;
|
||||
while(k>9)
|
||||
s = 0;
|
||||
k = n;
|
||||
while (k > 9)
|
||||
{
|
||||
while(k!=0)
|
||||
while (k != 0)
|
||||
{
|
||||
d=k%10;
|
||||
s+=d;
|
||||
k/=10;
|
||||
d = k % 10;
|
||||
s += d;
|
||||
k /= 10;
|
||||
}
|
||||
k=s;
|
||||
s=0;
|
||||
k = s;
|
||||
s = 0;
|
||||
}
|
||||
if(k==1)
|
||||
cout << n << " is a happy number" << endl;
|
||||
if (k == 1)
|
||||
cout << n << " is a happy number" << endl;
|
||||
else
|
||||
cout << n << " is not a happy number" << endl;
|
||||
cout << n << " is not a happy number" << endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -12,12 +12,12 @@ int main()
|
||||
string s1 = to_string(num);
|
||||
string s2 = s1;
|
||||
|
||||
reverse(s1.begin(),s1.end());
|
||||
reverse(s1.begin(), s1.end());
|
||||
|
||||
if(s1 == s2)
|
||||
cout<<"true";
|
||||
if (s1 == s2)
|
||||
cout << "true";
|
||||
else
|
||||
cout<<"false";
|
||||
cout << "false";
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#include<iostream>
|
||||
#include<string>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
|
||||
@@ -10,55 +10,66 @@ using namespace std;
|
||||
char stack[MAX];
|
||||
int top = -1;
|
||||
|
||||
void push(char ch){
|
||||
stack[ ++top ] = ch;
|
||||
void push(char ch)
|
||||
{
|
||||
stack[++top] = ch;
|
||||
}
|
||||
|
||||
char pop(){
|
||||
return stack[ top-- ];
|
||||
char pop()
|
||||
{
|
||||
return stack[top--];
|
||||
}
|
||||
|
||||
// -------------- end stack -----------
|
||||
|
||||
char opening(char ch){
|
||||
switch(ch){
|
||||
case '}':
|
||||
return '{';
|
||||
case ']':
|
||||
return '[';
|
||||
case ')':
|
||||
return '(';
|
||||
case '>':
|
||||
return '<';
|
||||
char opening(char ch)
|
||||
{
|
||||
switch (ch)
|
||||
{
|
||||
case '}':
|
||||
return '{';
|
||||
case ']':
|
||||
return '[';
|
||||
case ')':
|
||||
return '(';
|
||||
case '>':
|
||||
return '<';
|
||||
}
|
||||
}
|
||||
|
||||
int main(){
|
||||
int main()
|
||||
{
|
||||
|
||||
string exp;
|
||||
int valid = 1, i = 0;
|
||||
cout<<"Enter The Expression : ";
|
||||
cout << "Enter The Expression : ";
|
||||
cin >> exp;
|
||||
|
||||
while (valid == 1 && i < exp.length()){
|
||||
if (exp[i] == '(' || exp[i] == '{' || exp[i] == '[' || exp[i] == '<'){
|
||||
push(exp[i]);
|
||||
}
|
||||
else if (top >= 0 && stack[top] == opening(exp[i])){
|
||||
pop();
|
||||
}
|
||||
else{
|
||||
valid = 0;
|
||||
}
|
||||
i++;
|
||||
|
||||
while (valid == 1 && i < exp.length())
|
||||
{
|
||||
if (exp[i] == '(' || exp[i] == '{' || exp[i] == '[' || exp[i] == '<')
|
||||
{
|
||||
push(exp[i]);
|
||||
}
|
||||
else if (top >= 0 && stack[top] == opening(exp[i]))
|
||||
{
|
||||
pop();
|
||||
}
|
||||
else
|
||||
{
|
||||
valid = 0;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
|
||||
// makes sure the stack is empty after processsing (above)
|
||||
if (valid == 1 && top == -1){
|
||||
cout<<"\nCorrect Expression";
|
||||
// makes sure the stack is empty after processsing (above)
|
||||
if (valid == 1 && top == -1)
|
||||
{
|
||||
cout << "\nCorrect Expression";
|
||||
}
|
||||
else{
|
||||
cout<<"\nWrong Expression";
|
||||
else
|
||||
{
|
||||
cout << "\nWrong Expression";
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
@@ -1,33 +1,32 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
|
||||
//A simple and efficient implementation of a function to test if a number is prime, based on the fact that
|
||||
//Every Prime number, except 2 and 3 are of the form 6*k+1 or 6*k-1 for integer values of k.
|
||||
|
||||
bool IsPrime( int number )
|
||||
{
|
||||
if ( ( (!(number & 1)) && number != 2 ) || (number < 2) || (number % 3 == 0 && number != 3) )
|
||||
return false;
|
||||
|
||||
for( int k = 1; 36*k*k-12*k < number;++k)
|
||||
{
|
||||
if ( (number % (6*k+1) == 0) || (number % (6*k-1) == 0) )
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
//Main Function
|
||||
cout <<"Enter the value of n to check if Prime\n";
|
||||
int n;
|
||||
cin >> n;
|
||||
if(IsPrime(n))
|
||||
cout << n << " is Prime" <<endl;
|
||||
else
|
||||
cout << n << " is not Prime" <<endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
//A simple and efficient implementation of a function to test if a number is prime, based on the fact that
|
||||
//Every Prime number, except 2 and 3 are of the form 6*k+1 or 6*k-1 for integer values of k.
|
||||
|
||||
bool IsPrime(int number)
|
||||
{
|
||||
if (((!(number & 1)) && number != 2) || (number < 2) || (number % 3 == 0 && number != 3))
|
||||
return false;
|
||||
|
||||
for (int k = 1; 36 * k * k - 12 * k < number; ++k)
|
||||
{
|
||||
if ((number % (6 * k + 1) == 0) || (number % (6 * k - 1) == 0))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
//Main Function
|
||||
cout << "Enter the value of n to check if Prime\n";
|
||||
int n;
|
||||
cin >> n;
|
||||
if (IsPrime(n))
|
||||
cout << n << " is Prime" << endl;
|
||||
else
|
||||
cout << n << " is not Prime" << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -5,8 +5,8 @@ using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
int m,n;
|
||||
int counterZeros=0;
|
||||
int m, n;
|
||||
int counterZeros = 0;
|
||||
cout << "Enter dimensions of matrix (seperated with space): ";
|
||||
cin >> m >> n;
|
||||
int a[m][n];
|
||||
@@ -14,28 +14,28 @@ int main()
|
||||
cout << "\n";
|
||||
|
||||
// reads the matrix from stdin
|
||||
for(int i=0;i<m;i++)
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
for(int j=0;j<n;j++)
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
cout << "element? ";
|
||||
cin >> a[i][j];
|
||||
cout << "element? ";
|
||||
cin >> a[i][j];
|
||||
}
|
||||
}
|
||||
|
||||
// counts the zero's
|
||||
for(int i=0;i<m;i++)
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
for(int j=0;j<n;j++)
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
if(a[i][j]==0)
|
||||
counterZeros++; //Counting number of zeroes
|
||||
if (a[i][j] == 0)
|
||||
counterZeros++; //Counting number of zeroes
|
||||
}
|
||||
}
|
||||
|
||||
// makes sure the matrix is a sparse matrix
|
||||
if(counterZeros>((m*n)/2)) //Checking for sparse matrix
|
||||
cout << "Sparse matrix";
|
||||
if (counterZeros > ((m * n) / 2)) //Checking for sparse matrix
|
||||
cout << "Sparse matrix";
|
||||
else
|
||||
cout << "Not a sparse matrix";
|
||||
cout << "Not a sparse matrix";
|
||||
}
|
||||
|
||||
@@ -3,55 +3,51 @@ using namespace std;
|
||||
|
||||
Multiply(int A[][], int B[][], int n)
|
||||
{
|
||||
if (n==2)
|
||||
if (n == 2)
|
||||
{
|
||||
int p1= (a[0][0] + a[1][1])*(b[0][0]+b[1][1]);
|
||||
int p2= (a[1][0]+a[1][1])*b[0][0];
|
||||
int p3= a[0][0]*(b[0][1]-b[1][1]);
|
||||
int p4= a[1][1]*(b[1][0]-b[0][0]);
|
||||
int p5= (a[0][0]+a[0][1])*b[1][1];
|
||||
int p6= (a[1][0]-a[0][0])*(b[0][0]+b[0][1]);
|
||||
int p7= (a[0][1]-a[1][1])*(b[1][0]+b[1][1]);
|
||||
int p1 = (a[0][0] + a[1][1]) * (b[0][0] + b[1][1]);
|
||||
int p2 = (a[1][0] + a[1][1]) * b[0][0];
|
||||
int p3 = a[0][0] * (b[0][1] - b[1][1]);
|
||||
int p4 = a[1][1] * (b[1][0] - b[0][0]);
|
||||
int p5 = (a[0][0] + a[0][1]) * b[1][1];
|
||||
int p6 = (a[1][0] - a[0][0]) * (b[0][0] + b[0][1]);
|
||||
int p7 = (a[0][1] - a[1][1]) * (b[1][0] + b[1][1]);
|
||||
|
||||
int c[n][n];
|
||||
c[0][0] = p1 + p4 - p5 + p7;
|
||||
c[0][1] = p3 + p5;
|
||||
c[1][0] = p2 + p4;
|
||||
c[1][1] = p1 - p2 + p3 + p6;
|
||||
|
||||
int c[n][n];
|
||||
c[0][0]=p1+p4-p5+p7;
|
||||
c[0][1]=p3+p5;
|
||||
c[1][0]=p2+p4;
|
||||
c[1][1]=p1-p2+p3+p6;
|
||||
|
||||
return c[][];
|
||||
return c[][];
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int p,q,r,s;
|
||||
cout<<"Enter the dimensions of Matrices";
|
||||
cin>>n;
|
||||
int A[n][n],;
|
||||
int B[n][n],;
|
||||
cout<<"Enter the elements of Matrix A";
|
||||
int p, q, r, s;
|
||||
cout << "Enter the dimensions of Matrices";
|
||||
cin >> n;
|
||||
int A[n][n], ;
|
||||
int B[n][n], ;
|
||||
cout << "Enter the elements of Matrix A";
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
for (int j = 0; j <n ; j++)
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
cin>>A[i][j];
|
||||
cin >> A[i][j];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
cout<<"Enter the elements of Matrix B";
|
||||
cout << "Enter the elements of Matrix B";
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
for (int j = 0; j <n ; j++)
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
cin>>B[i][j];
|
||||
cin >> B[i][j];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
//The method used is manual addition with carry and placing it in a string which is called string addition
|
||||
//This makes it have no bounds or limits
|
||||
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
@@ -69,10 +68,9 @@ void fib_Accurate(long long n)
|
||||
fibMinus2 = fibMinus1;
|
||||
fibMinus1 = tmp;
|
||||
}
|
||||
cout << fibMinus2;
|
||||
cout << fibMinus2;
|
||||
}
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
int n;
|
||||
|
||||
@@ -1,88 +1,73 @@
|
||||
#include<iostream>
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
struct tower
|
||||
{
|
||||
int values[10];
|
||||
int top;
|
||||
}F, U, T;
|
||||
|
||||
|
||||
} F, U, T;
|
||||
|
||||
void show()
|
||||
{
|
||||
cout<<"\n\n\tF : ";
|
||||
for(int i=0; i<F.top; i++)
|
||||
cout << "\n\n\tF : ";
|
||||
for (int i = 0; i < F.top; i++)
|
||||
{
|
||||
cout<<F.values[i]<<"\t";
|
||||
cout << F.values[i] << "\t";
|
||||
}
|
||||
cout<<"\n\tU : ";
|
||||
for(int i=0; i<U.top; i++)
|
||||
cout << "\n\tU : ";
|
||||
for (int i = 0; i < U.top; i++)
|
||||
{
|
||||
cout<<U.values[i]<<"\t";
|
||||
cout << U.values[i] << "\t";
|
||||
}
|
||||
cout<<"\n\tT : ";
|
||||
for(int i=0; i<T.top; i++)
|
||||
cout << "\n\tT : ";
|
||||
for (int i = 0; i < T.top; i++)
|
||||
{
|
||||
cout<<T.values[i]<<"\t";
|
||||
cout << T.values[i] << "\t";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void mov(tower &From, tower &To)
|
||||
{
|
||||
--From.top;
|
||||
To.values[To.top]=From.values[From.top];
|
||||
To.values[To.top] = From.values[From.top];
|
||||
++To.top;
|
||||
}
|
||||
|
||||
void TH(int n, tower &From, tower &Using, tower &To)
|
||||
{
|
||||
|
||||
if (n==1)
|
||||
|
||||
if (n == 1)
|
||||
{
|
||||
mov(From, To);
|
||||
show();
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
TH(n-1, From, To, Using);
|
||||
TH(n - 1, From, To, Using);
|
||||
mov(From, To);
|
||||
show();
|
||||
TH(n-1, Using, From, To);
|
||||
TH(n - 1, Using, From, To);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
F.top=0;
|
||||
U.top=0;
|
||||
T.top=0;
|
||||
F.top = 0;
|
||||
U.top = 0;
|
||||
T.top = 0;
|
||||
|
||||
int no;
|
||||
|
||||
cout << "\nEnter number of discs : " ;
|
||||
cout << "\nEnter number of discs : ";
|
||||
cin >> no;
|
||||
|
||||
for (int i = no; i >0; i--)
|
||||
|
||||
for (int i = no; i > 0; i--)
|
||||
{
|
||||
F.values[F.top++]=i;
|
||||
F.values[F.top++] = i;
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
show();
|
||||
TH(no, F, U, T);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -6,14 +6,12 @@
|
||||
//It is a property of fibonacci similar to matrix exponentiation.
|
||||
|
||||
#include <iostream>
|
||||
#include<cstdio>
|
||||
#include <cstdio>
|
||||
using namespace std;
|
||||
|
||||
const long long MAX = 93;
|
||||
|
||||
|
||||
long long f[MAX] = {0};
|
||||
|
||||
|
||||
long long fib(long long n)
|
||||
{
|
||||
@@ -22,26 +20,23 @@ long long fib(long long n)
|
||||
return 0;
|
||||
if (n == 1 || n == 2)
|
||||
return (f[n] = 1);
|
||||
|
||||
|
||||
if (f[n])
|
||||
return f[n];
|
||||
|
||||
long long k = (n%2!=0)? (n+1)/2 : n/2;
|
||||
|
||||
f[n] = (n%2!=0)? (fib(k)*fib(k) + fib(k-1)*fib(k-1))
|
||||
: (2*fib(k-1) + fib(k))*fib(k);
|
||||
|
||||
long long k = (n % 2 != 0) ? (n + 1) / 2 : n / 2;
|
||||
|
||||
f[n] = (n % 2 != 0) ? (fib(k) * fib(k) + fib(k - 1) * fib(k - 1))
|
||||
: (2 * fib(k - 1) + fib(k)) * fib(k);
|
||||
return f[n];
|
||||
}
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
//Main Function
|
||||
for(long long i=1;i<93;i++)
|
||||
{
|
||||
cout << i << " th fibonacci number is " << fib(i) << "\n";
|
||||
}
|
||||
return 0;
|
||||
//Main Function
|
||||
for (long long i = 1; i < 93; i++)
|
||||
{
|
||||
cout << i << " th fibonacci number is " << fib(i) << "\n";
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -13,7 +13,6 @@ using namespace std;
|
||||
|
||||
int primes[MAX];
|
||||
|
||||
|
||||
/*
|
||||
* This is the function that finds the primes and eliminates
|
||||
* the multiples.
|
||||
@@ -22,12 +21,13 @@ void sieve(int N)
|
||||
{
|
||||
primes[0] = 1;
|
||||
primes[1] = 1;
|
||||
for(int i=2;i<=N;i++)
|
||||
{
|
||||
if(primes[i] == 1) continue;
|
||||
for(int j=i+i;j<=N;j+=i)
|
||||
primes[j] = 1;
|
||||
}
|
||||
for (int i = 2; i <= N; i++)
|
||||
{
|
||||
if (primes[i] == 1)
|
||||
continue;
|
||||
for (int j = i + i; j <= N; j += i)
|
||||
primes[j] = 1;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -35,8 +35,8 @@ void sieve(int N)
|
||||
*/
|
||||
void print(int N)
|
||||
{
|
||||
for(int i=0;i<=N;i++)
|
||||
if(primes[i] == 0)
|
||||
for (int i = 0; i <= N; i++)
|
||||
if (primes[i] == 0)
|
||||
cout << i << ' ';
|
||||
cout << '\n';
|
||||
}
|
||||
@@ -47,7 +47,7 @@ void print(int N)
|
||||
*/
|
||||
void init()
|
||||
{
|
||||
for(int i=0;i<MAX;i++)
|
||||
for (int i = 0; i < MAX; i++)
|
||||
primes[i] = 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,8 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
struct Point {
|
||||
struct Point
|
||||
{
|
||||
double x, y;
|
||||
Point(double a = 0.0, double b = 0.0)
|
||||
{
|
||||
@@ -15,7 +16,7 @@ struct Point {
|
||||
|
||||
double LenghtLine(Point A, Point B)
|
||||
{
|
||||
return sqrt(abs((B.x - A.x)*(B.x - A.x)) + abs((B.y - A.y)*(B.y - A.y)));
|
||||
return sqrt(abs((B.x - A.x) * (B.x - A.x)) + abs((B.y - A.y) * (B.y - A.y)));
|
||||
}
|
||||
|
||||
double TriangleArea(Point A, Point B, Point C)
|
||||
@@ -24,7 +25,7 @@ double TriangleArea(Point A, Point B, Point C)
|
||||
double b = LenghtLine(B, C);
|
||||
double c = LenghtLine(C, A);
|
||||
double p = (a + b + c) / 2;
|
||||
return sqrt(p*(p - a)*(p - b)*(p - c));
|
||||
return sqrt(p * (p - a) * (p - b) * (p - c));
|
||||
}
|
||||
|
||||
bool PointInCircle(vector<Point> &P, Point Center, double R)
|
||||
@@ -44,12 +45,12 @@ double circle(vector<Point> P)
|
||||
Point C;
|
||||
Point minC;
|
||||
for (size_t i = 0; i < P.size() - 2; i++)
|
||||
for (size_t j = i+1; j < P.size(); j++)
|
||||
for (size_t k = j+1; k < P.size(); k++)
|
||||
for (size_t j = i + 1; j < P.size(); j++)
|
||||
for (size_t k = j + 1; k < P.size(); k++)
|
||||
{
|
||||
C.x = -0.5 * ((P[i].y*(P[j].x*P[j].x + P[j].y*P[j].y - P[k].x*P[k].x - P[k].y*P[k].y) + P[j].y*(P[k].x*P[k].x + P[k].y*P[k].y - P[i].x*P[i].x - P[i].y*P[i].y) + P[k].y*(P[i].x*P[i].x + P[i].y*P[i].y - P[j].x*P[j].x - P[j].y*P[j].y)) / (P[i].x*(P[j].y - P[k].y) + P[j].x*(P[k].y - P[i].y) + P[k].x*(P[i].y - P[j].y) ));
|
||||
C.y = 0.5 * ((P[i].x*(P[j].x*P[j].x + P[j].y*P[j].y - P[k].x*P[k].x - P[k].y*P[k].y) + P[j].x*(P[k].x*P[k].x + P[k].y*P[k].y - P[i].x*P[i].x - P[i].y*P[i].y) + P[k].x*(P[i].x*P[i].x + P[i].y*P[i].y - P[j].x*P[j].x - P[j].y*P[j].y)) / (P[i].x*(P[j].y - P[k].y) + P[j].x*(P[k].y - P[i].y) + P[k].x*(P[i].y - P[j].y) ));
|
||||
R = (LenghtLine(P[i], P[j]) * LenghtLine(P[j], P[k]) * LenghtLine(P[k], P[i])) / (4 * TriangleArea(P[i], P[j], P[k]));
|
||||
C.x = -0.5 * ((P[i].y * (P[j].x * P[j].x + P[j].y * P[j].y - P[k].x * P[k].x - P[k].y * P[k].y) + P[j].y * (P[k].x * P[k].x + P[k].y * P[k].y - P[i].x * P[i].x - P[i].y * P[i].y) + P[k].y * (P[i].x * P[i].x + P[i].y * P[i].y - P[j].x * P[j].x - P[j].y * P[j].y)) / (P[i].x * (P[j].y - P[k].y) + P[j].x * (P[k].y - P[i].y) + P[k].x * (P[i].y - P[j].y)));
|
||||
C.y = 0.5 * ((P[i].x * (P[j].x * P[j].x + P[j].y * P[j].y - P[k].x * P[k].x - P[k].y * P[k].y) + P[j].x * (P[k].x * P[k].x + P[k].y * P[k].y - P[i].x * P[i].x - P[i].y * P[i].y) + P[k].x * (P[i].x * P[i].x + P[i].y * P[i].y - P[j].x * P[j].x - P[j].y * P[j].y)) / (P[i].x * (P[j].y - P[k].y) + P[j].x * (P[k].y - P[i].y) + P[k].x * (P[i].y - P[j].y)));
|
||||
R = (LenghtLine(P[i], P[j]) * LenghtLine(P[j], P[k]) * LenghtLine(P[k], P[i])) / (4 * TriangleArea(P[i], P[j], P[k]));
|
||||
if (!PointInCircle(P, C, R))
|
||||
{
|
||||
continue;
|
||||
@@ -59,7 +60,6 @@ double circle(vector<Point> P)
|
||||
minR = R;
|
||||
minC = C;
|
||||
}
|
||||
|
||||
}
|
||||
for (size_t i = 0; i < P.size() - 1; i++)
|
||||
for (size_t j = i + 1; j < P.size(); j++)
|
||||
@@ -84,30 +84,30 @@ double circle(vector<Point> P)
|
||||
void test()
|
||||
{
|
||||
vector<Point> Pv(5);
|
||||
Pv.push_back(Point(0,0));
|
||||
Pv.push_back(Point(1,3));
|
||||
Pv.push_back(Point(4,1));
|
||||
Pv.push_back(Point(5,4));
|
||||
Pv.push_back(Point(3,-2));
|
||||
Pv.push_back(Point(0, 0));
|
||||
Pv.push_back(Point(1, 3));
|
||||
Pv.push_back(Point(4, 1));
|
||||
Pv.push_back(Point(5, 4));
|
||||
Pv.push_back(Point(3, -2));
|
||||
cout << circle(Pv) << endl;
|
||||
}
|
||||
|
||||
void test2()
|
||||
{
|
||||
vector<Point> Pv(4);
|
||||
Pv.push_back(Point(0,0));
|
||||
Pv.push_back(Point(0,2));
|
||||
Pv.push_back(Point(2,2));
|
||||
Pv.push_back(Point(2,0));
|
||||
Pv.push_back(Point(0, 0));
|
||||
Pv.push_back(Point(0, 2));
|
||||
Pv.push_back(Point(2, 2));
|
||||
Pv.push_back(Point(2, 0));
|
||||
cout << circle(Pv) << endl;
|
||||
}
|
||||
|
||||
void test3()
|
||||
{
|
||||
vector<Point> Pv(3);
|
||||
Pv.push_back(Point(0.5,1));
|
||||
Pv.push_back(Point(3.5,3));
|
||||
Pv.push_back(Point(2.5,0));
|
||||
Pv.push_back(Point(0.5, 1));
|
||||
Pv.push_back(Point(3.5, 3));
|
||||
Pv.push_back(Point(2.5, 0));
|
||||
cout << circle(Pv) << endl;
|
||||
}
|
||||
int main()
|
||||
|
||||
@@ -1,54 +1,65 @@
|
||||
#include<iostream>
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
void genArray(int a[][10],int r,int c){
|
||||
void genArray(int a[][10], int r, int c)
|
||||
{
|
||||
|
||||
int value=1;
|
||||
for(int i=0;i<r;i++){
|
||||
for(int j=0;j<c;j++){
|
||||
int value = 1;
|
||||
for (int i = 0; i < r; i++)
|
||||
{
|
||||
for (int j = 0; j < c; j++)
|
||||
{
|
||||
a[i][j] = value;
|
||||
cout<<a[i][j]<<" ";
|
||||
cout << a[i][j] << " ";
|
||||
value++;
|
||||
}
|
||||
cout<<endl;
|
||||
cout << endl;
|
||||
}
|
||||
}
|
||||
void spiralPrint(int a[][10],int r,int c){
|
||||
void spiralPrint(int a[][10], int r, int c)
|
||||
{
|
||||
|
||||
int startRow=0,endRow=r-1;
|
||||
int startCol =0, endCol = c-1;
|
||||
int cnt=0;
|
||||
int startRow = 0, endRow = r - 1;
|
||||
int startCol = 0, endCol = c - 1;
|
||||
int cnt = 0;
|
||||
|
||||
while(startRow<=endRow && startCol<=endCol){
|
||||
while (startRow <= endRow && startCol <= endCol)
|
||||
{
|
||||
|
||||
///Print start row
|
||||
for(int i=startCol;i<=endCol;i++,cnt++){
|
||||
cout<<a[startRow][i]<<" ";
|
||||
for (int i = startCol; i <= endCol; i++, cnt++)
|
||||
{
|
||||
cout << a[startRow][i] << " ";
|
||||
}
|
||||
startRow++;
|
||||
|
||||
///Print the end col
|
||||
for(int i=startRow;i<=endRow;i++,cnt++){
|
||||
cout<<a[i][endCol]<<" ";
|
||||
for (int i = startRow; i <= endRow; i++, cnt++)
|
||||
{
|
||||
cout << a[i][endCol] << " ";
|
||||
}
|
||||
endCol--;
|
||||
|
||||
///Print the end row
|
||||
if(cnt==r*c){
|
||||
if (cnt == r * c)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
for(int i=endCol;i>=startCol;i--,cnt++){
|
||||
cout<<a[endRow][i]<<" ";
|
||||
for (int i = endCol; i >= startCol; i--, cnt++)
|
||||
{
|
||||
cout << a[endRow][i] << " ";
|
||||
}
|
||||
endRow--;
|
||||
|
||||
///Print the start Col
|
||||
if(cnt==r*c){
|
||||
if (cnt == r * c)
|
||||
{
|
||||
break;
|
||||
}
|
||||
for(int i=endRow;i>=startRow;i--,cnt++){
|
||||
cout<<a[i][startCol]<<" ";
|
||||
for (int i = endRow; i >= startRow; i--, cnt++)
|
||||
{
|
||||
cout << a[i][startCol] << " ";
|
||||
}
|
||||
startCol++;
|
||||
}
|
||||
@@ -58,11 +69,10 @@ int main()
|
||||
{
|
||||
int a[10][10];
|
||||
|
||||
int r,c;
|
||||
cin>>r>>c;
|
||||
genArray(a,r,c);
|
||||
spiralPrint(a,r,c);
|
||||
int r, c;
|
||||
cin >> r >> c;
|
||||
genArray(a, r, c);
|
||||
spiralPrint(a, r, c);
|
||||
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user