Skip to content

Instantly share code, notes, and snippets.

@kerolloz
Last active February 5, 2020 18:56
Show Gist options
  • Save kerolloz/737e466a8e3e7709b19169aa52f7c919 to your computer and use it in GitHub Desktop.
Save kerolloz/737e466a8e3e7709b19169aa52f7c919 to your computer and use it in GitHub Desktop.
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int answer = n; /// assume we used all of coin1
for (int c4 = 0; c4 <= n / 4; c4++) { // try to take c4 of coin4
for (int c3 = 0; c3 <= (n - 4 * c4) / 3; c3++) { // try to take c3 of coin3
int c1 = n - (4 * c4 + 3 * c3);
answer = min(answer, c1 + c3 + c4);
}
}
cout << answer << endl;
return 0;
}
#include <bits/stdc++.h>
using namespace std;
int count_digits_string(long long n){
string s = to_string(n);
return s.size();
}
int count_digits_loop(long long n){
int answer = 0;
if(n == 0)
return 1;
else if(n < 0)// count negative sign and make n positive
answer = 1, n *= -1;
while(n > 0)// while not zero count last digit and remove it
answer++, n /= 10;
return answer;
}
int count_digits_math(long long n){
int answer = 0;
if(n < 0)// count negative sign and make n positive
answer++, n *= -1;
answer += log10(n) + 1;// log10(n) + 1 is the number of digits in n
return answer;
}
int main(){
long long a, b;/// use long long to avoid overflow
cin >> a >> b;
cout << count_digits_math(a*b);
return 0;
}
#include <stdio.h>
int main () {
int n;
scanf("%d", &n); // cin >> n;
printf("%d\n", n); // cout << n << endl;
return 0;
}
#include <bits/stdc++.h>
using namespace std;
double calc(string mark)
{
int i = mark.find_first_of('/');
int got = stoi(mark.substr(0, i));
double total = stoi(mark.substr(i + 1, mark.size() - i + 1));
return got / total;
}
char rnk(double mark)
{
if (mark >= .85)
return 'A';
if (mark < .85 && mark >= .7)
return 'B';
if (mark < .7 && mark >= .65)
return 'C';
if (mark < .65 && mark >= .5)
return 'D';
return 'E';
}
struct Subject
{
string name;
double percent;
};
bool subj_sort(const Subject &sub1, const Subject &sub2)
{
return sub1.percent > sub2.percent;
}
int main()
{
int n;
cin >> n;
Subject subjects[n];
string mark;
for (int i = 0; i < n; i++)
{
cin >> subjects[i].name >> mark;
subjects[i].percent = calc(mark);
}
sort(subjects, subjects + n, subj_sort);
for (int i = 0; i < n; i++)
{
cout << subjects[i].name << " " << rnk(subjects[i].percent) << endl;
}
}
#include <bits/stdc++.h>
using namespace std;
char rnk(double mark)
{
if (mark >= .85)
return 'A';
if (mark < .85 && mark >= .7)
return 'B';
if (mark < .7 && mark >= .65)
return 'C';
if (mark < .65 && mark >= .5)
return 'D';
else
return 'E';
}
int main()
{
int n;
cin >> n;
string subjects[n];
string mark;
double percent[n];
for (int i = 0; i < n; i++)
{
cin >> subjects[i];
cin >> mark;
// now let's process the string mark
int scored = 0;
double total = 0;
string st_scored = "", st_total = "";
int index = 0;
while (mark[index] != '/')
st_scored += mark[index], index++;
// now, index is pointing to '/' char
// let's push index one step towards the first digit
index++;
// now, index is pointing to the first digit after '/'
while (index < mark.size())
st_total += mark[index], index++;
scored = stoi(st_scored); // stoi = string to integer
total = stod(st_total); // stod = string to double
percent[i] = scored / total;
}
// the following is a simple sorting algorithm
// selection sort
for (int i = 0; i < n; i++)
{
double max = percent[i];
int index = i;
for (int j = i + 1; j < n; j++)
{
if (percent[j] > max)
{
max = percent[j];
index = j;
}
}
swap(percent[i], percent[index]);
swap(subjects[i], subjects[index]);
}
for (int i = 0; i < n; i++)
{
cout << subjects[i] << " " << rnk(percent[i]) << endl;
}
}
#include <bits/stdc++.h>
using namespace std;
int main()
{
string s;
int x;
cin >> s >> x;
int operations = 0; // initially we didn't perform any operations yet
// first check if S is same as X
if (s == to_string(x)) // convert x to a string and compare for equality
{
cout << operations << endl;
return 0;
}
while (s.size() > 1) // as long as the number of digits in S is greater than 1
{
operations++;
int sum = 0;
for (char c : s) // for every char in S
{
sum += c - '0'; // convert character to int
}
s = to_string(sum);
if (x == sum)
{
cout << operations << endl;
return 0;
}
}
if (s == to_string(x))
cout << operations << endl;
else
cout << -1 << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment