[go: up one dir, main page]

0% found this document useful (0 votes)
881 views11 pages

C++ and Python Code Snippets Collection

The document provides a 7 step process for writing an essay: 1) understand the topic, 2) plan and outline, 3) structure the essay, 4) write the introduction, 5) write the body, 6) write the conclusion, and 7) revise and check the order.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
881 views11 pages

C++ and Python Code Snippets Collection

The document provides a 7 step process for writing an essay: 1) understand the topic, 2) plan and outline, 3) structure the essay, 4) write the introduction, 5) write the body, 6) write the conclusion, and 7) revise and check the order.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

#include <bits/stdc++.

h>
using namespace std;

int main() {

int n, M1, P1, M2, P2;


cin >> n >> M1 >> P1 >> M2 >> P2;

int mini = INT_MAX;

for (int i=0; M1*i <= n; i++)


{
int rem = n - i*M1;
if (rem%M2 == 0)
{
int cost = P1 * i + P2 * (rem/M2);
mini = (cost < mini or mini == -1) ? cost : mini;
}
}

if (mini != -1)
cout << mini;
else
cout << "Invalid inputs";

#include<bits/stdc++.h>
using namespace std;

int main(){

int n;
cin>>n;

vector <int> a(n);

for(int i=0; i<n; i++)


cin>>a[i];

for(int i=n-1; i>=0; i--){

for(int j=0; j<i;j++){

if(a[j]>a[j+1]){

int temp = a[j];


a[j] = a[j+1];
a[j+1] = temp;
}
}
}

for(int i=0; i<n; i++)


cout<<a[i]<<" ";

return 0;
}
#include<bits/stdc++.h>
using namespace std;

int main(){

int n;
cin>>n;

int a[n];

for(int i=0; i<n; i++)


cin>>a[i];

int p1=0, p2= n-1;

while(p1 < p2){

while(a[p1]%2 == 0 and p1<p2)


p1++;

while(a[p2]%2 != 0 and p1<p2)


p2--;

if(p1 < p2){


swap(a[p1], a[p2]);
p1++;
p2--;
}
}

for(int i=0; i<n; i++)


cout<<a[i]<<" ";

return 0;
}

#include<bits/stdc++.h>
using namespace std;

int main(){

string str;

cin>>str;

int hour,minute;

hour=(str[0]-'0')*10 + str[1]-'0';

minute=(str[3]-'0')*10 + str[4]-'0';

int res=0;

while(hour%10 != minute/10 or hour/10!= minute%10){

minute++;
if(minute==60){

minute=0;
hour++;
}

if(hour==24)
hour=0;
res++;
}

cout<<res;

return 0;
}

Example 1:
Input:
3
123
456
789

Output:
123698745

#include<bits/stdc++.h>
using namespace std;

int main(){

int n;
cin>>n;

int a[n][n], col=n, row=n;

for(int i=0; i<n; i++){


for(int j=0; j<n; j++)
cin>>a[i][j];
}

int k = 0, l = 0;

while (k < row and l < col){

for (int i = l; i < col; i++)


cout<<a[k][i]<<" ";
k++;
for (int i = k; i < row; i++)
cout<<a[i][n - 1]<<" ";
col--;

if (k < row){
for (int i = col - 1; i >= l; i--)
cout<<a[n - 1][i]<<" ";
row--;
}

if (l < col){
for (int i = row - 1; i >= k; --i)
cout<<a[i][l]<<" ";
l++;
}
}

return 0;
}

# Python3 program to print


# given matrix in spiral form
def spiralPrint(m, n, a) :
k = 0; l = 0

''' k - starting row index


m - ending row index
l - starting column index
n - ending column index
i - iterator '''

while (k < m and l < n) :

# Print the first row from


# the remaining rows
for i in range(l, n) :
print(a[k][i], end = " ")

k += 1

# Print the last column from


# the remaining columns
for i in range(k, m) :
print(a[i][n - 1], end = " ")

n -= 1

# Print the last row from


# the remaining rows
if ( k < m) :

for i in range(n - 1, (l - 1), -1) :


print(a[m - 1][i], end = " ")

m -= 1

# Print the first column from


# the remaining columns
if (l < n) :
for i in range(m - 1, k - 1, -1) :
print(a[i][l], end = " ")

l += 1

# Driver Code
a = [ [1, 2, 3, 4, 5, 6],
[7, 8, 9, 10, 11, 12],
[13, 14, 15, 16, 17, 18] ]

R = 3; C = 6
spiralPrint(R, C, a)

Trapezium question
#include<stdio.h>
int main ()
{
int n = 5, num = 1, i = 1, space = 0, k = 1, number = n;

for (i = 0; i < n; i++)


{
for (int j = 1; j <= space; j++)
{
printf (" ");
}
for (int m = 1; m < 2 * n - space; m++)
{
if (m % 2 == 0)
printf ("%s", "*");
else
printf ("%d", num++);
}

printf ("%s", "*");


for (int l = 1; l < 2 * n - space; l++)
{
if (l % 2 == 0)
printf ("%s", "*");
else
{
printf ("%d", k + number * number);
k++;
}
}
number--;
space = space + 2;
printf ("\n");
}
return 0;
}

/C++ Program to print 2*N Number of rows pattern


#include <iostream>
using namespace std;
int main()
{
int n=4,num=n-1; // Predefined input
for(int i=1;i<=n;i++)
{
for(int j=1;j<=i;j++)
cout<<num;
num++;
cout<<endl;
}

num--;
for(int i=n;i>=1;i--)
{
for(int j=1;j<=i;j++)
cout<<num;
num--;
cout<<endl;
}
return 0;
}

#include <iostream>
using namespace std;
int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}

