返回列表 發帖

兩數對調

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     int x,y,tmp;
  7.     cout<<"請依序輸入兩個正整數: ";
  8.     cin>>x>>y;
  9.     cout<<"對調前: ";
  10.     cout<<"x="<<x<<" y="<<y<<endl<<endl;
  11.    
  12.     tmp=x;
  13.     x=y;
  14.     y=tmp;
  15.    
  16.     cout<<"對調後: ";
  17.     cout<<"x="<<x<<" y="<<y<<endl<<endl;
  18.    
  19.     system("pause");
  20.     return 0;   
  21. }
複製代碼

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main(){
  5. int x,y,tmp;
  6. re:
  7.     system("cls");
  8. cout<<"請依序輸入兩正整數";
  9. cin>>x>>y;
  10. cout<<"x="<<x<<"y="<<y<<endl;
  11. tmp=x;//x值丟給tmp
  12. x=y;//y值丟給x
  13. y=tmp;//同理
  14. cout<<endl;
  15. cout<<"對調後:"<<endl;
  16. cout<<"x="<<x<<"y="<<y<<endl;


  17. system("pause");
  18. goto re;
  19. return 0;

  20. }
複製代碼

TOP

本帖最後由 李唯銘 於 2024-7-6 14:45 編輯
  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     re:
  7.     int x,y,z;
  8.     cout<<"依序輸入兩個數"<<endl;
  9.     cin>>x>>y;
  10.     cout<<"對調前:x="<<x<<" y="<<y<<endl;
  11.     z=x;
  12.     x=y;
  13.     y=z;
  14.     cout<<"對調後:x="<<x<<" y="<<y<<endl;


  15.     system ("pause");
  16.     goto re;
  17.     return 0;
  18. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main(){
  5. int a,b,c;


  6. cout<<"請依序輸入兩個數: ";
  7. cin>>a>>b;
  8. cout<<"對調前:";
  9. cout<<"a="<<a<<",b="<<b<<endl;
  10. c=a;
  11. a=b;
  12. b=c;

  13. cout<<"對調後: ";
  14.     cout<<"a="<<a<<",b="<<b<<endl;


  15.     system("pause");
  16.     return 0;
  17. }
複製代碼

TOP

  1. #include <iostream>
  2. #include <cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     int x,y,a;
  7.     cout<<"請輸入兩個正整數:";
  8.     cin>>x>>y;
  9.     cout<<"對調前x="<<x<<" y="<<y<<endl;
  10.     a=x;
  11.     x=y;
  12.     y=a;
  13.     cout<<"對調後x="<<x<<" y="<<y<<endl;
  14.     cout<<endl;
  15.     system("pause");
  16.     return 0;
  17. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main(){
  5.     int a,b,tmp;
  6.     cout<<"請依序輸入兩個正整數:"<<endl;
  7.     cin>>a>>b;
  8.     cout<<endl;
  9.     cout<<"對調前: "<<"x="<<a<<",y="<<b<<endl;

  10.     tmp=a;
  11.     a=b;
  12.     b=tmp;

  13.     cout<<"對調後: "<<"x="<<a<<",y="<<b<<endl;


  14. system("pause");
  15. return 0;
  16. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     int x, y, tmp;
  7.     cout<<"請依序輸入兩個正整數: ";
  8.     cin>>x>>y;
  9.     cout<<endl;
  10.     cout<<"對調前: ";
  11.     cout<<"x="<<x<<" y="<<y<<endl<<endl;
  12.     tmp=x;
  13.     x=y;
  14.     y=tmp;
  15.     cout<<"對調後: ";
  16.     cout<<"x="<<x<<" y="<<y<<endl<<endl;
  17.     system("pause");
  18.     return 0;
  19. }
複製代碼

TOP

返回列表