數組 一維數組 定義:類型符 數組名 [常量表達式] int a[]={1,2,3,4,5,}; 1 #include<stdio.h> 2 #define NUM 6 3 void main() 4 { 5 int a[NUM]; 6 printf("輸入NUM個整數:\n"); 7 for(in ...
數組
一維數組
定義:類型符 數組名 [常量表達式] int a[]={1,2,3,4,5,};
1 #include<stdio.h> 2 #define NUM 6 3 void main() 4 { 5 int a[NUM]; 6 printf("輸入NUM個整數:\n"); 7 for(int i=0;i<NUM;i++) 8 { 9 scanf("%d",&a[i]); 10 } 11 12 13 for(i=1;i<NUM;i++) 14 { 15 for(int j=0;j<NUM-i;j++) 16 { 17 if(a[j]<a[j+1]) 18 { 19 int t=a[j]; 20 a[j]=a[j+1]; 21 a[j+1]=t; 22 } 23 } 24 } 25 for(i=0;i<NUM;i++) 26 { 27 printf("%d ",a[i]); 28 } 29 printf("\n"); 30 }View Code
二維數組
定義:類型說明符 數組名 [常量表達式] [常量表達式] int a[3][4]={{1},{4,2}};
字元數組
char c[]=”china”;
輸出 Puts(字元數組) 輸入 Gets(字元數組)
連接 stract(字元數組1, 字元數組2)
複製 strcpy(字元數組1, 字元串2) strnpy(字元數組1, 字元串2,n)
比較 strcmp(字元串1, 字元串2)
測長 strlen(字元數組)
函數
函數:定義、聲明、調用
函數遞歸
猴子吃桃問題。猴子第一天摘下若幹個桃子,當即吃了一半,還不過癮,又多吃了一個。第二天早上又將剩下的桃子吃掉一半,又多吃了一個。以後每天早上都吃了前一天剩下的一半零一個。到第10天早上想再吃時,就只剩一個桃子了。求第一天共摘多少桃子。
1 #include <stdio.h> 2 Int tao(int n) 3 { 4 Int c; 5 If(n==1) C=1; 6 Else c=(tao(n-1)+1)*2; 7 Return(c); 8 } 9 Void main() 10 { 11 Int z; 12 Z=tao(10); 13 Printf(“%d\n”,z); 14 }View Code
數組做參數
選擇法對數組中10個數由小到大排序
1 #include <stdio.h> 2 void sort(int b[],int n) 3 { 4 int i,j,k,t; 5 for(i=0;i<n-1;i++) 6 { 7 k=i; 8 for(j=i+1;j<n;j++) 9 { 10 if(b[i]<b[k]) 11 k=j; 12 t=b[k];b[k]=b[i];b[i]=t; 13 } 14 } 15 } 16 int main () 17 { 18 int a[10],I; 19 for(i=0;i<10;i++) 20 scanf(“%d”,&a[i]); 21 sort(a,10); 22 for(i=0;i<10;i++) 23 printf(“%d”,a[i]); 24 printf(“\n”); 25 }View Code
其它
自動變數(auto) 寄存器變數(register)
靜態局部變數(static)
內部函數 static類型名 函數名(形參表) static int fun (int a,int b)
外部函數 extern int fun (int a,int b)