#include <iostream> #include <sstream> using namespace std; #define M 5 struct Cosa{ string nome="Pippo"; int q; }; struct Negozio{ string nome; Cosa cose[M]; int N=M; }; void inizializzaNegozio(Negozio &neg, string nome){ neg.nome=nome; stringstream ss; for(int i=0;i<neg.N;i++) { ss.str(""); ss<<"Cosa_"<< i; neg.cose[i].nome= ss.str(); neg.cose[i].q=10+i; } } void stampaNegozio(Negozio neg){ cout<<neg.nome<<" ha "<<neg.N<<" parti:\n"; for(int i=0;i<neg.N;i++) cout<<neg.cose[i].nome<<" con la quantita\' di "<< neg.N<<"\n"; } int main() { cout << "structNegozio" << endl; Negozio emporio; inizializzaNegozio(emporio, "Ferramenta"); stampaNegozio(emporio); return 0; }
// Example program #include <iostream> using namespace std; int main(){ //.declare double salary; double z= 0.25; double x= 0.30; double y= 0.32; double compensation; double credit; double contribution; //.code cout<<"Enter your annual gross salary: "; cin>> salary; //. Withholding tax if (salary <=250000){ cout<<"your net annual salary is: "<< salary<< endl; } else if ((salary >=250001)||(salary<=400000)){ cout<<"your net annual salary is: " << (salary-((salary-250000)*.20))<<endl; } else if ((salary >=400001)||(salary <=800000)){ cout<<"your net annual salary is: " << ((salary-((salary-400000)*z)+30000))<<endl; } else if ((salary >=800001)||(salary <=2000000)){ cout<<"your net annual salary is: " << ((salary-((salary-800000)*x)+130000))<<endl; } else if ((salary >=2000001)||(salary <=8000000)){ cout<<"your net annual salary is: " << ((salary-((salary-2000000)*y)+490000))<<endl; } else if (salary >8000000){ cout<<"your net annual salary is: " << ((salary-((salary-8000000)*y)+2041000))<<endl; } else{cout<<"invalid";} } /* //.SSS if(compensation >=1000 && <=1249.99){ contribution = 110.00; } if(compensation >=1250 && <=1749.99){ contribution = 165.00; } if(compensation >=1750 && <=2249.99){ contribution = 220.00; } if(compensation >=2250 && <=2749.99){ contribution = 275.00; } if(compensation >=2750 && <=3249.99){ contribution = 330.00; } if(compensation >=3250 && <=3749.99){ contribution = 385.00; } if(compensation >=3750 && <=4249.99){ contribution = 440.00; } if(compensation >=4250 && <=4749.99){ contribution = 495.00; } if(compensation >=4750 && <=5249.99){ contribution = 550.00; } if(compensation >=5250 && <=5749.99){ contribution = 605.00; } if(compensation >=5750 && <=6249.99){ contribution = 660.00; } if(compensation >=6250 && <=6749.99){ contribution = 715.00; } if(compensation >=6750 && <=7249.99){ contribution = 770.00; } if(compensation >=7250 && <=7749.99){ contribution = 825.00; } if(compensation >=7750 && <=8249.99){ contribution = 880.00; } if(compensation >=8250 && <=8749.99){ contribution = 935.00; } */ return 0; }
#include <iostream> #include <iomanip> using namespace std; int main() { //declare named constants and variables const double RATE1 = 0.02; const double RATE2 = 0.015; double sales = 0.0; double bonus = 0.0; // enter input items cout << ": " ; cin >> code; cout <<" CurrentPay: "; cin >> currentPay ; //calculate raise and new pay if (code == '1' || code == '4' || code == '9') raise = currentPay * RATE1; else raise = currentPay * RATE2; //end if newPay = currentPay + raise; //display new pay cout << fixed << setprecision(2); cout << "New pay : $" << newPay << endl ; return 0; } // end of main fuction
#include <iostream> #include <vector> #include <set> using namespace std; bool woman_prefers(vector<int> woman_preferences, int new_man, int old_man) { for(int i = 0; i < woman_preferences.size(); i++) { if (woman_preferences.at(i) == new_man) return true; if (woman_preferences.at(i) == old_man) return false; } throw 1; } // Outputs vector of length n where vec.at([i]) is the i-th woman's assigned man vector<int> modifiedGS(vector<vector<int>>& men_preferences, vector<vector<int>>& women_preferences) { // cout << "CALLED MGS" << endl; // for(int m = 0; m < men_preferences.size(); m++) { // cout << "m = " << m << ", mp[" << m << "] = [" << men_preferences.at(m).at(0); // for(int i = 1; i < men_preferences.at(m).size();i++) cout << ", " << men_preferences.at(m).at(i); // cout << "]" << endl; // } vector<int> proposal_indices; vector<bool> men_engaged; vector<int> output; for (int i = 0; i < men_preferences.size(); i++) { proposal_indices.push_back(0); men_engaged.push_back(false); output.push_back(-1); } int num_engaged = 0; while (num_engaged < men_preferences.size()) { int free_man = 0; while(men_engaged[free_man]) free_man++; // free_man proposes to men_preferences[proposal_indices[free_man]] if (proposal_indices[free_man] >= men_preferences.size()) { vector<int> no_sm; return no_sm; } int woman = men_preferences[free_man][proposal_indices[free_man]++]; if (output[woman] == -1 || woman_prefers(women_preferences[woman], free_man, output[woman])) { if (output[woman] > 0) men_engaged[output[woman]] = false; else num_engaged++; men_engaged[free_man] = true; output[woman] = free_man; } } for (int& wp : output) { if (wp == -1) { vector<int> no_sm; return no_sm; } } return output; } // Takes a partially reduced men_preferences and women_preferences vectors, // a reference to a set to which to add solutions, and the output of the last // GS run, which is used to trim the preference lists // mp is passed by value so it can be safely modified void recursiveExplore(vector<vector<int>> mp, vector<vector<int>>& wp, set<vector<int>>& output_set, vector<int>& last_run) { // First trim per the last run: for (int w = 0; w < last_run.size(); w++) { // for each woman, remove her match's more preferred picks int m = last_run.at(w); int match_index = 0; while (mp.at(m).at(match_index) != w)match_index++; mp.at(m).erase(mp.at(m).begin(), mp.at(m).begin() + match_index); } // Now, for all men who still have more than one preference, expand the search tree for (int i = 0; i < mp.size(); i++) { if (mp.at(i).size() > 1) { int removed_woman = mp.at(i).at(0); mp.at(i).erase(mp.at(i).begin()); auto res = modifiedGS(mp, wp); if (res.size() > 0) { // if false, matching is no longer possible, cut off this tree branch output_set.insert(res); // log result, and expand on this node recursiveExplore(mp, wp, output_set, res); } // replace the removed woman for the next expansion mp.at(i).insert(mp.at(i).begin(), removed_woman); } } } set<vector<int>> allStableMatchings(vector<vector<int>>& men_preferences, vector<vector<int>>& women_preferences) { set<vector<int>> out; auto gs = modifiedGS(men_preferences, women_preferences); out.insert(gs); recursiveExplore(men_preferences, women_preferences, out, gs); return out; } void printMatching(vector<int> women_matches) { cout << "(w" << 0 << ", m" << women_matches.at(0) << ")"; for (int i = 1; i < women_matches.size(); i++) { cout << ", (w" << i << ", m" << women_matches.at(i) << ")"; } cout << endl; } int main() { vector<vector<int>> mp, wp; vector<int> m1, m2, m3, w1, w2, w3; m1.push_back(0); m1.push_back(1); m1.push_back(2); m2.push_back(1); m2.push_back(2); m2.push_back(0); m3.push_back(2); m3.push_back(0); m3.push_back(1); w1.push_back(1); w1.push_back(2); w1.push_back(0); w2.push_back(2); w2.push_back(0); w2.push_back(1); w3.push_back(0); w3.push_back(1); w3.push_back(2); mp.push_back(m1); mp.push_back(m2); mp.push_back(m3); wp.push_back(w1); wp.push_back(w2); wp.push_back(w3); auto all_matchings = allStableMatchings(mp, wp); for (auto& matching : all_matchings) { printMatching(matching); } return 0; }
/** @file Integer7.h @author Mark Young @date 2013-03-12 @brief overloaded operators (based on ch7.integernamespace.h) */ #include <iostream> #include<cmath> using namespace std; class Integer7 { public: // members Integer7(int = 0); // "big 3" not required // -- default copy constructor, assignment operator, and destructor // all provide suitable behaviour int get() const; void set(int); // member operators // ... from the plus family Integer7& operator+=(const Integer7& i); Integer7& operator+=(int v); // .. from the increment family Integer7& operator++(); // prefix; member of Integer7 Integer7 operator++(int); //postfix; also a member; dummy parameter! int operator*=(int v); // (int, value) int operator*=(Integer7 value); // (value, int) //Integer7 operator*=(Integer7 value1, Integer7 value2); // (value, value) int operator*(int v); // (int, value) int operator*(Integer7 value); // (value, int) //Integer7 operator*(Integer7 value1, Integer7 value2); // (value, value) protected: int value_; }; Integer7::Integer7(int v){ value_=v; } int Integer7::operator*=(int v){ value_=v*value_; return value_; } int Integer7::operator*=(Integer7 value){ value_=value.value_*value_; return value_; } //Integer7 Integer7::operator*=(Integer7 value1, Integer7 value2){ // value1.value_=value1.value_*value2.value_; //return value1; //} int Integer7::operator*(int v){ int v_new=v*value_; return v_new; } int Integer7::operator*(Integer7 value){ Integer7 value_new(value.value_*value_); return value_new.value_; } int operator*(int v, Integer7 value){ int newinteger=value.value_*v; return newinteger; } //Integer7 Integer7::operator*(Integer7 value1, Integer7 value2){ // Integer7 value3=new Integer7(value1.value_*value2.value_); //return value3; //} int main(){ Integer7 m(3); Integer7 n(5); m*=n; n*=3; cout << m * n << endl; cout << m * 17 << endl; //Integer7 integer(13); cout << 13 * n << endl; return 0; }
#include <iostream> #include <vector> using namespace std; int main() { vector <int> arreglo; cout<<arreglo.size()<<endl; arreglo.push_back(54); arreglo.push_back(87); arreglo.push_back(66); arreglo.push_back(84); arreglo.push_back(23); arreglo.push_back(3); arreglo.push_back(25); arreglo.push_back(8); arreglo.push_back(17); arreglo.push_back(98); for (int k=0; k <arreglo.size(); k++) { cout<< k << " " << arreglo [k]<< endl; } } vector <int< primeros (); arreglo.push_back(54); arreglo.push_back(87); arreglo.push_back(66); arreglo.push_back(84); arreglo.push_back(23); for (int k=0; k< 5 ; k++) { arreglo[k]=k; cout<< k << " " << arreglo [k]<< endl; } return 0; }
#include <iostream> #include <sstream> #include <cstring> #include <ctime> #include <unistd.h> #define M 5 using namespace std; struct Cosa{ string nome="Niente"; int pos=0; int q=0; double prezzo=5; }; struct Persona{ string nome; Cosa oggetto; double soldi=30.0; }; struct Negozio{ string nome; Cosa cose[M]; int N=M; Persona commessa; }; void inizializzaNegozio(Negozio &neg, string nome){ srand(time(0)); string nomi[]={"Ada","Gioia","Aurora","Tonia","Eva"}; neg.nome=nome; neg.commessa.nome=nomi[rand()%5]; stringstream ss; for(int i=0;i<neg.N;i++) { ss.str(""); ss<<"Cosa_"<< i; neg.cose[i].nome= ss.str(); neg.cose[i].q=10+i; } } void preleva(Negozio &neg,int pos,int quant){ if(neg.cose[pos].q >= quant) { neg.cose[pos].q -=quant; }else { neg.cose[pos].q=0; cout << "scorte non sufficienti!!!\n"; } } void deposita(Negozio &neg,int pos,int quant){ neg.cose[pos].q += quant; } void stampaNegozio(Negozio neg){ cout<<neg.nome<<" gestita da "+neg.commessa.nome+" ha "<<neg.N<<" parti:\n"; for(int i=0;i<neg.N;i++) cout<<neg.cose[i].nome<<" con la quantita\' di "<< neg.cose[i].q<<"\n"; } void gestioneNegozio(Negozio &neg,string nome){ inizializzaNegozio(neg, nome); stampaNegozio(neg); preleva(neg,2,5); deposita(neg,0,7); stampaNegozio(neg); } void stampaCliente(Persona cliente){ std::cout << cliente.nome << " ha un " << cliente.oggetto.nome; std::cout << " e " << cliente.soldi << " Euro\n"; } int main() { cout << "structNegozio" << endl; Negozio farmacia; inizializzaNegozio(farmacia, "SanitariaFarmaceutica"); stampaNegozio(farmacia); Persona cliente; cliente.nome="Paolo"; stampaCliente(cliente); preleva(farmacia,2,1); cliente.oggetto = farmacia.cose[2]; cliente.soldi -= farmacia.cose[2].prezzo; stampaCliente(cliente); stampaNegozio(farmacia); return 0; }
#include <cstdio> #include <cstdint> struct Fib { struct Arr { uint64_t n[4] = {1, 1, 1, 0}; } mtx; inline uint64_t fib(uint32_t val) { if (val < 3) return 1; return (pwr(mtx, val - 1)).n[0]; } private: inline Arr mltp(Arr a, Arr b) { Arr c; c.n[0] = a.n[0] * b.n[0] + a.n[1] * b.n[2]; c.n[1] = a.n[0] * b.n[1] + a.n[1] * b.n[3]; c.n[2] = a.n[2] * b.n[0] + a.n[3] * b.n[2]; c.n[3] = a.n[2] * b.n[1] + a.n[3] * b.n[3]; return c; } inline Arr pwr(Arr arr, uint64_t power) { if (power == 1) return arr; if (!(power %2)) return pwr(mltp(arr, arr), (int) power >> 1); return mltp(arr, pwr(mltp(arr, arr), (int) power >> 1)); } }; int main() { uint32_t val; Fib fb; scanf("%lu", &val); printf("%llu\n", fb.fib(val)); return 0; }
#include <iostream> #include <vector> using namespace std; // Task: Implement the sortingPermutation function so that it returns a vector // with indices which denote the sorted order of the input. See example. // // Bonus Task: Implement sortingPermutation for arbitrary comparable types // inside the std::vector ( like int, std::string ) std::vector<size_t> sortingPermutation( const std::vector<double>& vec ){ // Implement me return std::vector<size_t>(); } int main() { std::vector<double> test1 = {7,3,5,9}; auto p = sortingPermutation( test1 ); // p should contain [1, 2, 0, 3] return 0; }
We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy. Accept Learn more