Skip to content

Commit

Permalink
Add codes
Browse files Browse the repository at this point in the history
These codes are mainly solutions from codeforces and atcoder.
no advanced algorithms. just bruteforce and simulation. tried to learn dynamic programming
  • Loading branch information
ni9999 authored Feb 8, 2022
1 parent 6afbf7a commit 90ce822
Show file tree
Hide file tree
Showing 37 changed files with 1,037 additions and 0 deletions.
23 changes: 23 additions & 0 deletions competitive_programming/abctripletatc.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <iostream>
using namespace std;
int main ()
{
long long n;
cin >> n;
long long a, b, c, triplets = 0;
for ( a = 1; a <= n; a++)
{
for ( b = a; b <= n; b++)
{
for ( c = b; c <= n; c++)
{
if (a*b*c <= n) triplets++;
}

}

}

cout << triplets << endl;

}
42 changes: 42 additions & 0 deletions competitive_programming/ancientcivicf.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <iostream>
using namespace std;
int main()
{
int t;
cin >> t;
while (t--)
{
int n, l, i = 0, j = 0;
cin >> n >> l;
int deci[n];
while (n--)
{

cin >> deci[i];
i++;
}

int bina[n][l];
for (i = 0; i < n; i++)
{
for (j = 0; j < l; j++)
{
bina[i][j] = 0;
}
}
for (i = 0; i < n; i++)
{

for (j = l-1; j >= 0; j--)
{
if (deci[n])
{
bina[i][j] = deci[n] % 2;
deci[n] = deci[n] / 2;
}
cout << bina[i][j];
}
cout << endl;
}
}
}
42 changes: 42 additions & 0 deletions competitive_programming/average.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
int t, n, c = 0;
cin >> t;
while (t--)
{
float outs = 0, runs = 0;
float ans;
c++;
cin >> n;
while (n--)
{
string run;
cin >> run;
int x = run.size() - 1;
if (run[x] != '*')
outs++;
float run2;
stringstream ss;
ss << run;
ss >> run2;
runs += run2;
}
if (outs == 0)
cout << "Case " << c << ":" << " " << -1 << endl;
else
{
ans = runs / outs;
if (ans - int(ans) > 0)
{
cout << "Case " << c << ":" << " " << -1 << endl;
}
else
cout << "Case " << c << ":" << " " << int(ans) << endl;
}
}
return 0;
}
25 changes: 25 additions & 0 deletions competitive_programming/buildingareaguess.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//ac
#include <iostream>
using namespace std;
int main()
{
int n, i, rightguess = 0;
cin >> n;
int s, b, zerornot;
for (i = 0; i < n; i++)
{
cin >> s;

for (b = 1; b <= (s - 3) / 7; b++) // for a = 1 we get the highest number of b
{
zerornot = (s - 3 * b) % (4 * b + 3); // determining --> is it a natural number or fractional
if (zerornot == 0)
{
rightguess++;
break;
}
}
}
cout << n - rightguess << endl;
return 0;
}
21 changes: 21 additions & 0 deletions competitive_programming/cellguesscf.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <iostream>

using namespace std;

int main() {
long long t , n, m;
cin >> t;
for(int i = 0 ; i < t ; i++){
cin >> n >> m;
if (n==m==1)
cout << 0 << endl;
else if (n<m){
cout << n << endl;
}
else{
cout << m << endl;
}

}
return 0;
}
22 changes: 22 additions & 0 deletions competitive_programming/chal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import random

def encrypt(plaintext):
ciphertext = ''
for letter in plaintext:
i = ALPHA.index(letter)
c = (a*i + b) % m
ciphertext += ALPHA[c]
return ciphertext


ALPHA = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ{}_ #"
m = len(ALPHA)
a = random.randrange(1, m)
b = random.randrange(1, m)

message = open("message.txt").read().replace('\n', '')
cipher = encrypt(message)

