算法
桶排序:
#include <iostream>
using namespace std;
int main()
{
int book[1001], i,j,t,n;
for(i=0;i<1001;i++){
book[i]=0;
}
cout<<"please input the datas:";
cin>>n;
for(i=1;i<=n;i++){
cin>>t;
book[t]++;
}
for(i=1000;i>=0;i--)
for(j=1;j<=book[i];j++)
cout<<i<<" ";
system("pause");
return 0;
}
冒泡排序:
#include <iostream>
using namespace std;
int main()
{
int array[6], n, temp, i,j;
cout<<"Please input n: "<<endl;
cin>>n;
cout<<"Please input array: "<<endl;
for(i = 1;i<=n;i++){
cin>>array[i];
}
for(i=1;i<=n-1;i++) {
for(j=1;j<=n-i;j++){
if(array[j]<array[j+1]){
temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
}
}
}
for(i=1;i<=n;i++){
cout<<array[i]<<endl;
}
system("pause");
}
快速排序:
#include <iostream>
using namespace std;
int a[101],n;
void quicksort(int left,int right)
{
int i,j,t,temp;
if(left>right)
return;
temp=a[left]; // 基准数
i=left;
j=right;
while(i!=j)
{
while(a[j]>=temp&&i<j)
j--;
while(a[i]<=temp&&i<j)
i++;
if(i<j)
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
//将基准数归位
a[left]=a[i];
a[i]=temp;
quicksort(left,i-1);
quicksort(i+1, right);
}
int main()
{
int i;
cout<<"please input datas:";
cin>>n;
for(i=0;i<n;i++)
{
cin>>a[i];
}
quicksort(0,n-1);
for(i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
system("pause");
return 0;
}
按顺序输出不重复的数据:
#include <iostream>
using namespace std;
int main()
{
int book[1001], i,j,t,n;
for(i=0;i<1001;i++){
book[i]=0;
}
cout<<"please input the datas:";
cin>>n;
for(i=1;i<=n;i++){
cin>>t;
book[t]=1;
}
for(i=0;i<1000;i++)
if(book[i]==1)
cout<<i<<" ";
system("pause");
return 0;
}
冒泡排序的方法:
#include <iostream>
using namespace std;
int main()
{
int array[101], n, temp, i,j;
cout<<"Please input n: "<<endl;
cin>>n;
cout<<"Please input array: "<<endl;
for(i = 1;i<=n;i++){
cin>>array[i];
}
for(i=1;i<=n-1;i++) {
for(j=1;j<=n-i;j++){
if(array[j]<array[j+1]){
temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
}
}
}
cout<<array[1]<<" ";
for(i=2;i<=n;i++){
if(array[i]!=array[i-1])
cout<<array[i]<<" ";
}
system("pause");
}
qq号码解密:
规则是这样的:首先将第1 个数删除,紧接着将第2 个数放到
这串数的末尾,再将第3 个数删除并将第4 个数放到这串数的末尾,再将第5 个数删除……
直到剩下最后一个数,将最后一个数也删除
#include <iostream>
using namespace std;
struct queue
{
int data[100];
int head;
int tail;
};
int main()
{
struct queue q;
int i;
q.head=1;
q.tail=1;
for(i=1;i<=9;i++)
{
cin>>q.data[q.tail];
q.tail++;
}
while(q.head<q.tail)
{
//打印队首,将队首出列
cout<<q.data[q.head]<<" ";
q.head++;
//新队首添加到队尾
q.data[q.tail]=q.data[q.head];
q.tail++;
//队首出列
q.head++;
}
system("pause");
return 0;
}
栈: 判断回文: xyzyx, xyzzyx:
#include <iostream>
using namespace std;
int main()
{
char a[101],s[101];
int top, i,len,mid,next;
cin>>a;
len=strlen(a);
mid=len/2-1;
top=0;
for(i=0;i<=mid;i++)
{
s[++top]=a[i];
}
if(len%2==0)
next=mid+1;
else
next=mid+2;
for(i=next;i<=len-1;i++)
{
if(a[i]!=s[top])
break;
top--;
}
if(top==0)
cout<<"YES"<<endl;
else
cout<<"NO!"<<endl;
system("pause");
return 0;
}
队列+栈:小猫钓鱼游戏:
#include <iostream>
using namespace std;
struct queue
{
int data[1000];
int head;
int tail;
};
struct stack
{
int data[10];
int top;
};
int main()
{
struct queue q1,q2;
struct stack s;
int book[10];
int i,t;
//初始化队列
q1.head=1; q1.tail=1;
q2.head=1; q2.tail=1;
//初始化栈
s.top=0;
//初始化用来标记的数组,用来标记哪些牌已经在桌上
for(i=1;i<=9;i++)
book[i]=0;
//依次向队列插入6个数
//小哼手上的6张牌
for(i=1;i<=6;i++)
{
cin>>q1.data[q1.tail];
q1.tail++;
}
//小哈手上的6张牌
for(i=1;i<=6;i++)
{
cin>>q2.data[q2.tail];
q2.tail++;
}
while(q1.head<q1.tail && q2.head<q2.tail ) //当队列不为空的时候执行循环
{
t=q1.data[q1.head];//小哼出一张牌
//判断小哼当前打出的牌是否能赢牌
if(book[t]==0) //表明桌上没有牌面为t的牌
{
//小哼此轮没有赢牌
q1.head++; //小哼已经打出一张牌,所以要把打出的牌出队
s.top++;
s.data[s.top]=t; //再把打出的牌放到桌上,即入栈
book[t]=1; //标记桌上现在已经有牌面为t的牌
}
else
{
//小哼此轮可以赢牌
q1.head++;//小哼已经打出一张牌,所以要把打出的牌出队
q1.data[q1.tail]=t;//紧接着把打出的牌放到手中牌的末尾
q1.tail++;
while(s.data[s.top]!=t) //把桌上可以赢得的牌依次放到手中牌的末尾
{
book[s.data[s.top]]=0;//取消标记
q1.data[q1.tail]=s.data[s.top];//依次放入队尾
q1.tail++;
s.top--; //栈中少了一张牌,所以栈顶要减1
}
}
t=q2.data[q2.head]; //小哈出一张牌
//判断小哈当前打出的牌是否能赢牌
if(book[t]==0) //表明桌上没有牌面为t的牌
{
//小哈此轮没有赢牌
q2.head++; //小哈已经打出一张牌,所以要把打出的牌出队
s.top++;
s.data[s.top]=t; //再把打出的牌放到桌上,即入栈
book[t]=1; //标记桌上现在已经有牌面为t的牌
}
else
{
//小哈此轮可以赢牌
q2.head++;//小哈已经打出一张牌,所以要把打出的牌出队
q2.data[q2.tail]=t;//紧接着把打出的牌放到手中牌的末尾
q2.tail++;
while(s.data[s.top]!=t) //把桌上可以赢得的牌依次放到手中牌的末尾
{
book[s.data[s.top]]=0;//取消标记
q2.data[q2.tail]=s.data[s.top];//依次放入队尾
q2.tail++;
s.top--;
}
}
}
if(q2.head==q2.tail)
{
cout<<"小哼win"<<endl;
cout<<"小哼当前手中的牌是"<<endl;
for(i=q1.head;i<=q1.tail-1;i++)
cout<<q1.data[i]<<endl;
if(s.top>0) //如果桌上有牌则依次输出桌上的牌
{
cout<<"桌上的牌是"<<endl;
for(i=1;i<=s.top;i++)
cout<<s.data[i]<<endl;
}
else
cout<<"\n桌上已经没有牌了"<<endl;
}
else
{
cout<<"小哈win"<<endl;
cout<<"小哈当前手中的牌是"<<endl;
for(i=q2.head;i<=q2.tail-1;i++)
cout<<q2.data[i]<<endl;
if(s.top>0) //如果桌上有牌则依次输出桌上的牌
{
cout<<"桌上的牌是"<<endl;
for(i=1;i<=s.top;i++)
cout<<s.data[i]<<endl;
}
else
cout<<"桌上已经没有牌了"<<endl;
}
system("pause");
return 0;
}
链表, 输入一个排序好的数, 然后插入一个数, 要求也要排序好.
#include <iostream>
using namespace std;
struct node
{
int data;
struct node *next;
};
int main()
{
struct node *head, *p,*q,*t;
int i,n,a;
cin>>n;
head=NULL;
for(i=1;i<=n;i++)
{
cin>>a;
p=(struct node *)malloc(sizeof(struct node));
p->data=a;
p->next=NULL;
if(head==NULL)
head=p;
else q->next=p;
q=p;
}
cin>>a;
t=head;
while(t!=NULL)
{
if(t->next->data>a)
{
p=(struct node *)malloc(sizeof(struct node));
p->data=a;
p->next=t->next;
t->next=p;
break;
}
t=t->next;
}
t=head;
while(t!=NULL)
{
cout<<t->data<<" ";
t=t->next;
}
system("pause");
return 0;
}
枚举算法:火柴摆出A+B=C
1. 加号和等号都是需要2根
2. A+B=C, B+A=C是不同的等式
3. 所有火柴棍都要用上
#include <iostream>
using namespace std;
int fun(int x)
{
int num=0;
int f[10]={6,2,5,5,4,5,6,3,7,6}; //0-9每个数字需要几根火柴
while(x/10!=0)
{
num+=f[x%10];
x=x/10;
}
num+=f[x]; //此时就剩个位数了
return num;
}
int main()
{
int a,b,c,m,i,sum=0;
cin>>m;
for(a=0;a<=1111;a++)
{
for(b=0;b<=1111;b++)
{
c=a+b;
if(fun(a)+fun(b)+fun(c)==m-4)
{
cout<<a<<"+"<<b<<"="<<c<<endl;
sum++;
}
}
}
cout<<"共拼出"<<sum<<"种不同的等式"<<endl;
system("pause");
return 0;
}
深度优先算法, 全排列问题:
#include <iostream>
using namespace std;
int a[10],book[10],n;
void dfs(int step)
{
int i;
if(step==n+1)
{
for(i=1;i<=n;i++)
cout<<a[i];
cout<<endl;
return;
}
for(i=1;i<=n;i++)
{
if(book[i]==0)
{
a[step]=i;
book[i]=1;
dfs(step+1);
book[i]=0;
}
}
return;
}
int main()
{
cin>>n;
dfs(1);
system("pause");
return 0;
}
浙公网安备 33010602011771号