|
編寫一個返回循環鏈表中節點數的函數 實現代碼如下: #include<stdio.h> #include<stdlib.h> typedef struct node *link; struct node{ int item; link next; };
int node_number(link p,int n) { int count=0,i; for(i=0;i<n-1;i++) { p=p->next; } while(p->item) { p->item=0; p=p->next; count++; } return count; }
int main() { int i,N; link t=(link)malloc(sizeof(node)); t->item=1; t->next=t; link x=t; for(i=2;i<=10;i++) { x = (x->next= (link)malloc(sizeof(node))); x->item=i; x->next=t; } printf("Please input the order of node: "); scanf("%d",&N); printf("total number of nodes is: %d\n",node_number(t,N)); return 0; }
假設有N個人決定選出一個領導人,方法如下:所有人排成一個圓圈,按順序數數,每隔第M個人出局,此時他兩邊的人靠攏重新形成圓圈。問題是找出哪一個人將會是最后剩下的那個人。我們希望打印出所有人的出局順序和最后選出的領導人是哪一位。 這個問題稱為約瑟夫問題,可以利用鏈表解決。 代碼如下: //約瑟夫問題 #include<stdio.h> #include<stdlib.h> typedef struct node *link; struct node { int item; link next; }; //定義結點 int main() { int i,N,M; printf("Input N and M: "); //N表示共有N個人,M表示每隔第M個人要出局 scanf("%d%d",&N,&M); link t = (link)malloc(sizeof(node)); //新建結點t link x=t; t->item = 1; t->next=t; //創建一個代表1號的單個節點的循環鏈表 for(i=2;i<=N;i++) { x=(x->next= (link)malloc(sizeof(node)));//將2~N號按序插到之前創建的單個節點的循環鏈表中 x->item=i; x->next=t; } while(x!= x->next) //如果不是最后一個節點,因為是循環鏈表,所以x!=x->next { for(i=1;i<M;i++) //則順著鏈表向前遍歷,數出M-1個元素 x=x->next; printf("%d ",x->next->item); x->next = x->next->next; //刪除第M個元素 N--; //節點數減1 } printf("\n%d\n",x->item); //最后打印出最后一個節點 return 0; }
編寫程序,對于N個隨機產生的單位正方形中的點,統計可以被長度小于d的直線連結的點對數。 程序如下: typedef struct{ float x; float y; } point; //定義點的數據類型 float distance(point,point); //兩點之間距離函數 float distance(point a,point b) { float dx= a.x - b.x, dy= a.y - b.y; return sqrt(dx*dx + dy*dy); } //以上為頭文件 Point.h 的內容
#include<stdio.h> #include<stdlib.h> #include<math.h> #include "Point.h" float randFloat() { return 1.0*rand()/RAND_MAX; } //產生隨機數的函數 int main() { float d,N; int i,j,cnt=0; scanf("%f%f",&d,&N); //d為要求兩點之間距離小于的長度,N為測試的點
point *a = (point *)malloc( sizeof(point)*N); //動態生成數據類型為point的數組a
for(i=0;i<N;i++) { a[i].x = randFloat(); a[i].y = randFloat(); } for(i=0;i<N;i++) for(j=i+1;j<N;j++) if(distance(a[i],a[j])<d) //如果兩點之間的距離小于d,那么cnt加1
cnt++; printf("%d edges shorter than %f\n",cnt,d); //輸出有多少條邊的長度小于d return 0; }
//模擬拋硬幣的實驗
#include<stdio.h> #include<stdlib.h> int heads() //返回0或非0值 { return rand() <RAND_MAX/2; }
int main() { int i,j,cnt; int N,M; scanf("%d%d",&N,&M); //拋一枚硬幣N=32次,如此做M=1000次這樣的實驗 int *f=(int *)malloc((N+1)*sizeof(int)); for(j=1;j<=N;j++) //初始化數組全部為0值 f[j]=0; for(i=1;i<M;i++,f[cnt]++) //開始拋硬幣,f[cnt]記錄第cnt次拋硬幣出現正面的次數 for(cnt=1,j=1;j<=N;j++) //開始第一輪共32次的拋硬幣實驗 if(heads()) cnt++; //如果出現正面,即heads()返回值為1,則對應著f[cnt]++,同時cnt++,此處利用數組索引統計正面出現次數,負面數組值始終為0 for(j=1;j<=N;j++) { printf("%2d ",j); for(i=0;i<f[j];i+=10) printf("*"); //正面每出現十次打印一個星號 printf("\n"); } return 0; }
// From < C Programming FAQs > 找出所有小于10000的素數,算法原理請自行google 埃拉托色尼篩法 程序代碼: #define N 10000 #include<stdio.h> int main() { int i,j,a[N]; for(i=2;i<N;i++) a[i]=1; //將數組中的值全部設為1 for(i=2;i<N;i++) if(a[i]) for(j=i;i*j<N;j++) a[i*j]=0; //將索引為2,3,5, 的倍數的數組元素設為0,因為這些數不是素數 for(i=2;i<N;i++) if(a[i]) printf("4%d\n",i); //遍歷打印出找到的素數
printf("\n"); return 0; }
輸入10個學生的成績,編寫一程序對學生的成績按從高到低輸出,要求用鏈表實現。 #include<stdio.h> #include<stdlib.h> struct Stu { int score; struct Stu *next; }; typedef struct Stu Node; int main() { int i; Node *head,*p,*q; head=(Node*)malloc( sizeof(Node)); //創建頭結點 if(head == NULL) { printf("Memory is not enough!"); return 0; } head->next=NULL; for(i=0;i<10;i++) { p=(Node*)malloc( sizeof(Node)); //創建一個新結點p if(p == NULL) { printf("no enough memory!"); return 0; } printf("Input the %dth student's score: ",i+1); scanf("%d",&p->score); //輸入成績
q=head; while(q->next != NULL) //遍歷鏈表
{ if(q->next->score < p->score) //如果發現鏈表中的某個成績比當前輸入成績小,就跳出循環,在其前面插入當前輸入成績 break; q=q->next; //繼續遍歷直到遍歷的成績比當前輸入的成績小
} p->next=q->next; //這是當前成績插入到鏈表中比其小的成績前面的代碼
q->next=p; } p=head->next; while(p !=NULL) { printf("%d ",p->score); p=p->next; } p=head; while(p->next !=NULL) { q=p->next; p->next=q->next; free(q); } free(head); return 0; }
編寫一個函數totsubstrnum(char *str, char *substr) ,它的功能是:統計子字符串substr在字符串str中出現的次數。 思想:len2為子串的長度,設置變量 i =0, 利用strncmp函數將str+i 開始的len2個字符與子串substr進行比較,如果相等,則count加1,此時 i 加 len2,如果不等,則 i 加1,繼續尋找。 代碼測試通過: #include<stdio.h> #include<string.h> int totsubstrnum(char *str, char *substr); int main() { char str[80],substr[80]; printf("Input string: "); gets(str); printf("Input substring: "); gets(substr); printf("count = %d\n",totsubstrnum(str,substr));
return 0; }
int totsubstrnum(char *str, char *substr) { int i=0,count=0,len1,len2; len1=strlen(str); len2=strlen(substr); while(i <= len1-len2) { if(strncmp(str+i,substr,len2) == 0) { count++; i +=len2; } else i++; } return (count); }
編寫一函數strlshif(char *s, int n),其功能是把字符串s中的所有字符左移n個位置,字符串中的前n個字符移到最后。 代碼測試通過: #include<stdio.h>#include<string.h>void strlshif(char *s, int n);void main(){ char str[]="0123456789"; strlshif(str,3); printf("%s\n",str);}void strlshif(char *s, int n){ int i,len; char ch; len=strlen(s); for(i=0;i<n;i++) { ch=s[0]; strncpy(s,s+1,len-1); s[len-1]=ch; }}那么若是不用strncpy函數功能,如何使指定的字符串左移n位? 代碼測試通過,如下:
#include<stdio.h> #include<string.h> int main() { char str[]="0123456789"; char sstr[80]={0}; //使用一數組儲存移動后的字符串 char *p; int c,j; static int i,n; p=&str[0]; printf("input the number: \n"); scanf("%d",&n); //輸入要左移的前n個字符,即將這n個字符移動到最后面 c=n; while( c-- && p++ ); //找到沒有移動過的剩下的全部字符,把它們儲存在數組sstr 中
for(i=0;i<strlen(str)-n;i++) { sstr[i]= *p; p++; } p=&str[0]; //指針指向第一個字符 for(j=i;j<strlen(str);j++) //將要移動的字符一個一個地“接”到數組sstr后面 { sstr[j]= *p; p++; } sstr[j]='\0'; //最后字符串結尾用'\0' printf("%s",sstr); return 0; }
// s 是字符串 startloc 是開始取的位置 len表示取得子串長度 #include<stdio.h> #include<string.h> #include<stdlib.h> void substr(char *s, int startloc,int len) { if((startloc < 0) || (startloc >= strlen(s)) || (len<0)) { printf("input error!"); exit(0); } int i,c=0; char sstr[80]; while(*s !='\0') { if(c!=startloc) { ++c; s++; } else { for(i=0;i<len;i++) { sstr[i]= *s; s++; } sstr[i]='\0'; break; } } printf("%s",sstr); }
int main() { char str[80]; int s,l; printf("Input string: "); gets(str); printf("Start Location: "); scanf("%d",&s); printf("Substring length: "); scanf("%d",&l); substr(str,s,l); return 0; }
類似C語言中的strcat()函數,編程實現mystrcat( char *str, char * destr)的功能并測試 。 代碼測試通過: #include<stdio.h> void mystrcat( char *str,char *destr) { while(*str !='\0') str++; while(*destr !='\0') { *str = *destr; str++; destr++; }
*str = '\0'; }
int main() { char str[30],destr[30]; printf("input string and substring: "); scanf("%s%s",str,destr); mystrcat(str,destr); printf("%s",str); return 0; }
|