with open("cipher.txt", 'w') as f:
for i in range(0,len(cipher),64):
f.write( cipher[i:i+64]+'\n' )
44 changes: 44 additions & 0 deletions competitive_programming/continuefraction.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include <iostream>
#include <iomanip>
int i;
using namespace std;
int frac(int x, int y)
{
int z;
z = x / y;
cout << z << " ";
int rem;
rem = x%y;
if(rem!=1)
frac(y, rem);
else
cout << y;
return 0;
}

int howmuch(int x, int y)
{
i++;
int rem;
rem = x%y;
if(rem!=1)
howmuch(y, rem);
else
cout << i << " ";
return 0;
}

int main()
{
int t;
cin >> t;
while (t--)
{
i = 0;
int x, y, rem;
cin >> x >> y;
howmuch(x,y);
frac(x, y);
cout << endl;
}
}
9 changes: 9 additions & 0 deletions competitive_programming/daily_star.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import requests
from bs4 import BeautifulSoup
daily_star = requests.get('https://www.thedailystar.net')
parsed_html = BeautifulSoup(daily_star.text, 'html.parser')
headlines = parsed_html.find_all('h3')
for i in headlines:
if len(i.text.strip()) > 0:
print(i.text)

39 changes: 39 additions & 0 deletions competitive_programming/deviationcf.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <iostream>
using namespace std;
int main()
{
int t, x = 6;
cin >> t;
int a[3];
for (int i = 0; i < t; i++)
{

cin >> a[0] >> a[1] >> a[2];
while (x--)
{
if (a[0] + a[2] - 2 * a[1] == 0)
{
cout << 0 << endl;
break;
}
else if (abs(a[0] + a[2] - 2 * a[1]) == 1)
{
cout << 1 << endl;
break;
}
else
{
if (a[0] + a[2] - 2 * a[1] > 1)
{
a[1] = a[1] + 1;
a[0] = a[0] - 1;
}
else
{
a[1] = a[1] - 1;
a[0] = a[0] + 1;
}
}
}
}
}
26 changes: 26 additions & 0 deletions competitive_programming/distancemanhattancf.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <iostream>
#include <cmath>

int main() {
float t, x, y, a, b, d_bcac;
std::cin >> t;
for (int i=0; i<t; i++)
{
std::cin >> x >> y;
d_bcac = (x+y)/2; //if abs used then d_bcac won't be a float number
if(d_bcac - round(d_bcac) == 0)
{
for (int i=0; i<=d_bcac; i++)
{
a=i;
b=d_bcac-i;
if (abs(a-x) +abs(b-y) == d_bcac)
break;
}
std::cout << a << " " << b << "\n";
}
//std::cout << (x+2*d_bcac-y)/2 << " " << (y+2*d_bcac-x)/2 << "\n";
else
std::cout << -1 << " " << -1 << "\n";
}
}
31 changes: 31 additions & 0 deletions competitive_programming/divanstorecf.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <bits/stdc++.h>
using namespace std;

int main() {
int t, i;
cin >> t;
for(i = 0 ; i < t ; i++){
int n, l, r, k, a = 0, spended =0, chocolates = 0;
cin >> n >> l >> r >> k;
int chocoprice[n];
for (i = 0; i < n; i++) cin >> chocoprice[i];
for ( i = 0; i < n; i++)
{
if(chocoprice[i] >= l && chocoprice[i] <= r) a++;
}
int buyable[a];
for (int i = 0; i < n; i++)
{
if(chocoprice[i] >= l && chocoprice[i] <= r) buyable[a] = chocoprice[i];
}
int asize = sizeof(buyable) / sizeof(buyable[0]);
sort(buyable, buyable + asize);
for ( i = 0; i < a; i++)
{
spended += buyable[i];
if (spended <= k) chocolates++;
}
cout << chocolates << endl;
}
return 0;
}
Loading

0 comments on commit 90ce822

Please sign in to comment.