JohnyCNAM Posted June 11, 2012 Report Share Posted June 11, 2012 Ma puteti ajuta cu ceva?Am de transformat urmatorul pseudocod in cod c++.http://competente2011.wikispaces.com/file/view/Algoritmi+elementari.pdfVarianta 1.Va multumesc. Quote Link to comment Share on other sites More sharing options...
totti93 Posted June 11, 2012 Report Share Posted June 11, 2012 #include <iostream>using namespace std;int main() { float a, b, x; cin >> a >> b; x = a; a = b; b = x; cout << a << endl << b; return 0;} Quote Link to comment Share on other sites More sharing options...
JohnyCNAM Posted June 11, 2012 Author Report Share Posted June 11, 2012 (edited) Merci mult.Daca imi faci si varianta 2 esti cel mai tare.LE:Mersi fratilor am rezolvat-o.Am luat 2 10 .Habar nu am de ce erau sa de grele.Restul am facut singur.Oricum merci totti. Edited June 11, 2012 by ionutcristea analfabetism Quote Link to comment Share on other sites More sharing options...
totti93 Posted June 11, 2012 Report Share Posted June 11, 2012 #include <iostream>using namespace std;int main() { float a, b; cin >> a >> b; a = a - b; b = a + b; a = b - a; cout << a << endl << b;}Aceasta metoda nu este chiar asa de buna, deoarece poti face overflow (marimea a + b poate trece de sizeof(float)) Quote Link to comment Share on other sites More sharing options...
M2G Posted June 11, 2012 Report Share Posted June 11, 2012 Hai ma ca e simplu.Uita-te cum a facut totti la prima varianta. Varianta a doua are diferit fata de prima doar acele asignari de variabile:in loc de x = a ai a = a - b. La fel si la celelalte...Deci codul e cam asa:#include <iostream>using namespace std;int main() { float a, b; cin >> a >> b; a = a - b; b = a + b; a = b - a; cout << a << endl << b; return 0;}//sorry ai fost mai rapid totti Quote Link to comment Share on other sites More sharing options...
totti93 Posted June 11, 2012 Report Share Posted June 11, 2012 Daca te intereseaza o alta metoda de swap, uite aici cu XOR (nu merge pe float):#include <iostream>using namespace std;int main() { int a, b; cin >> a >> b; a = a ^ b; b = a ^ b; a = a ^ b; cout << a << endl << b;} Quote Link to comment Share on other sites More sharing options...
ionut.hulub Posted June 11, 2012 Report Share Posted June 11, 2012 sau a = a-bb += aa = b-adesi e mai simplu de retinut varianta cu xor Quote Link to comment Share on other sites More sharing options...
Matei Posted June 16, 2012 Report Share Posted June 16, 2012 si nici nu te mai obsesti sa folosesti o variabila in plus. mi se pare mai rapida varianta cu XOR Quote Link to comment Share on other sites More sharing options...
em Posted June 16, 2012 Report Share Posted June 16, 2012 Extra:Am observat c? pu?in? lume ?tie c? avem metod? de swap ?i în std. (std::swap).#include <iostream>using namespace std;int main(){ int a(2), b(3); swap(a,; cout<<a<<" "<<b; return 0;}3 2 Quote Link to comment Share on other sites More sharing options...
bodostee Posted March 8, 2022 Report Share Posted March 8, 2022 Inceput Intregi n,a,b; Citeste n; a<-1; b<=1; Cat timp a*a+b*b<=n executa Daca a*a+b*b=n; Scrie a,b; Sfarsit Daca a<-a+1; b<-[radical(n-a*a)]; Sfarsit Cat timp Sfarsit In c++ Quote Link to comment Share on other sites More sharing options...