不同语言实现两个变量的交换 Python之禅

209 篇文章 7 订阅
73 篇文章 1 订阅

 

目录

Python

The Zen of Python

C/C++

C#

Java


Python

x = 10
y = 100
x, y = y, x
print(x)
print(y)

The Zen of Python

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

 

C/C++

#include <iostream>

using namespace std;

void main()
{

	int a = 10;
	int b = 100;
	int temValue = 0;

	printf("--------------before---------------------");
	printf("a = %d ; b = %d\n", a, b);

	temValue = a;
	a = b;
	b = temValue;

	printf("--------------after---------------------");
	printf("a = %d ; b = %d\n", a, b);

}

C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Change
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 10;
            int b = 100;
            Console.WriteLine("---------------before--------------");
            Console.WriteLine(" a is {0}, b is {1} \n", a, b);
            ChangValue(ref a, ref b);
            Console.WriteLine("---------------after--------------");
            Console.WriteLine(" a is {0}, b is {1} \n", a, b);
            ChangValueNo(b:100, a:10);
            Console.WriteLine(" ChangValueNo : a is {0}, b is {1} \n", a, b);

            Console.Read();
        }

       static void ChangValue(ref int a, ref  int b)
    {
        int temp = a;
        a = b;
        b = temp;
    }
       static void ChangValueNo( int a,   int b)
       {
           int temp = a;
           a = b;
           b = temp;
       }

    }
}

Java

package com.company;

public class JavaLearn {

    public  static  void main(String[] args)
    {
        int x = 1;
        int y = 100;
        changValue( x,  y);

        System.out.print(x+"\n");
        System.out.print(y+"\n");
        System.out.printf("x is %d, y is %d " + "\n", x, y);
        int arrTest[] = new int[3];
        for (int i = 0; i < arrTest.length; i++)
        {
            System.out.format("arrTest[%d] is %d\n", i, arrTest[i]);
        }
        //默认初始化:数组是引用类型,它的元素相当于类的成员变量,
        // 因此数组分配空间后,每个元素也被按照成员变量的规则被隐士初始化。
        int arr[] = {1, 100};
        changValueArr( arr);
        System.out.printf("x is %d, y is %d ", arr[0], arr[1]);
    }

    private static void changValue(int x, int y)
    {
        int temp = x;
        x = y;
        y = temp;
    }
    private static void changValueArr(int[] arr)
    {
        int temp = arr[0];
        arr[0] = arr[1];
        arr[1] = temp;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值