c語言實現兩數交換
藉助第三變數來交換
- 1、
int a = 10;
int b = 20;
int temp = a;
a = b;
b = temp;
- 2、(指標實現)
void swap(int*x, int*y)
{
int temp = *x;
*x = *y;
*y = temp;
}
在呼叫swap()時注意傳參時傳的是&a,和&b
swap(&a, &b);
不建立第三變數交換
1、按位異或
a = a^b;
b = a^b;
a = a^b;
例如:
a=10—— 1010
b=5——–0101
那麼(a^b )^b=1111^0101=1010 就是a 的值
同理(a^b)^b(這裡b已經是a了) 為1111^1010=0101
和與差方法
a = a + b;
b = a - b;
a = a - b;
相關推薦
c語言實現兩數交換
藉助第三變數來交換 1、 int a = 10; int b = 20; int temp = a; a = b; b = temp; 2、(指標實現) void swap(int*x, int*y)
C語言實現兩數相加2018-09-23
/*給定兩個非空連結串列來表示兩個非負整數。位數按照逆序方式儲存,它們的每個節點只儲存單個數字。將兩數相加返回一個新的連結串列。 你可以假設除了數字 0 之外,這兩個數字都不會以零開頭。 示例: 輸入:(2 -> 4 -> 3) + (5 -> 6 -> 4)輸出:7 -> 0
C語言/C++實現兩數交換函式
首先我們可以先引入一箇中間變數temp進行數值交換。 在C語言中: 下面的這個函式能不能達到函式交換的目的? void Swap(int x, int y) { int temp = 0; temp = x; x = y; y = temp; } in
用c語言實現兩個陣列中的內容進行交換。(陣列一樣大)
方法一:建立新的陣列。 #include<stdio.h> #include<stdlib.h> int main() { int arr1[5] = { 1, 2, 3, 4, 5, }; int arr2[5] = { 0, 6, 7, 8, 9, }
C語言實現兩個數的交換
常常用C寫排序演算法時,經常寫兩個數的交換。想來,也把自己知到的這些方式記錄一下: (1)巨集定義方法: #define SWAP(a, b) {a = a + b; b = a - b; a = a - b;} 使用示例: int a[2] = {5, 1}; SW
用C語言實現兩變數內容交換的N種方法
一:建立變數實現交換 1.#include <stdio.h> int main() { int a = 10; int b = 20; int c; printf("before change:a=%d b=%d\n",a,b); c
C語言實現常用數據結構——棧
pre sta printf \n all pri oid isp return #include<stdio.h> #include<stdlib.h> //用鏈表實現棧 typedef struct Node { int data;
C語言實現 兩個int(32位)整數m和n的二進位制表達中,有多少個位(bit)不同?
輸入例子: 1999 2299 輸出例子:7 int main() { int a = 0; int b = 0; int num = 0; int count = 0; printf("請輸入兩個整數:"); scanf("%d%d",&a,&b); n
用C語言實現兩個連結串列查集
#include<stdio.h> #include<stdlib.h> typedef struct node{ int data; struct node *next; }LinkList; //建立連結串列 LinkLis
C語言實現兩個數的最大公約數
(1)窮舉法 c=兩個數中小的一個,給兩個數分別取餘c,判斷餘數是否為0 為0,c就是最大公約數 不為0,依次給c–判斷啥時候兩個數的餘數都為0,即為最大公約數 #include<stdio.h> int main() { int a =
^異或實現兩數交換
轉載於:https://blog.csdn.net/zxm1306192988/article/details/50446399 原文:https://blog.csdn.net/u010141928/article/details/76140165 通常我們實現兩數交換不得不引
使用異或運算實現兩數交換
通常我們實現兩數交換不得不引入一個臨時變數temp作為媒介,而使用異或運算也能實現同樣的功能,甚至無需使用臨時變數。 這是一個通常的做法: int main(){ int a=1,b=2,temp; temp=a; a=b; b=temp; printf("%d,%d\n",a,b); ret
C語言實現的數獨解題程式
用最暴力的遞迴方式在所有可能的空間中尋找數獨的解法。試了一下,不管多難的數獨都能在1s內找到所有答案,所以也沒有采取更智慧的演算法進行優化,如加入人的邏輯推理演算法。 這裡只是把一種最笨的方法分享出來,只是感嘆現在的計算機運算能力太強大了。原始碼如下: #include
C語言實現兩個矩陣相乘
#define _CRT_SECURE_NO_WARNINGS 1 #include<stdio.h> #include<stdlib.h> #define ROW 3 #define COL 3 #define MID 4 int main() {
C語言實現 螺旋數 方陣
題目是來自C的一道面試題:向一個 NXN 的矩陣中寫入螺旋數 例如:N=3,顯示如下: 1 2 3 8 9 4 7 6 5 附上程式碼 #include <stdio.h> #include <process.h> #include <co
C語言實現十進位制數轉二進位制數的演算法
因為C語言的printf函式的格式化輸出只支援八進位制和十六進位制的輸出,所以二進位制需要自己實現。 這其中有幾個難點,一是演算法,二是資料結構。可以用二不斷除被除數取餘數,倒序輸出,還可以像我這麼想,用位運算和移位操作進行輸出,因為本身在記憶體中就是二級制的形式存放的,可
C語言 實現兩張圖片的拼接
在Linux中用gcc編譯 這個程式碼的邏輯其實蠻簡單的,就是用read()函式將對應的要拼接的部分,讀入到一塊兒快取中(定義一個數組就行),最後用write()函式將切出來的對應的部分輸出到要拼接的圖片檔案就大功告成。 值得注意的有兩點:1、圖片在必須用.
C語言實現兩臺電腦通過串列埠通訊
用C語言實現在傳送端控制檯輸入字串並在接收端接收顯示的功能。 /*********************server.c****************/#include<stdio.h>#include<sys/types.h>#include
c語言實現二進位制數轉換為十進位制
// 二進位制轉十進位制.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include "stdio.h" int xn(int num,int n)//
[日常練習] 2. 基於函式輸出9*9乘法表、交換兩數、判斷閏年、清空/初始化陣列、判斷素數的C語言實現
在C語言學習中,我們知道它是面向過程進行程式設計的,強調的是功能行為,其主要框架為:資料結構+演算法。在此也可以理解成:資料+函式。其實,函式在C語言學習中無時無刻不在使用,最為簡單的#include<stdio.h>,這便是我們程式的開頭,也是我們所呼叫的第一個函式,稱為:庫函式。