zoukankan      html  css  js  c++  java
  • 在不申请第三方变量的情况下交换a和b

    1、算数运算

    a=a*b;b=a/b;a=a/b;

    2、XOR运算

    a和bxor运算两次还是 a

    3、栈运算

    stack<数据类型>s

    4、程序运行 

     1 #include <iostream>
     2 #include<stdlib.h>
     3 #include<stack>//STL里重要的函数 
     4 using namespace std;
     5 /*void _stack(int x,int y){
     6 stack<int>s;
     7 s.push(x);
     8 s.push(y);
     9 x = s.top();
    10 s.pop();
    11 y = s.top();
    12 s.pop();
    13 cout << x << " " << y << endl;
    14 }
    15 void _arithmetic(int a, int b){
    16 a = a*b;
    17 b = a / b;
    18 a = a / b;
    19 cout << a << " " << b << endl;
    20 }
    21 void _xor(int a, int b){
    22 a = a^b;
    23 b = a^b;
    24 a = a^b;
    25 cout << a << " " << b << endl;
    26 }*/
    27 class _swap{
    28 public:
    29     void _valueab(){ cout << "请输入a和b的值:" << endl; cin >> a >> b; }
    30     void _stack(){
    31         stack<int>s;
    32         s.push(a);
    33         s.push(b);
    34         a = s.top();
    35         s.pop();
    36         b = s.top();
    37         s.pop();
    38         cout << a << " " << b << endl;
    39     }
    40     void _arithmetic(){
    41         a = a*b;
    42         b = a / b;
    43         a = a / b;
    44         cout << a << " " << b << endl;
    45     }
    46     void _xor(){
    47         a = a^b;
    48         b = a^b;
    49         a = a^b;
    50         cout << a << " " << b << endl;
    51     }
    52 private:
    53     int a, b;
    54 };
    55 int main(){//这里要用int,因为main函数会自带return 0,所以应该写int
    56     _swap function;
    57     function._valueab();
    58     function._stack();
    59     function._arithmetic();
    60     function._xor();
    61     system("pause");
    62 }

    5、运行结果

  • 相关阅读:
    java web分页查询初试
    SQL注入原理深度解析
    JS 清除IE缓存
    Android 代码混淆及第三方jar包不被混淆
    [leetcode]Unique Paths II
    ffmpeg API录制rtsp视频流
    HDU 2045 不容易系列之(3)—— LELE的RPG难题
    Ffmpeg和SDL创建线程(转)
    “富豪相亲大会”究竟迷失了什么?
    Ffmpeg和SDL如何同步视频(转)
  • 原文地址:https://www.cnblogs.com/hehesunshine/p/11609916.html
Copyright © 2011-2022 走看看