Posts

Showing posts from April, 2020

Basketball One-on-One : Kattis Solution in C++

Image
#include<bits/stdc++.h> using namespace std; int main(int argc, char const *argv[]){ string n; cin >> n; int len = n.length(); int scoreA = 0,scoreB = 0; for (int i = 0; i < len; i+=2) { if(n[i] == 'A'){ int score = n[i+1] - '0'; scoreA += score; }else if(n[i] == 'B'){ int score = n[i+1] - '0'; scoreB += score; } } (scoreA > scoreB) ? cout << 'A' : cout << 'B'; return 0; } question Link :  https://open.kattis.com/problems/basketballoneonone

Kattis Solution to Reverse Binary in C++

Image
#include<bits/stdc++.h> using namespace std; // using namespace std::chrono; int main(int argc, char const *argv[]) {     int n,i=0,t;     cin >> n;       vector<int>v;       // auto start = high_resolution_clock::now();     while(true){         t = n % 2;         v.push_back(t);         n = n / 2;         if(n == 0 ) break;         i++;     }     // auto stop = high_resolution_clock::now();     // auto duration = duration_cast<microseconds>(stop-start);     // cout << duration.count()<<endl;       reverse(v.begin(),v.end());     int index = 0,sum = 0;     for(auto b : v){         if(b == 1){             sum+=pow(2,index);     ...

Kattis Solution to Odd Man out In cpp

Image
#include <bits/stdc++.h> using namespace std ; int main ( int argc , char const * argv [ ]) { int n ; cin >> n ; for ( int i = 0 ; i < n ; ++ i ) { int t ; cin >> t ; set < int > s ; for ( int j = 0 ; j < t ; ++ j ) { int k ; cin >> k ; if ( s . count ( k )) { s . erase ( k ) ; } else { s . insert ( k ) ; } } cout << " Case # " << i +1 << " : " << * s . begin ( ) << " \n " ; } }

Kattis Solution to mixed fraction in cpp

Image
mixedfractions.cpp #include <bits/stdc++.h> using namespace std ; int main ( ) { long numerator , denominator ; while ( cin >> numerator >> denominator && numerator != 0 && denominator != 0 ) { if ( numerator < denominator ) { cout << " 0 " << numerator << " / " << denominator << " \n " ; } else { cout << numerator / denominator << " " << numerator % denominator << " / " << denominator << " \n " ; } } return 0 ; } Problem link:https://open.kattis.com/problems/mixedfractions

Kattis Solution to lost lineup in C++

Image
#include <bits/stdc++.h> using namespace std ; int main ( int argc , char const * argv [ ]) { int n ; cin >> n ; std :: map < int , int > order ; int key ; for ( int i = 2 ; i <= n ; ++ i ) { cin >> key ; order [ key ] = i ; } cout << " 1 " ; for ( int i = 0 ; i < n -1 ; ++ i ) { cout << order [ i ] << " " ; } return 0 ; } Click here for the problem https://open.kattis.com/problems/lostlineup

Kattis Solution to Real Challenge in CPP

Image
#include <bits/stdc++.h> using namespace std ; int main ( int argc , char const * argv [ ]) { long x ; cin >> x ; cout . precision ( 10 ) ; cout << sqrt ( x ) * 4 ; return 0 ; } problem link : https://open.kattis.com/problems/areal

Kattis Solution to Chanukah Challenge in CPP

Image
#include <bits/stdc++.h> using namespace std ; int main ( int argc , char const * argv [ ]) { int n ; cin >> n ; for ( int i = 0 ; i < n ; ++ i ) { int temp , j ; cin >> j >> temp ; int sum = 0 ; for ( int k = 2 ; k < temp +2 ; ++ k ) { sum += k ; } cout << i +1 << " " << sum << " \n " ; } return 0 ; }

Kattis Solution to Death Knight Hero in CPP

Image
#include <bits/stdc++.h> using namespace std ; int main ( int argc , char const * argv [ ]) { int n ; cin >> n ; int won = n ; for ( int i = 0 ; i < n ; ++ i ) { string sequence ; cin >> sequence ; if ( sequence . find ( " CD " ) != -1 ) { won -- ; } } cout << won ; return 0 ; }