返回列表 發帖

兩數對調

本帖最後由 tonyh 於 2017-12-20 17:15 編輯

  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<<"對調前: x="<<x<<" y="<<y<<endl<<endl;
  10.    
  11.     tmp=x;
  12.     x=y;
  13.     y=tmp;

  14.     cout<<endl<<"對調後: x="<<x<<" y="<<y<<endl<<endl;
  15.     system("pause");
  16.     return 0;
  17. }
複製代碼

  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<<"對調前: x="<<x<<" y="<<y<<endl;
  10.     tmp=x;
  11.     x=y;
  12.     y=tmp;
  13.     cout<<endl<<"對調後: x="<<x<<" y="<<y<<endl<<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,z;
  7.     cout<<"依序輸入兩個正整數: ";
  8.     cin>>x>>y;
  9.     cout<<endl<<"對調前: x="<<x<<" y="<<y<<endl<<endl;

  10.     z=x;
  11.     x=y;
  12.     y=z;
  13.      
  14.     cout<<endl<<"對調後: x="<<x<<" y="<<y<<endl<<endl;
  15.     system("pause");
  16.     return 0;
  17. }
複製代碼

TOP

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

TOP

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

TOP

返回列表