Jump to content
JohnyCNAM

Transformare pseudocod c++

Recommended Posts

Posted (edited)

Merci mult.

Daca imi faci si varianta 2 esti cel mai tare.

LE:Mersi fratilor am rezolvat-o.Am luat 2 10 :D.

Habar nu am de ce erau sa de grele.

Restul am facut singur.

Oricum merci totti.

Edited by ionutcristea
analfabetism
Posted


#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))

Posted

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 :)

Posted

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;
}

Posted

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,B);
cout<<a<<" "<<b;
return 0;
}

3 2

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...