请用c++和数据结构算法编写以下程序 c/c++编写算法(3)-数据结构&算法
\u8bf7\u7528c++\u548c\u6570\u636e\u7ed3\u6784\u7b97\u6cd5\u7f16\u5199\u4ee5\u4e0b\u8981\u6c42\u7684\u7a0b\u5e8f#include
#include
class set
{
typedef struct Node
{
int data;
Node* next;
}Node;
private:
Node *head;
int length;
public:
set()
{
head=(Node*)malloc(sizeof(Node));
head->next=NULL;
length=0;
}
~set()
{
Node *cur=head->next,*tmp;
while(cur)
{
tmp=cur;
cur=cur->next;
free(tmp);
}
}
set& operator=(const set& X)
{
Node* cur=X.head;
while(cur->next)
{
append(cur->next->data);
cur=cur->next;
}
length=X.length;
return *this;
}
void append(int x)
{
Node *cur=head,*tmp;
while(cur->next)
{
if(cur->next->data==x)
return ;
cur=cur->next;
}
tmp=new Node;
tmp->data=x;
tmp->next=NULL;
cur->next=tmp;
length++;
}
bool find(int x)const
{
Node *cur=head;
while(cur->next)
{
if(cur->next->data==x)
return true;
cur=cur->next;
}
return false;
}
int getLength()
{
return length;
}
friend void bing(const set& A,const set& B,set& C)
{
Node *cur=A.head;
while(cur->next)
{
C.append(cur->next->data);
cur=cur->next;
}
cur=B.head;
while(cur->next)
{
C.append(cur->next->data);
cur=cur->next;
}
}
friend void jiao(const set& A,const set& B,set& C)
{
Node *cur=A.head;
while(cur->next)
{
if(B.find(cur->next->data))
C.append(cur->next->data);
cur=cur->next;
}
}
friend void cha(const set& A,const set& B,set& C)
{
Node *cur=A.head;
while(cur->next)
{
if(!B.find(cur->next->data))
C.append(cur->next->data);
cur=cur->next;
}
}
void print()
{
Node *cur=head;
while(cur->next)
{
printf("%d ",cur->next->data);
cur=cur->next;
}
putchar('\n');
}
};
main()
{
set A,B,C,D,E;
A.append(1);
A.append(1);
A.append(2);
A.append(3);
B.append(4);
B.append(2);
B.append(5);
jiao(A,B,C);//C=A\u4ea4B
C.print();
cha(A,B,D); //D=A-B
D.print();
bing(A,B,E); //E=A\u5e76B
E.print();
}
unsigned long x(unsigned long I)
{
unsigned long ret = 1;
for(int i = 1;i<=I;i++)
{
ret = ret * i;
if (ret > maxint) throw;
}
}
void main()
{
unsigned long p;
unsigned long *a = new unsigned long[p];
std::cin>>p;
try
{
unsigned long r = x(p)*2*p;
a[p - 1] = r;
} catch(...)
{
std::cout<"number is too large"<<std::endl;
}
delete [] a;
}
#include<iostream.h>
#include<time.h>
#include<stdlib.h>
#include<malloc.h>
typedef struct Bank{
int customer ;
struct Bank *front,*next,*base,*rear;
}Bank,*Num;
int InitQueue(Num &Queue)
{
Queue=(Num)malloc(sizeof(Bank));
Queue->front=Queue;
Queue->base=Queue;
Queue->rear=Queue;
Queue->next=Queue;
return 1;
}
int InserQueue(Num &Queue,int customer)
{
Num L,P;
L=(Num)malloc(sizeof(Bank));
P=Queue->rear;
P->next=L;
P=P->next;
P->customer=customer;
P->next=Queue->base;
Queue->rear=P;
return 1;
}
int PopQueue(Num &Queue,int &customer)
{
Num p;
p=Queue->front->next;
if(p!=Queue->base)
{
customer=p->customer;
Queue->front->next=p->next;
free(p);return 1;
}else return 0;
}
int LengthOfQueue(Num &Queue)
{
Num p;
int n=0;
p=Queue->front->next;
while(p!=Queue->base)
{
n+=1;
p=p->next;
}
return n;
}
int main()
{
Num Sa[4];
srand(time(0));
int customer,i,j,j1,k,h=0,time=0;
int k1,k2,k3,k4,time1=0,time2=0,time3=0,time4=0;
InitQueue(Sa[0]);
InitQueue(Sa[1]);
InitQueue(Sa[2]);
InitQueue(Sa[3]);
cin>>customer;
for(i=1;i<=customer;i++)
{ //time=rand()%21+10;
h=0;
if(i!=1){
for(j=0;j<=3;j++)
{
for(j1=0;j1<=3;j1++)
{if(LengthOfQueue(Sa[j1])<LengthOfQueue(Sa[j]))h=j1;}
}
}
InserQueue(Sa[h],i);
}
cout<<LengthOfQueue(Sa[0])<<" "<<LengthOfQueue(Sa[1])<<" "
<<LengthOfQueue(Sa[2])<<" "<<LengthOfQueue(Sa[3])<<" "<<endl;
k=LengthOfQueue(Sa[h]);
k1=LengthOfQueue(Sa[0]);
k2=LengthOfQueue(Sa[1]);
k3=LengthOfQueue(Sa[2]);
k4=LengthOfQueue(Sa[3]);
for(i=1;i<=k;i++)
{
PopQueue(Sa[h],customer);
time+=rand()%6+5;
}
for(i=1;i<=k1;i++)
{
if(h==0){time1=time;break;}
PopQueue(Sa[0],customer);
time1+=rand()%6+5;
}
for(i=1;i<=k2;i++)
{if(h==1){time2=time;break;}
PopQueue(Sa[1],customer);
time2+=rand()%6+5;
}
for(i=1;i<=k3;i++)
{if(h==2){time3=time;break;}
PopQueue(Sa[2],customer);
time3+=rand()%6+5;
}
for(i=1;i<=k4;i++)
{if(h==3){time4=time;break;}
PopQueue(Sa[3],customer);
time4+=rand()%6+5;
}
cout<<time<<endl;
cout<<h<<endl;
cout<<k<<endl;
cout<<time1<<" "<<time2<<" "<<time3<<" "<<time4<<" ";
time=(time1+time2+time3+time4)/customer;
cout<<time;
}
虽然我也学了一学期C,但基本上等于没学,有心无力,看分却拿不到。。。还是到专门的vc论坛求救吧 http://bbs.bc-cn.net/ 百度搜一下,很多的
也是求作业的?
绛旓細///*鏄剧ず鏁版嵁*/// /// void show(link l){ link p; int j;p=l;j=0;cout<<"閾捐〃鐨勫间负锛歕n";while(p->next){ cout<next->date<<endl;p=p->next;} } /// /// ///閿姣侀摼琛/// /// void destorylinst(link &L){ while(L)...
绛旓細while(p->next!=NULL){ n++;p=p->next;} return(n);} void DispList(LinkList *L)//杈撳嚭绾挎ц〃 { LinkList *p=L->next;while(p!=NULL){ printf("%c",p->data);p=p->next;} } int GetElem(LinkList *L,int i,ElemType &e)//姹傜嚎鎬ц〃涓煇涓鏁版嵁鍏冪礌鍊 { int j=0;...
绛旓細鍒欎綋鐜颁簡浣跨敤鏁扮粍鏉ュ畬鎴愰摼琛ㄨ瀹屾垚鐨勫姛鑳,铏界劧褰㈠紡涓婂畬鍏ㄤ笉鐩稿悓,浣嗗嵈姹傚嚭浜 鐩稿悓鐨勭粨鏋.鏈夊紓鏇插悓宸ヤ箣濡.鎬讳箣鎴戜釜浜鸿涓烘槸鏁扮粍涓潪甯哥粡鍏哥殑绠楁硶浜.甯屾湜鏈 浜哄啓鐨勪唬鐮佷笉浼氬彨澶у鍟愰獋!3.绋嬪簭婧愪唬鐮侊細include <stdio.h> define N 50 define S 3 void main(){ int a[N];int i,k;int sum=N;...
绛旓細define QUEUE_MAX_SIZE 30 ifndef elemType typedef char elemType;endif /***/ /* 浠ヤ笅鏄叧浜庝簩鍙夋爲鎿嶄綔鐨11涓畝鍗绠楁硶 */ /***/ struct BTreeNode{ elemType data;struct BTreeNode *left;struct BTreeNode *right;
绛旓細define MAXSIZE 100 define OK 1 define ERROR 0 define ElemType char typedef struct Node {ElemType data;struct Node *next;}Node, *LinkList;void InitList(LinkList *L){*L=(LinkList)malloc(sizeof(Node));(*L)->next=NULL;} void CreatFromHead(LinkList L){Node *s;char c;int ...
绛旓細printf("%c\t",q->data[f]);f++; i++;if(i%5==0)printf("\n");} printf("%c\n",q->data[f]);} void main(){ SqQueue *q;ElemType e;InitQueue(q);if(QueueEmpty(q))printf("杩欎釜闃熷垪鏄┖鐨勶紒\n");else printf("杩欎釜闃熷垪涓嶆槸绌虹殑锛乗n");enQueue(q,'a'); enQueue(...
绛旓細2.1 鍩烘湰姒傚康涓庢娊璞鏁版嵁绫诲瀷2.1.1 绾挎ц〃瀹氫箟鍜岀壒寰併2.1.2 鐩稿叧鎶借薄鏁版嵁绫诲瀷鐨勮繍鐢ㄣ2.2 椤哄簭琛ㄧず涓庨摼鎺ヨ〃绀 鍒嗗埆鎺㈣浜嗗瓨鍌缁撴瀯鍜岃繍绠楀疄鐜帮紝浠ュ強涓嶅悓琛ㄧず鏂瑰紡鐨勪紭鍔e垎鏋愩2.2.4 闈㈠绌洪棿鎵╁睍锛岄『搴忚〃濡備綍璋冩暣銆2.3.4 鍗曢摼琛ㄧ殑鏀硅繘鍜屾墿灞曠瓥鐣ャ...锛堜互姝ょ被鎺紝鍚庣画閮ㄥ垎缁х画绫讳技缁撴瀯锛屾瘡閮ㄥ垎閮...
绛旓細T->data=n;//缁撶偣鏁版嵁鍩熻祴鍊 create(T->l);//鍒涘缓宸﹀瓙鏍 create(T->r);//鍒涘缓鍙冲瓙鏍 } } void pro(link T)//鍏堝簭閬嶅巻浜屽弶鏍 { if(T){ printf("%d ",T->data);//杈撳嚭鎵鏈夋垚缁 if(T->data>=90)score[0]++;//璁板綍浼樼浜烘暟 if(T->data<60)score[1]++;//璁板綍涓嶅強鏍肩殑浜烘暟 ...
绛旓細//鏉ヨ嚜涓涓彍楦熺殑鍥炵瓟锛屽啓鐨勪笉濂斤紝鏈夐棶棰樺彲浠ユ寚鍑哄摝銆俰nclude<stdio.h> void main(){ int i,N;int queue_1[50]={0},queue_2[50]={0},front1=0,front2=0,rear1=0,rear2=0;scanf("%d",&N);for(i=1;i<=N;i++){ if(i%2==0&&rear1!=front1){ // 濡傛灉鏃堕棿鏄2T 姹借溅...
绛旓細瀹為獙涓 include<stdio.h>#include<stdlib.h>#define N 10void shellpass(int a[], int n, int d){int i,j,temp;for(i=d;i<n;i++){if(a[i]=0&&temp<a[j];j-=d)a[j+d]=a[j];a[j+d]=temp;}}}void shellsort(int a[], int n, int delta[], int t){int i, j...