各種基本演算法實現總結
各種基本演算法實現小結(七)—— 常用演算法
(均已測試通過)
======================================================================
1、判斷素數
測試環境:VC 6.0 (C)
[cpp:showcolumns] view plain copy ·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150- #include <stdio.h>
- #include <math.h>
- int is_sushu(int n)
- {
- int i, mid;
- mid=(int)sqrt(n);
- for(i=2; i<=mid; i++)
- if(0 == n%i)
- return 0;
- return 1;
- }
- void main()
- {
- int n;
- printf("Enter a num: ");
- scanf("%d", &n);
- if(is_sushu(n))
- printf("%d is sushu!/n", n);
- else
- printf("%d is not sushu.../n", n);
- }
執行結果:
==========================================================
2、 求2-1000之間的所有素數
測試環境:VC 6.0 (C)
[cpp:showcolumns] view plain copy ·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150- #include <stdio.h>
- #include <math.h>
- #define MAX 1000
- int is_sushu(int n)
- {
- int i, mid;
- mid=(int)sqrt(n);
- for(i=2; i<=mid; i++)
- if(0 == n%i)
- return 0;
- return 1;
- }
- void main()
- {
- int i, count;
- count=0;
- for(i=2; i<=MAX; i++)
- if(is_sushu(i))
- {
- count++;
- printf("%5d", i);
- if(0 == count%10)
- printf("/n");
- }
- printf("/n");
- }
執行結果:
==========================================================
3、 驗證哥德巴赫猜想
哥德巴赫猜想:任意一個大於等於6的偶數都可以分解為兩個素數之和
如: 6 = 3+3;100 = 3+97=11+89; 1000 = 3+997=59+941=。。。
測試環境:VC 6.0 (C)
[cpp:showcolumns] view plain copy ·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150- #include <stdio.h>
- #include <math.h>
- #define MAX 1000
- int is_sushu(int n)
- {
- int i, mid;
- mid=(int)sqrt(n);
- for(i=2; i<=mid; i++)
- if(0 == n%i)
- return 0;
- return 1;
- }
- void main()
- {
- int i, mid, n;
- printf("Enter an even num: ");
- scanf("%d", &n);
- mid=n/2;
- for(i=2; i<=mid; i++)
- {
- if(is_sushu(i) && is_sushu(n-i))
- printf("%d = %d + %d/n", n, i, n-i);
- }
- }
執行結果:
==========================================================
4、 求最大公約數(GCD)和最小公倍數(LCM)
測試環境:VC 6.0 (C)
[cpp:showcolumns] view plain copy ·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150- #include <stdio.h>
- void max_min(int &m, int &n)
- {
- int tmp;
- if(m<n)
- {
- tmp=m;
- m=n;
- n=tmp;
- }
- }
- int Cal_GCD(int m, int n)
- {
- int gcd;
- max_min(m, n);
- gcd=m%n;
- while(gcd)
- {
- m=n;
- n=gcd;
- gcd=m%n;
- }
- return n;
- }
- void main()
- {
- int m, n, gcd;
- printf("Enter two num a b: ");
- scanf("%d %d", &m, &n);
- gcd=Cal_GCD(m, n);
- printf("%d and %d GCD: %d/n", m, n, gcd);
- printf("%d and %d LCM: %d/n", m, n, m*n/gcd);
- }
執行結果:
==========================================================
5、統計個數(數字)
用隨機函式產生100個[0,99]範圍內的隨機整數,
統計個位上的數字分別為0,1,2,3,4,5,6,7,8,9的數的個數並打印出來
測試環境:VC 6.0 (C)
[cpp:showcolumns] view plain copy相關推薦
各種基本演算法實現總結
各種基本演算法實現小結(七)—— 常用演算法 (均已測試通過) ====================================================================== 1、判斷素數 測試環境:VC 6.0 (C)
各種基本演算法實現小結(五)—— 排序演算法
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow 也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!  
C/C++:各種基本演算法實現小結(四)—— 圖及其遍歷
各種基本演算法實現小結(四)—— 圖及其遍歷 (均已測試通過) ==================================================================== 圖——深度優先和廣度優先演算法 無向圖
C/C++:各種基本演算法實現小結(一)—— 單鏈表
各種基本演算法實現小結(一)—— 單鏈表 (均已測試通過) ============================================================ 單鏈表(測試通過) 測試環境: Win-TC
C/C++:各種基本演算法實現小結(六)—— 查詢演算法
各種基本演算法實現小結(六)—— 查詢演算法 (均已測試通過) =================================================================== 1、簡單查詢 在一組無序數列中,查詢特定某個數值,並返回其位置
C/C++:各種基本演算法實現小結(七)—— 常用演算法
各種基本演算法實現小結(七)—— 常用演算法 (均已測試通過) ====================================================================== 1、判斷素數 測試環境:VC 6.0
C/C++:各種基本演算法實現小結(二)—— 堆 棧
各種基本演算法實現小結(二)—— 堆 棧 (均已測試通過) ============================================================== 棧——陣列實現 測試環境:Win - TC
C/C++:各種基本演算法實現小結(三)—— 樹與二叉樹
各種基本演算法實現小結(三)—— 樹與二叉樹 (均已測試通過) =================================================================== 二叉樹——先序 測試環境:VC 6.0 (C
C++單鏈表基本演算法實現
C++單鏈表基本演算法實現 #ifndef LinkList_h #define LinkList_h #include <iostream>using namespace std; template <class T> struct Node{ T data;
C++ 靜態連結串列基本演算法實現
C++ 靜態連結串列基本演算法實現 #ifndef StaticLinkList_h #define StaticLinkList_h const int MAXSIZE = 100; template <class T> struct StaticNode{ T data;
C++ 順序棧基本演算法實現
C++ 順序棧基本演算法 #ifndef SeqStack_h #define SeqStack_h #include <iostream> using namespace std; const int StackSize = 1024; template <class T>
C++ 鏈棧 基本演算法實現
C++ 鏈棧 基本演算法實現 #ifndef LinkStack_h #define LinkStack_h #include <iostream> template <class T> struct Node{ T data; struct Node <
C++ 迴圈佇列基本演算法實現
C++ 迴圈佇列基本演算法實現 #ifndef CircleQueue_h #define CircleQueue_h const int QueueSize = 1000; template <class T> class CircleQueue { public: Circl
C++ 鏈佇列基本演算法實現
C++ 鏈佇列基本演算法實現 #ifndef LinkQueue_h #define LinkQueue_h #include <iostream> template <class T> struct Node{ T data; struct Node <
C++優先順序隊列表基本演算法實現
C++優先順序隊列表基本演算法實現 主要採用鏈式結構,進行資料儲存,然後定義一個最後結點指標陣列,將所有優先順序最後一個元素的地址儲存到這個指標陣列中。 #ifndef PriorityQueue_h #define PriorityQueue_h #include <iostream>
各種排列演算法實現
1. 1 3 6 10 2 5 9 4 8 7 void arrange(int n) { //n為列印數字的個數 int note = 1, sum = 0, count = 1; while (sum < n) { s
各種排序演算法的總結
1.時間複雜度 排序方式 時間複雜度 空間複雜度 穩定性 複雜度 平均情況 最好情況 最壞情況 交換排序 氣泡排序
各種排序演算法的總結和比較
1 快速排序(QuickSort)快速排序是一個就地排序,分而治之,大規模遞迴的演算法。從本質上來說,它是歸併排序的就地版本。快速排序可以由下面四步組成。(1) 如果不多於1個數據,直接返回。(2) 一般選擇序列最左邊的值作為支點資料。(3) 將序列分成2部分,一部分都大於支
圖的基本演算法實現(鄰接矩陣與鄰接表兩種方法)
本部落格前面文章已對圖有過簡單的介紹,本文主要是重點介紹有關圖的一些具體操作與應用 一、無向圖 1 無向圖——鄰接矩陣 測試環境:VS2008 #include "stdafx.h" #include <stdlib.h> #include <m
String 常用方法最優演算法實現總結 (三) -- findCommonSubstring 和difference
1. String difference(final String str1, final String str2) 說明:Compares two Strings, and returns the portion where they differ. i.e: ("ahc