|
//靜態順序表的各種操作 #include<stdio.h> #define MaxSize 10 void insertElem(int Sqlist[],int *len,int i,int x)//表首地址、表長、插入元素位置、待插入的元素值 { int t,temp; if(*len==MaxSize||i<1||i>*len+1) { printf("This insert is illegal\n"); return; } for(t=*len-1;t>=i-1;t--) Sqlist[t+1]=Sqlist[t];//i-1后的元素都向后移一位 Sqlist[i-1]=x;//插入元素 *len=*len+1;//表長加1 }
void DelElem(int Sqlist[],int *len,int i)//向順序表中刪除元素 { int j; if(i<1||i>*len) { printf("This insert is illegal"); return; } for(j=i;j<=*len-1;j++) Sqlist[j-1]=Sqlist[j];//將第i個元素之后的元素前移,覆蓋即刪除 *len=*len-1; }
int main() { int Sqlist[MaxSize]; int len; int i; for(i=0;i<6;i++) scanf("%d",&Sqlist[i]); len=6; for(i=0;i<len;i++) printf("%d ",Sqlist[i]); printf("\nThe spare length is %d\n",MaxSize-len);//顯示表中剩余空間 insertElem(Sqlist,&len,3,0);//在表中第3個位置插入整數0 for(i=0;i<len;i++) printf("%d ",Sqlist[i]); printf("\nThe spare length is %d\n",MaxSize-len); insertElem(Sqlist,&len,11,0); DelElem(Sqlist,&len,6); for(i=0;i<len;i++) printf("%d ",Sqlist[i]); printf("\nThe spare length is %d\n",MaxSize-len); return 0; }
#include<stdio.h> //使用數組創建隊列 #define MAXQUEUE 10 //隊列的最大容量 int queue[MAXQUEUE]; //隊列的數組聲明 int front=-1;//隊列的隊頭 int rear=-1; //隊列的隊尾 //隊列數據的存入 int enqueue(int value) { if(rear>=MAXQUEUE) return -1; //檢查隊列是否全滿 rear++; //隊尾指針往前移 queue[rear]=value; //存入隊列 } //隊列數據的取出,取出時隊頭指針往后移 int dequeue() { if(front==rear) //檢查隊列是否是空 return -1; //無法取出 front++; //隊頭指針往前移(即向隊尾指針方向移) return queue[front]; //隊列取出 } //主程序:模擬隊列操作 //輸出輸入的內容都會存儲在數組中,接著輸出數組內容來看其結果 void main() { int input[100];//存儲輸入的元素 int output[100];//存儲取出的元素 int select; int i_count=0; //數組input的索引 int o_count=0; int loop=1; int i,temp; while(loop) { printf("[1]輸入 [2]取出 [3]列出全部內容 ==>"); scanf("%d",&select); switch(select) { case 1:printf("請輸入存入隊列的值(%d)==> ",i_count+1); scanf("%d",&temp); if(enqueue(temp) == -1) printf("隊列全滿.\n"); else input[i_count++]=temp; break; case 2:if((temp=dequeue())==-1) printf("隊列是空的.\n"); else { printf("取出隊列元素:%d\n",temp); output[o_count++]=temp; } break; case 3: loop=0; break; } } printf("輸入隊列的元素:"); for(i=0;i<i_count;i++) printf("[%d]",input[i]); printf("\n取出隊列的元素: "); for(i=0;i<o_count;i++) printf("[%d]",output[i]); printf("\n剩下隊列的元素:"); while((temp=dequeue())!=-1) printf("[%d]",temp); printf("\n"); }
//應用棧來走迷宮 #include<stdio.h> #include<stdlib.h> struct stack_node { int x;//路徑坐標x int y;//路徑坐標y struct stack_node *next;//指向下一結點 }; typedef struct stack_node stack_list; typedef stack_list *link; link path=NULL;//路徑棧指針 //棧數據的存入 link push(link stack,int x,int y) { link new_node;//新結點指針 //分配結點內存 new_node=(link)malloc(sizeof(stack_list)); if(!new_node) { printf("內存分配失敗!\n"); return NULL; } new_node->x=x; //存入路徑坐標x new_node->y=y; //存入路徑坐標y new_node->next=stack;//新結點指向原開始 stack=new_node; //新結點成為棧開始 return stack; } //棧數據的取出 link pop(link stack,int *x,int *y) { link top;//指向棧頂端 if(stack!=NULL) { top=stack; stack=stack->next;//棧指針指向下結點 *x=stack->x;//取出路徑坐標x *y=stack->y;//取出路徑坐標y free(top); return stack; } else *x=-1; } //主程序:用回溯的方法在數組迷宮找出口 //數字0:表示是可以走的路 //數字1:表示是墻壁,不可走的路 //數字2:表示是走過的路 //數字3:表示是回溯的路 void main() { int maze[7][10]={ 1,1,1,1,1,1,1,1,1,1, 1,0,1,0,1,0,0,0,0,1, 1,0,1,0,1,0,1,1,0,1, 1,0,1,0,1,1,1,0,0,1, 1,0,1,0,0,0,0,0,1,1, 1,0,0,0,1,1,1,0,0,1, 1,1,1,1,1,1,1,1,1,1,}; int i,j; int x=5;//迷宮入口坐標 int y=8; while(x!=1||y!=1)//是否是迷宮出口 { maze[x][y]=2;//標示走過的路 if(maze[x-1][y]<=0) //往上方走 { x=x-1; path=push(path,x,y);//存入路徑 } else if(maze[x+1][y]<=0)//往下方走 { x=x+1; path=push(path,x,y); } else if(maze[x][y-1]<=0)//往左方走 { y=y-1; path=push(path,x,y); } else if(maze[x][y+1]<=0)//往右方走 { y=y+1; path=push(path,x,y); } else { maze[x][y]=3; path=pop(path,&x,&y);//退回一步 } } maze[x][y]=2; printf("迷宮的路徑如下圖所示:\n"); for(i=1;i<6;i++)//輸出迷宮圖形 { for(j=1;j<9;j++) printf("%d",maze[i][j]);//輸出各坐標 printf("\n"); } }
//利用鏈表創建棧 #include<time.h> #include<stdlib.h> #include<stdio.h> struct stack_node { int data; struct stack_node*next; }; typedef struct stack_node stack_list; typedef stack_list *link; link stack=NULL; //棧數據的存入 int push(int value) { link new_node; new_node=(link)malloc(sizeof(stack_list)); if(!new_node) { printf("內存分配失敗!\n"); return -1; } new_node->data=value; new_node->next=stack; stack=new_node; } //棧數據的取出 int pop() { link top; int temp; if(stack!=NULL) { top=stack; stack=stack->next; temp=top->data; free(top); return temp; } 6 else return -1; } int empty() { if(stack == NULL) return 1; else return 0; } void main() { int card[52]; int pos; int i,temp; for(i=0;i<52;i++) card[i]=0; i=0; while(i!=52) { pos=rand()%52; if(card[pos] == 0) { push(pos); card[pos]=1; i++; } } printf(" 1 2 3 4\n"); printf("======================\n"); for(i=0;i<5;i++) { temp=pop(); printf("[%c%2d]",temp/13+3,temp%13+1); temp=pop(); printf("[%c%2d]",temp/13+3,temp%13+1); temp=pop(); printf("[%c%2d]",temp/13+3,temp%13+1); temp=pop(); printf("[%c%2d]",temp/13+3,temp%13+1); printf("\n"); } }
#include<stdlib.h> #include<time.h> #include<stdio.h> #define MAXSTACK 100//棧的最大容量 int stack[MAXSTACK];//棧的數組聲明 int top =-1; //棧的頂端 //棧的數據存入 int push(int value) { if(top>=MAXSTACK)//是否超過容量 { printf("棧的內容全滿\n"); return -1; } top++; stack[top]=value;//棧指針加1,存入棧 } //棧數據的取出 int pop() { int temp; if(top<0) { printf("棧內容是空的\n"); return -1; } temp = stack[top]; top--; return temp; } //檢查棧是否是空的 int empty() { if(top == -1) return 1; else return 0; } //主程序:運用empty()檢查牌是否發完 //紅心:數組0~12,方塊:數組13~25,梅花:數組26~38,黑桃:數組39~51 void main() { int card[52]; int pos; int i,temp; for(i=0;i<52;i++) card[i]=0; i=0; while(i!=5)//洗五張牌循環 { pos=rand()%52;//隨機數取值0~51 if(card[pos] == 0) //是否是未洗牌 { push(pos);//存此張牌進棧 card[pos]=1;//設置此張牌洗過 i++;//下一張牌 } } while(!empty())//發完棧全部的牌 { temp=pop(); //取出棧數據 printf("[%c%3d]",temp/13+3,temp%13+1); } printf("\n"); }
#include<stdlib.h> #include<stdio.h> struct dlist //雙向鏈表結構聲明 { int data; struct dlist *front;//指向下一結點的指針 struct dlist *back; //指向前一結點的指針 }; typedef struct dlist dnode;//雙向鏈表新類型 typedef dnode *dlink;//雙向鏈表指針新類型 void printdlist(dlink head) { while (head!=NULL) { printf("[%d]",head->data); head=head->front; } printf("\n"); } //雙向鏈表結點的插入 dlink insertnode(dlink head,dlink ptr,int value) { dlink new_node; //創建新結點,分配結點內存 new_node=(dlink)malloc(sizeof(dnode)); if(!new_node) return NULL; new_node->data=value; new_node->front=NULL; new_node->back=NULL; if(head == NULL) return new_node; if(ptr == NULL) { //第一種情況:插在第一個結點之前,成為鏈表開始 new_node->front=head; head->back=new_node; head=new_node; } else { if(ptr->front == NULL) { //第二種情況:插在鏈表的最后 ptr->front=new_node;//最后結點指向新結點 new_node->back=ptr;//新結點指回最后結點 } else { //第三種情況:插入結點至鏈表中間結點內 ptr->front->back=new_node;//下一結點指回新結點 new_node->front=ptr->front;//新結點指向下一結點 new_node->back=ptr; //新結點指回插入結點 ptr->front=new_node; //插入結點指向新結點 } } return head;//返回鏈表起始指針 } //主程序:使用插入結點的方式來創建鏈表,完成后將鏈表內容輸出 void main() { dlink head = NULL;//循環鏈表指針 dlink tail = NULL;//鏈表最后的指針 int list[6]={1,2,3,4,5,6}; int i; head = insertnode(head,head,list[0]); printdlist(head); tail = head;//保留鏈表最后指針 //第一種情況:插在第一個結點之前 head=insertnode(head,NULL,list[1]); printdlist(head); //第二種情況:插在鏈表的最后 head = insertnode(head,tail,list[2]); printdlist(head); for(i=3;i<6;i++) { //第三種情況:插入結點至鏈表中間結點內 head = insertnode(head,head,list[i]); printdlist(head); } }
#include<stdio.h> #include<stdlib.h> //雙向鏈表結構 struct dlist { int data; struct dlist *front;//指向下一結點的指針 struct dlist *back;//指向前一結點的指針 }; typedef struct dlist dnode; typedef dnode *dlink; dlink createdlist(int *array,int len) { dlink head; dlink before; dlink new_node; int i; //創建第一個結點,分配指針內存 head=(dlink)malloc(sizeof(dnode)); if(!head) return NULL; head->data=array[0]; head->front=NULL; head->back=NULL; before=head;//指向第一個結點 for(i=1;i<len;i++)//用循環創建其他結點 { new_node=(dlink)malloc(sizeof(dnode)); if(!new_node) return NULL; new_node->data=array[i]; new_node->front=NULL; new_node->back=before;//將新結點指向前結點 before->front=new_node;//將前結點指向新結點,構成循環鏈表 before=new_node;//新結點成為前結點 } return head; } //雙向鏈表的輸出 void printdlist(dlink head,dlink now) { while(head!=NULL) //鏈表循環遍歷 { if(head == now) printf("#%d#",head->data); else printf("[%d]",head->data); head=head->front; } printf("\n"); } void main() { dlink head; dlink now=NULL; int list[6]={1,2,3,4,5,6}; int select; head=createdlist(list,6); if(head==NULL) { printf("內存分配失敗!\n"); exit(1); } now=head; while(1) { printf("鏈表內容是:"); printdlist(head,now); printf("[1]往下移動 [2]往回移動 [3]離開 ==> "); scanf("%d",&select); switch(select) { case 1: if(now->front!=NULL) now=now->front; break; case 2: if(now->back!=NULL) now=now->back; break; case 3: exit(1); } } }
/*含頭結點的循環鏈表的多項式*/ # include<stdio.h> # include<stdlib.h> struct plist /* 多項式結構聲明*/ { int coef; /*多項式的系數*/ int exp; /*多項式的指數*/ struct plist *next; /*指向下一結點的指針*/ }; typedef struct plist pnode; /* 多項式新類型*/ typedef pnode *plink; /* 多項式指針新類型*/ /*鏈表輸出*/ void printpoly(plink poly) { plink ptr; ptr=poly->next; /*指向鏈表開始*/ while(poly!=ptr) /*鏈表遍歷循環*/ { /*輸出結點數據*/ printf("%dX^%d",ptr->coef,ptr->exp); ptr=ptr->next; /* 指向下一結點*/ if(poly!=ptr) printf("+"); } printf("\n"); /* 換行*/ } /*使用數組值創建多項式*/ plink createpoly(int *array,int len) { plink head; /*循環鏈表的指針*/ plink before; /*前一結點的指針*/ plink new_node; /*新結點的指針*/ int i; /*創建頭結點,分配結點內存*/ head=(plink)malloc(sizeof(pnode)); if(!head) return NULL; /*檢查內存指針*/ head->exp=-1; /*創建結點內容*/ before=head; /*指向第一個結點*/ for(i=len-1;i>=0;i--) /*用循環創建其他結點*/ if(array[i]!=0) { /*分配結點內存*/ new_node=(plink)malloc(sizeof(pnode)); if(!new_node) return NULL; /*檢查內存指針*/ new_node->coef=array[i]; /*創建系數內容*/ new_node->exp=i; /*創建指數內容*/ new_node->next=NULL; /*設置指針初值*/ before->next=new_node; /*將前結點指向新結點*/ before=new_node; /*新結點成為前結點*/ } new_node->next=head; /*創建環狀鏈接*/ return head; /*返回鏈表起始指針*/ } /*多項式相加*/ plink polyadd(plink poly1,plink poly2) { plink head1; /*多項式1的開始*/ plink head2; /*多項式2的開始*/ plink result; /*多項式的結果*/ plink before; /*前一結點的指針*/ plink new_node; /*新結點的指針*/ head1=poly1->next; /*指向多項式1的開始*/ head2=poly2->next; /*指向多項式2的開始*/ /*創建頭結點且分配結點內存*/ result=(plink)malloc(sizeof(pnode)); if(!result) return NULL; /*檢查內存指針*/ result->exp=-1; /*創建結點內容*/ before=result; /*指向第一個結點*/ while(poly1!=head1||poly2!=head2) { /*分配結點內存*/ new_node=(plink)malloc(sizeof(pnode)); if(!new_node) return NULL; /*檢查內存指針*/ if(head1->exp<head2->exp) /*多項式2的指數大*/ { new_node->coef=head2->coef; /*設置系數*/ new_node->exp=head2->exp; /*設置指數*/ head2=head2->next; /*指向下一結點*/ } else if(head1->exp>head2->exp) /*多項式1的指數大*/ { new_node->coef=head1->coef; /*設置系數*/ new_node->exp=head1->exp; /*設置指數*/ head1=head1->next; /*指向下一結點*/ } else /*多項式的指數相等*/ { /*系數相加*/ new_node->coef=head1->coef+head2->coef; new_node->exp=head1->exp; /*設置指數*/ head1=head1->next; /* 指向下一結點*/ head2=head2->next; /* 指向下一結點*/ } before->next=new_node; /*將前一結點指向新結點*/ before=new_node; /*新結點成為前結點*/ } new_node->next=result; /*創建環狀鏈接*/ return result; /*返回多項式的指針*/ } void main() { plink poly1; /*多項式1的指針*/ plink poly2; /*多項式2的指針*/ plink result; /*多項式結果的指針*/ int list1[6]={4,0,3,0,7,0}; /*數組1的內容*/ int list2[6]={9,7,1,0,5,6}; /*數組2的內容*/ poly1=createpoly(list1,6); /*創建多項式1*/ printf("the content1:"); printpoly(poly1); /*輸出多項式1*/ poly2=createpoly(list2,6); /*創建多項式2*/ printf("the content2:"); printpoly(poly2); /*輸出多項式2*/ result=polyadd(poly1,poly2); /*多項式相加*/ printf("the add:"); printpoly(result); /*輸出多項式結果*/ }
- 描述
輝輝、姍姍和佳佳是好朋友,他們一起參加了在湖南長沙長郡中學舉辦的第二十一屆全國青少年信息學奧林匹克競賽(NOI2004)。他們很早就來到了長沙,可是報名還沒有開始。怎么辦呢?他們決定分頭出去玩一天,晚上回到宿舍以后給大家說說自己這一天做了什么有意義的事情。 你一定想不到輝輝干嘛去了——他睡了一天。他想:“比賽前幾天老是寫程序到深夜,頭暈暈的……沒關系,好好睡一覺,然后我會精神抖擻。醒了之后,我要做有意義的事情。”這一睡可不得了,輝輝從早上a點b分c秒一直睡到了下午d點e分f秒。他睡了多少秒鐘呢?
- 輸入
測試數據包含多組輸入。 每組輸入一行,僅包含六個非負整數a, b, c, d, e, f,以空格分離。1<=a, d<=11, 0<=b, c, e, f<=59。如輸入6 5 4 3 2 1表示輝輝從06:05:04睡到15:02:01。 輸入以六個零結尾。 - 輸出
每組輸出一行,僅包含一個整數s,即輝輝睡覺的總秒數。 - 樣例輸入
6 5 4 3 2 1 0 0 0 0 0 0 - 樣例輸出
32217
注意秒、分、時之間當不同大小時要進行適當的借1運算 源代碼如下(感覺挺丑陋的):
#include<iostream> using namespace std; int main() { int a,b,c,d,e,f; int h,m,s,sum; while(cin>>a>>b>>c>>d>>e>>f) { if(a==0&&b==0&&c==0&&d==0&&e==0&&f==0) break;
if(b>e)//醒來的時刻的分鐘數大于睡時的分鐘數 { h=d-a+11;//小時減1 if(c>f) { s=f-c+60; m=e-b+59; } else { s=f-c; m=e-b+60; } } else if(b<e) { h=d-a+12; if(c>f) { s=f-c+60; m=e-b-1; } else { s=f-c; m=e-b; } } else if(b==e) { if(c>f) { h=d-a+11; m=e-b+59; s=f-c+60; } else { h=d-a+12; m=e-b; s=f-c; } }
sum=h*3600+m*60+s;//全部換算成統一單位——秒 cout<<sum<<endl; } return 0; }
為防止在對數組動態賦值時發生數組越界,C++提供了一種能夠解決此問題的方法——重載運算符[]。示例程序: #include<iostream> class CArray { public: CArray(int l) { length=l; Buff=new char[length]; } ~CArray(){delete Buff;} int GetLength(){return length;} char& operator [](int i); private: int length; char *Buff; };
char & CArray::operator[](int i) { static char ch=0; if(i<length && i>=0) return Buff[i]; else { cout<<"\nIndex out of range."; return ch; } }
void main() { int cnt; CArray string1(6); char *string2="string"; for(cnt=0;cnt<string1.GetLength();cnt++) string1[cnt]=string2[cnt]; cout<<"\n"; for(cnt=0;cnt<string1.GetLength();cnt++) cout<<string1[cnt]; cout<<"\n"; cout<<string1.GetLength()<<endl; }
在重載下標運算符函數時注意: 1)該函數只帶一個參數,不可帶多個參數。 2)得重載為友元函數,必須是非static類的成員函數。
|