int main()
{
int a,b;
cin>>a;
cin>>b;
cout<<gcd(a,b);
return 0;
}

#include <stdio.h>
int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}

void main()
{
int a,b,c;
scanf("%d %d %d",&a,&b,&c);
int res=gcd(a,gcd(b,c));
printf("%d",res);
}

def sockMerchant(n, ar):


pairs = 0
set_ar = set(ar)
for i in set_ar:
count = ar.count(i)
pairs+=count//2
return pairs

n = int(input())
ar = list(map(int, input().split()))
print(sockMerchant(n, ar))

def countingValley(steps, path):


level = valley = 0
for i in path:
if i == 'U':
level += 1
elif i == 'D':
if level == 1:
valley += 1
level -= 1
return valley

steps = int(input())
path = input()
print(countingValley(steps, path))

def rotateLeft(n,d,arr):
for i in range(d):
arr = rotatearr(n,arr)
return arr

def rotatearr(n, arr):


first = arr[0]
for i in range(n-1):
arr[i] = arr[i+1]
arr[n-1] = first
return arr

, d = map(int, input().split())
arr = list(map(int, input().split()))
for i in rotateLeft(n,d,arr):
print(i, end=" ")

a = [[1,1,1,1],[2,2,2,2],[3,3,3,3],[4,4,4,4]]
b = [[1,1,1,1],[2,2,2,2],[3,3,3,3],[4,4,4,4]]
if a==b:
prin("Metrices are identical")
else:
pint("Metrices are not identical")
def pythagoreanTriplets(limit):
a = b = c = 0
m = 2
while True:
for n in range(1,m+1):
a = m*m - n*n
b = 2*m*n
c = m*m + n*n
if c > limit:
break
if a==0 or b==0 or c==0:
break
print(a,b,c)
m=m+1

limit = int(input())
pythagoreanTriplets(limit)

1. Step 1. Understand the topic.


2. Step 2. Plan and outline.
3. Step 3. Structure your essay.
4. Step 4. Write the introduction.
5. Step 5. Write the body.
6. Step 6. Write a conclusion.
7. Step 7. Finishing touches (revise and check the order)

You might also like