切換
舊版
前往
大廳
主題

如何用一個暫存變數搞定三數字的交換(C++

虛鹿 | 2019-01-20 13:49:04 | 巴幣 2 | 人氣 1025

度的,
今天教你如何用一個暫存變數搞定三數字的交換。

題目:
a=40 b=20 c=100
將其交換成:
a=20 b=100 c=40

以下為程式碼
----------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>


int main(void)
{
int a=40; //a設為40
int b=20;  //b設為20
int c=100;  //c設為100
int temp;  //暫存變數的temp,初值為0


printf("a is %d , b is %d , c is %d \n", a, b, c);    //原本各個數值


temp=c;  // 將c的值匯入temp中  (此時: a=40 b=20 c=100 temp=100
c=a;        // 將a的值匯入c中        (此時: a=40 b=20 c=40 temp=100
a=b;        // 將b的值匯入a中        (此時: a=20 b=20 c=40 temp=100
b=temp; //將temp的值匯入b中   (此時: a=20 b=100 c=40 temp=100



printf("a is %d , b is %d , c is %d \n", a, b, c);     //後來各個數值


system("pause");
return 0;
}
----------------------------------------------------------------------------
>
>
就這樣,
很簡單八!!!!

送禮物贊助創作者 !
0
留言

創作回應

懶人卡夏
2019-01-20 17:21:45
Legato
還真的很簡單 過了頭
2019-01-22 23:07:44
虛鹿
發廢文賺經驗值達成
2019-01-22 23:10:33

更多創作