xxxxxxxxxx
class Solution {
public:
vector<int> findSwapValues(vector<int>& array1, vector<int>& array2) {
int s1 = 0, s2 = 0;
unordered_set<int> s;
for (int a : array1) s1 += a;
for (int b : array2) {
s2 += b;
s.insert(b);
}
int diff = s1 - s2;
if (diff & 1) {
return {};
}
diff >>= 1;
for (int a : array1) {
int b = a - diff;
if (s.count(b)) {
return {a, b};
}
}
return {};
}
};
xxxxxxxxxx
#include <iostream>
#include <queue>
using namespace std;
struct Node{
int data;
struct Node* left, *right;
};
// Function to count the full Nodes in a binary tree
int fullcount(struct Node* node){
// Check if tree is empty
if (!node){
return 0;
}
queue<Node *> myqueue;
// traverse using level order traversing
int result = 0;
myqueue.push(node);
while (!myqueue.empty()){
struct Node *temp = myqueue.front();
myqueue.pop();
if (temp->left && temp->right){
result++;
}
if (temp->left != NULL){
myqueue.push(temp->left);
}
if (temp->right != NULL){
myqueue.push(temp->right);
}
}
return result;
}
struct Node* newNode(int data){
struct Node* node = new Node;
node->data = data;
node->left = node->right = NULL;
return (node);
}
int main(void){
struct Node *root = newNode(10);
root->left = newNode(20);
root->right = newNode(30);
root->left->left = newNode(40);
root->left->right = newNode(50);
root->left->left->right = newNode(60);
root->left->right->right = newNode(70);
cout <<"count is: "<<fullcount(root);
return 0;
}
xxxxxxxxxx
#include <iostream>
using namespace std;
int main()
{
int i, j, n, sum = 0, tsum;
cout << "\n\n Find the sum of the series (1) + (1+2) + (1+2+3) + (1+2+3+4) + ... + (1+2+3+4+...+n):\n";
cout << "------------------------------------------------------------------------------------------\n";
cout << " Input the value for nth term: ";
cin >> n;
for (i = 1; i <= n; i++)
{
tsum = 0;
for (j = 1; j <= i; j++)
{
sum += j;
tsum += j;
cout << j;
if (j < i)
{
cout << "+";
}
}
cout << " = " << tsum << endl;
}
cout << " The sum of the above series is: " << sum << endl;
}
xxxxxxxxxx
#include <iostream>
#include <queue>
using namespace std;
struct Node{
int data;
struct Node* left, *right;
};
// Function to count the full Nodes in a binary tree
int fullcount(struct Node* node){
// Check if tree is empty
if (!node){
return 0;
}
queue<Node *> myqueue;
// traverse using level order traversing
int result = 0;
myqueue.push(node);
while (!myqueue.empty()){
struct Node *temp = myqueue.front();
myqueue.pop();
if (temp->left && temp->right){
result++;
}
if (temp->left != NULL){
myqueue.push(temp->left);
}
if (temp->right != NULL){
myqueue.push(temp->right);
}
}
return result;
}
struct Node* newNode(int data){
struct Node* node = new Node;
node->data = data;
node->left = node->right = NULL;
return (node);
}
int main(void){
struct Node *root = newNode(10);
root->left = newNode(20);
root->right = newNode(30);
root->left->left = newNode(40);
root->left->right = newNode(50);
root->left->left->right = newNode(60);
root->left->right->right = newNode(70);
cout <<"count is: "<<fullcount(root);
return 0;
}
xxxxxxxxxx
#include<bits/stdc++.h>
using namespace std;
void testfun(map<string, int> mp){
map<string, int>::iterator it = mp.begin();
for(; it!=mp.end(); it++){
cout<<it->first<<":"<<it->second<<" ";
}
cout<<endl;
}
int main()
{
int no, add=0;
cin>>no;
int var;
cin>>var;
map<string, int> d, h;
h["F"] = 0;
while(var--){
string naam;
int a, b;
cin>>naam>>a>>b;
d[naam] = b/a;
h[naam] = b;
add+=b;
}
cin>>var;
while(var--){
string naam, c1="", c2="";
cin>>naam;
int meflux=0;
while(meflux<naam.length()){
if(naam[meflux]=='_'){
c1 = c2;
c2 = "";
meflux++;
continue;
}
c2+=naam[meflux++];
}
h[c2]+= h[c1];
}
//print(days);
//print(storage);
map<string, int>::reverse_iterator a= d.rbegin();
int multiply = 1;
for(; a!=d.rend(); a++ ){
int temperature = a->second*multiply;
if(temperature<no){
add+=(h[a->first]*(no-temperature));
multiply++;
}
}
cout<<add;
}
xxxxxxxxxx
#include<iostream>
#include<iomainp>
Using namespace std;
Int main()
{
Int a, b, x, y;
Std::cin>>a>>b>>x>>y;
Float m =a/6;
Int n =y/6;
Int p =y%6;
Float k= ((float) n +(float) p/10) ;
Float d=x/k;
Float f = b/m;
Std::cout<<std::fixed<<std::setprecision(1) <<k<<"\n";
Std::cout<<d<<"\n"<<f<<"\n";
If(d>f)
Std::cout<<"Eligible to win";
}
xxxxxxxxxx
#ifndef CMAC_H
#define CMAC_H
void AES_CMAC(unsigned char *key, unsigned char *input, int length,
unsigned char *mac);
#endif
xxxxxxxxxx
#include <iostream>
using namespace std;
int main()
{
cout << "## Program Menghitung Gaji Karyawan ##" << endl;
cout << "========================================" << endl;
cout << endl;
string nama;
char golongan;
int jam_kerja, upah_per_jam, total_upah;
// proses input
cout << "Nama Karyawan: ";
getline(cin,nama);
cout << "Golongan: ";
cin >> golongan;
cout << "Jumlah jam kerja: ";
cin >> jam_kerja;
// tentukan jumlah upah per jam berdasarkan golongan
switch (golongan) {
case 'A':
upah_per_jam = 5000;
break;
case 'B':
upah_per_jam = 7000;
break;
case 'C':
upah_per_jam = 8000;
break;
case 'D':
upah_per_jam = 10000;
break;
}
total_upah = jam_kerja * upah_per_jam;
// cek apakah jam kerja lebih dari 48 jam
if ( (jam_kerja - 48) > 0 ) {
total_upah = total_upah + ((jam_kerja - 48)*4000);
}
// proses output
cout << endl;
cout << nama << " menerima upah Rp." << total_upah << " per minggu";
cout << endl;
return 0;
}
xxxxxxxxxx
int playTheGame(int array[],int n)
{
int ans=0;
while(true)
{
int i,j,ctr=0;
for(i=0;i<n;i++)
{
if(array[i] & 1)
{
break;
}
else if(array[i]==0)
{
ctr++;
}
}
if(ctr==n)
{
return ans;
}
if(i==n)
{
for(j=0;j<n;j++)
{
array[j]=array[j]/2;
}
ans++;
}
for(j=i;j<n;j++)
{
if (array[j] & 1)
{
array[j]--;
ans++;
}
}
}
return ans;
}
Jack and jill
C++
xxxxxxxxxx
#include<bits/stdc++.h>
using namespace std;
int main(){
int n,a,b,nowcap=0,maxcap=0;
cin>>n;
while(n--){
cin>>a>>b;
nowcap-=a;nowcap+=b;
maxcap=max(maxcap,nowcap);
}
cout<<maxcap<<endl;
return 0;
}