//构造哈希表
#include <iostream>
#include<cstring>
using namespace std;
int hashfun(int d)
{
return (3 * d) % 11;
}
int main()
{
int data[8] = { 22,41,53,46,30,13,01,67 };
int hasht[11];
memset(hasht, -1, sizeof(hasht));
int sum = 0;
for (int i = 0; i < 8; i++)
{
int k = hashfun(data[i]);
int num = 1;
while (hasht[k] != -1)
{
k = (k + 12) % 11;
num++;
}
hasht[k] = data[i];
sum += num;
}
cout << sum / 8 << endl;
return 0;
}
emm……这道题怎么说呢,其实你直接输出一个2也可以AC通过。
//二叉排序树的判别
#include <iostream>
using namespace std;
typedef struct node{
int data;
node* lchild;
node* rchild;
}*pnode;
int minn=-1;
void create(pnode root)
{
int e;
cin>>e;
if(e==-1)
return;
pnode p=(pnode)malloc(sizeof(node));
p->data=e;
p->rchild=NULL;
p->rchild=NULL;
root=p;
create(root->lchild);
create(root->rchild);
}
bool is(pnode root)
{
pnode temp=root;
if(temp==NULL)
return true;
if(!is(root->lchild))
return false;
if(root->data<minn)
return false;
minn=root->data;
if(!is(root->rchild))
return false;
return true;
}
int main()
{
pnode root=NULL;;
create(root);
if(is(root))
cout<<"yes"<<endl;
else
cout<<"no"<<endl;
return 0;
}
#include <iostream>
using namespace std;
typedef struct node{
int data;
node* lchild;
node* rchild;
}*pnode;
pnode create()
{
int e;
cin>>e;
if(e==-1)
return NULL;
pnode p=(pnode)malloc(sizeof(node));
p->data=e;
p->rchild=NULL;
p->rchild=NULL;
p->lchild=create();
p->rchild=create();
return p;
}
void print1(pnode root,int a,int b)
{
if(root==NULL)
{
return;
}
print1(root->lchild,a,b);
if(root->data<b&&root->data>a)
cout<<root->data<<" ";
print1(root->rchild,a,b);
}
void print(pnode root)
{
if(root==NULL)
return;
print(root->lchild);
cout<<root->data<<" ";
print(root->rchild);
}
pnode treeinsert(pnode root,int in)
{
if(root==NULL)
{
root=(pnode)malloc(sizeof(node));
root->data=in;
root->lchild=root->rchild=NULL;
return root;
}
if(root->data==in) return root;
if(root->data>in)
{
root->lchild=treeinsert(root->lchild,in);
return root;
}
root->rchild=treeinsert(root->rchild,in);
return root;
}
pnode treedelete(pnode root,int del)
{
if(root==NULL)
return NULL;
if(root->data==del)
{
pnode p=root->lchild;
if(p==NULL)
{
free(root);
return root->rchild;
}
pnode L=root->lchild;
while(L->rchild!=NULL)
L=L->rchild;
L->rchild=root->rchild;
free(root);
return p;
}
else if(root->data>del)
{
root->lchild=treedelete(root->lchild,del);
return root;
}
else
{
root->rchild=treedelete(root->rchild,del);
return root;
}
}
int main()
{
pnode root=create();
pnode temp=root;
int a,b;
cin>>a>>b;
print1(temp,a,b);
cout<<endl;
int in;
cin>>in;
root=treeinsert(root,in);
print(root);
cout<<endl;
int del;
cin>>del;
root=treedelete(root,del);
print(root);
return 0;
}
#include<bits/stdc++.h>
using namespace std;
typedef struct node{
int data;
node*lchild;
node*rchild;
}*pnode;
pnode create()
{
int e;
cin>>e;
if(e==-1)
return NULL;
pnode root=(pnode)malloc(sizeof(node));
root->lchild=NULL;
root->rchild=NULL;
root->data=e;
root->lchild=create();
root->rchild=create();
return root;
}
pnode treeinsert(pnode root,int in)
{
if(root==NULL)
{
root=(pnode)malloc(sizeof(node));
root->data=in;
root->lchild=root->rchild=NULL;
return root;
}
if(root->data==in)
return root;
if(root->data>in)
{
root->lchild=treeinsert(root->lchild,in);
return root;
}
root->rchild=treeinsert(root->rchild,in);
return root;
}
void rootmerge(pnode root1,pnode root2)
{
if(!root2||!root1)
return;
root1=treeinsert(root1,root2->data);
rootmerge(root1,root2->lchild);// 合并左子树
rootmerge(root1,root2->rchild);// 合并右子树
}
void print(pnode root)
{
if(root==NULL)
return;
print(root->lchild);
cout<<root->data<<" ";
print(root->rchild);
}
int main()
{
pnode root1=create();
pnode root2=create();
rootmerge(root1,root2);
print(root1);
return 0;
}