1、簡單輸出整數
本題要求實現一個函數,對給定的正整數N
,打印從1到N
的全部正整數。
1 #include <stdio.h> 2 void PrintN ( int N ); 3 int main () 4 { 5 int N; 6 scanf("%d", &N); 7 PrintN( N ); 8 return 0; 9 } 10 void PrintN ( int N ){ 11 for(int i=1; i<=N; ++i) 12 printf("%d\n",i); 13 }
2、多項式求值
本題要求實現一個函數,計算階數為n
,系數為a[0]
... a[n]
的多項式f(x)=∑i=0n(a[i]×xi) 在x
點的值。
1 #include <stdio.h> 2 3 #define MAXN 10 4 5 double f( int n, double a[], double x ); 6 7 int main() 8 { 9 int n, i; 10 double a[MAXN], x; 11 12 scanf("%d %lf", &n, &x); 13 for ( i=0; i<=n; i++ ) 14 scanf(“%lf”, &a[i]); 15 printf("%.1f\n", f(n, a, x)); 16 return 0; 17 } 18 double f( int n, double a[], double x ){ 19 double sum = 0; 20 for(int i=0; i<=n; ++i){ 21 sum += a[i]*pow(x,i); 22 } 23 return sum; 24 }
3、簡單求和
本題要求實現一個函數,求給定的N
個整數的和。
1 #include <stdio.h> 2 3 #define MAXN 10 4 5 int Sum ( int List[], int N ); 6 7 int main () 8 { 9 int List[MAXN], N, i; 10 11 scanf("%d", &N); 12 for ( i=0; i<N; i++ ) 13 scanf("%d", &List[i]); 14 printf("%d\n", Sum(List, N)); 15 16 return 0; 17 } 18 int Sum ( int List[], int N ){ 19 int sum=0; 20 for(int i=0; i<N; ++i) 21 sum += List[i]; 22 return sum; 23 }
4、求自定類型元素的平均
本題要求實現一個函數,求N
個集合元素S[]
的平均值,其中集合元素的類型為自定義的ElementType
。
1 #include <stdio.h> 2 3 #define MAXN 10 4 typedef float ElementType; 5 6 ElementType Average( ElementType S[], int N ); 7 8 int main () 9 { 10 ElementType S[MAXN]; 11 int N, i; 12 13 scanf("%d", &N); 14 for ( i=0; i<N; i++ ) 15 scanf("%f", &S[i]); 16 printf("%.2f\n", Average(S, N)); 17 18 return 0; 19 } 20 ElementType Average( ElementType S[], int N ){ 21 ElementType sum = 0; 22 for(int i=0; i<N; ++i) 23 sum +=S[i]; 24 return sum/N; 25 }
5、求自定類型元素的最大值
本題要求實現一個函數,求N
個集合元素S[]
中的最大值,其中集合元素的類型為自定義的ElementType
。
1 #include <stdio.h> 2 3 #define MAXN 10 4 typedef float ElementType; 5 6 ElementType Max( ElementType S[], int N ); 7 8 int main () 9 { 10 ElementType S[MAXN]; 11 int N, i; 12 13 scanf("%d", &N); 14 for ( i=0; i<N; i++ ) 15 scanf("%f", &S[i]); 16 printf("%.2f\n", Max(S, N)); 17 18 return 0; 19 } 20 ElementType Max( ElementType S[], int N ){ 21 ElementType max = S[0]; 22 for(int i=1; i<N; ++i) 23 if(max < S[i]) 24 max = S[i]; 25 return max; 26 }
6、求單鏈表結點的階乘和
本題要求實現一個函數,求單鏈表L
結點的階乘和。這里默認所有結點的值非負,且題目保證結果在int
范圍內。
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 typedef struct Node *PtrToNode; 5 struct Node { 6 int Data; /* 存儲結點數據 */ 7 PtrToNode Next; /* 指向下一個結點的指針 */ 8 }; 9 typedef PtrToNode List; /* 定義單鏈表類型 */ 10 11 int FactorialSum( List L ); 12 13 int main() 14 { 15 int N, i; 16 List L, p; 17 18 scanf("%d", &N); 19 L = NULL; 20 for ( i=0; i<N; i++ ) { 21 p = (List)malloc(sizeof(struct Node)); 22 scanf("%d", &p->Data); 23 p->Next = L; L = p; 24 } 25 printf("%d\n", FactorialSum(L)); 26 27 return 0; 28 } 29 int FactorialSum( List L ){ 30 int sum = 0; 31 while(L) 32 { 33 int fac=1; 34 for(int i=1; i<=L->Data; ++i) fac *= i; 35 sum += fac; 36 L = L->Next; 37 } 38 return sum; 39 }
7、 統計某類完全平方數
本題要求實現一個函數,判斷任一給定整數N
是否滿足條件:它是完全平方數,又至少有兩位數字相同,如144、676等。
1 #include <stdio.h> 2 #include <math.h> 3 4 int IsTheNumber ( const int N ); 5 6 int main() 7 { 8 int n1, n2, i, cnt; 9 10 scanf("%d %d", &n1, &n2); 11 cnt = 0; 12 for ( i=n1; i<=n2; i++ ) { 13 if ( IsTheNumber(i) ) 14 cnt++; 15 } 16 printf("cnt = %d\n", cnt); 17 18 return 0; 19 } 20 int IsTheNumber ( const int N ){ 21 int i,ret=0; 22 for (i = 0; i <= N / 2; i++) 23 if (i*i == N){ 24 ret++; 25 break; 26 } 27 int key[10] = {0}; 28 int t = N; 29 while(t) 30 { 31 key[t%10]++; 32 t /= 10; 33 } 34 for(int j=0; j<=9; ++j) 35 if(key[j]>=2){ 36 ret++; 37 break; 38 } 39 return ret == 2; 40 }
8、簡單階乘計算
本題要求實現一個計算非負整數階乘的簡單函數。
1 #include <stdio.h> 2 3 int Factorial( const int N ); 4 5 int main() 6 { 7 int N, NF; 8 9 scanf("%d", &N); 10 NF = Factorial(N); 11 if (NF) printf("%d! = %d\n", N, NF); 12 else printf("Invalid input\n"); 13 14 return 0; 15 } 16 int Factorial( const int N ){ 17 if(N<0) return 0; 18 int fac=1; 19 for(int i=1; i<=N; ++i) 20 fac *=i; 21 return fac; 22 }
9、統計個位數字
本題要求實現一個函數,可統計任一整數中某個位數出現的次數。例如-21252中,2出現了3次,則該函數應該返回3。
1 #include <stdio.h> 2 3 int Count_Digit ( const int N, const int D ); 4 5 int main() 6 { 7 int N, D; 8 9 scanf("%d %d", &N, &D); 10 printf("%d\n", Count_Digit(N, D)); 11 return 0; 12 } 13 int Count_Digit ( const int N, const int D ){ 14 if(!N&&!D) return 1; 15 int count=0,t = N; 16 if(t<0) t = -t; 17 while(t) 18 { 19 if(t%10==D) 20 count++; 21 t /= 10; 22 } 23 return count; 24 }
10、階乘計算升級版
本題要求實現一個打印非負整數階乘的函數。
1 #include <stdio.h> 2 3 void Print_Factorial ( const int N ); 4 5 int main() 6 { 7 int N; 8 9 scanf("%d", &N); 10 Print_Factorial(N); 11 return 0; 12 } 13 void Print_Factorial ( const int N ){ 14 if(N<0){ 15 printf("Invalid input"); 16 return; 17 } 18 int a[100000] = {0,1},index=1; 19 for(int n = 1; n<=N; ++n) 20 { 21 int carry = 0;/**< 進位數 */ 22 for(int j=1; j<=index; j++)/**< 乘數與被乘數,從第一位開始,逐位相乘 */ 23 { 24 int temp = n*a[j] + carry;/**< 乘積 + 進位數 */ 25 a[j] = temp%10;/**< 乘積 + 進位數,保留個位 */ 26 carry = temp/10;/**< 進位 */ 27 } 28 while(carry > 0)/**< 進位數大於0,最高位++,最高位保留進位數個位 */ 29 { 30 a[++index] = carry%10; 31 carry /= 10; 32 } 33 } 34 for(int i=index; i>0;--i) 35 printf("%d",a[i]); 36 }
11、求自定類型元素序列的中位數
本題要求實現一個函數,求N
個集合元素A[]
的中位數,即序列中第⌊N/2+1⌋大的元素。其中集合元素的類型為自定義的ElementType
1 #include <stdio.h> 2 3 #define MAXN 10 4 typedef float ElementType; 5 6 ElementType Median( ElementType A[], int N ); 7 8 int main () 9 { 10 ElementType A[MAXN]; 11 int N, i; 12 13 scanf("%d", &N); 14 for ( i=0; i<N; i++ ) 15 scanf("%f", &A[i]); 16 printf("%.2f\n", Median(A, N)); 17 18 return 0; 19 } 20 void HeapAdjust(ElementType array[], int i, int nlength) 21 { 22 ElementType nTemp; 23 for (int nChild; 2 * i + 1 < nlength; i = nChild) 24 { 25 nChild = 2 * i + 1; 26 //得到子節點中較大的節點 27 if (nChild < nlength - 1 && array[nChild + 1] > array[nChild]) 28 nChild++; 29 //如果較大的子節點大於父節點那么把較大的子節點往上移動,替換它的父節點 30 if (array[i] < array[nChild]) 31 { 32 nTemp = array[i]; 33 array[i] = array[nChild]; 34 array[nChild] = nTemp; 35 } 36 else 37 break; 38 } 39 } 40 void Heapsort(ElementType array[], int length) 41 { 42 int i;//調整序列的前半部分元素,調整完之后第一個元素是序列的最大元素 43 ElementType temp; 44 for (i = length/2 - 1; i >= 0; i--) 45 HeapAdjust(array, i, length); 46 //從最后一個元素開始對序列進行調整,不斷的縮小調整的范圍直到第一個元素 47 for (i = length - 1; i >= 0; i--) 48 { 49 //把第一個元素和當前的最后一個元素交換 50 //保證當前的最后一個位置的元素都是現在這個序列中最大的 51 temp = array[0]; 52 array[0] = array[i]; 53 array[i] = temp; 54 //不斷縮小調整heap的范圍,每一次調整完畢保證第一個元素是當前序列的最大值 55 HeapAdjust(array, 0, i); 56 } 57 } 58 ElementType Median(ElementType A[], int N) 59 { 60 Heapsort(A, N); 61 return A[N / 2]; 62 }
12、判斷奇偶性
本題要求實現判斷給定整數奇偶性的函數。
1 #include <stdio.h> 2 3 int even( int n ); 4 5 int main() 6 { 7 int n; 8 9 scanf("%d", &n); 10 if (even(n)) 11 printf("%d is even.\n", n); 12 else 13 printf("%d is odd.\n", n); 14 15 return 0; 16 } 17 int even( int n ){ 18 if(n%2) 19 return 0; 20 return 1; 21 }
13、折半查找
給一個嚴格遞增數列,函數int Search_Bin(SSTable T, KeyType k)用來二分地查找k在數列中的位置。
1 #include <iostream> 2 using namespace std; 3 4 #define MAXSIZE 50 5 typedef int KeyType; 6 7 typedef struct 8 { 9 KeyType key; 10 } ElemType; 11 12 typedef struct 13 { 14 ElemType *R; 15 int length; 16 } SSTable; 17 18 void Create(SSTable &T) 19 { 20 int i; 21 T.R=new ElemType[MAXSIZE+1]; 22 cin>>T.length; 23 for(i=1;i<=T.length;i++) 24 cin>>T.R[i].key; 25 } 26 27 int Search_Bin(SSTable T, KeyType k); 28 29 int main () 30 { 31 SSTable T; KeyType k; 32 Create(T); 33 cin>>k; 34 int pos=Search_Bin(T,k); 35 if(pos==0) cout<<"NOT FOUND"<<endl; 36 else cout<<pos<<endl; 37 return 0; 38 } 39 int Search_Bin(SSTable T, KeyType k){ 40 int left, right, mid, NotFound=0; 41 left = 1; /*初始左邊界*/ 42 right = T.length; /*初始右邊界*/ 43 while ( left <= right ) 44 { 45 mid = (left+right)/2; /*計算中間元素坐標*/ 46 if( k < T.R[mid].key) right = mid-1; /*調整右邊界*/ 47 else if( k > T.R[mid].key) left = mid+1; /*調整左邊界*/ 48 else return mid; /*查找成功,返回數據元素的下標*/ 49 } 50 return NotFound; /*查找不成功,返回-1*/ 51 }