1. 程式人生 > >各種基本演算法實現總結

各種基本演算法實現總結

各種基本演算法實現小結(七)—— 常用演算法

(均已測試通過)

======================================================================

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
  1. #include <stdio.h>
      
  2. #include <math.h>  
  3. int is_sushu(int n)  
  4. {  
  5.     int i, mid;  
  6.     mid=(int)sqrt(n);  
  7.     for(i=2; i<=mid; i++)  
  8.         if(0 == n%i)  
  9.             return 0;  
  10.     return 1;  
  11. }  
  12. void main()  
  13. {  
  14.     int n;  
  15.       
  16.     printf("Enter a num: ");  
  17.     scanf("%d", &n);  
  18.     if(is_sushu(n))  
  19.         printf("%d is sushu!/n", n);  
  20.     else  
  21.         printf("%d is not sushu.../n", n);  
  22. }  

執行結果:

    

==========================================================

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
  1. #include <stdio.h>  
  2. #include <math.h>  
  3. #define MAX 1000  
  4. int is_sushu(int n)  
  5. {  
  6.     int i, mid;  
  7.     mid=(int)sqrt(n);  
  8.     for(i=2; i<=mid; i++)  
  9.         if(0 == n%i)  
  10.             return 0;  
  11.     return 1;  
  12. }  
  13. void main()  
  14. {  
  15.     int i, count;  
  16.       
  17.     count=0;  
  18.       
  19.     for(i=2; i<=MAX; i++)  
  20.         if(is_sushu(i))  
  21.         {  
  22.             count++;  
  23.             printf("%5d", i);  
  24.                 if(0 == count%10)  
  25.                     printf("/n");  
  26.         }  
  27.     printf("/n");  
  28. }  

執行結果:

==========================================================

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
  1. #include <stdio.h>  
  2. #include <math.h>  
  3. #define MAX 1000  
  4. int is_sushu(int n)  
  5. {  
  6.     int i, mid;  
  7.     mid=(int)sqrt(n);  
  8.     for(i=2; i<=mid; i++)  
  9.         if(0 == n%i)  
  10.             return 0;  
  11.     return 1;  
  12. }  
  13. void main()  
  14. {  
  15.     int i, mid, n;  
  16.       
  17.     printf("Enter an even num: ");  
  18.     scanf("%d", &n);  
  19.       
  20.     mid=n/2;  
  21.     for(i=2; i<=mid; i++)  
  22.     {  
  23.         if(is_sushu(i) && is_sushu(n-i))  
  24.             printf("%d = %d + %d/n", n, i, n-i);  
  25.     }  
  26.       
  27. }  

執行結果:

       

==========================================================

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
  1. #include <stdio.h>  
  2. void  max_min(int &m, int &n)  
  3. {  
  4.     int tmp;  
  5.     if(m<n)  
  6.     {  
  7.         tmp=m;  
  8.         m=n;  
  9.         n=tmp;  
  10.     }  
  11. }  
  12. int Cal_GCD(int m, int n)  
  13. {  
  14.     int gcd;  
  15.       
  16.     max_min(m, n);  
  17.     gcd=m%n;  
  18.     while(gcd)  
  19.     {  
  20.         m=n;  
  21.         n=gcd;  
  22.         gcd=m%n;  
  23.     }  
  24.     return n;  
  25. }  
  26. void main()  
  27. {  
  28.     int m, n, gcd;  
  29.       
  30.     printf("Enter two num a b: ");  
  31.     scanf("%d %d", &m, &n);  
  32.     gcd=Cal_GCD(m, n);  
  33.     printf("%d and %d GCD: %d/n", m, n, gcd);  
  34.     printf("%d and %d LCM: %d/n", m, n, m*n/gcd);  
  35. }  

執行結果:

==========================================================

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