一些算法题的实现

来源:互联网 发布:复合的sql语句怎么写 编辑:程序博客网 时间:2024/05/16 14:35

#include<iostream.h>
void main()
{
cout<<" hello" <<endl;
int a[100];
int i=0,j=0,q=0;
for(int k=0;k<100;k++)
a[k]=1;
while(!(q==100))
{

if(j>=99) j=0;
if (a[j]!=0)
{
 if(i%5==0)
 {
  a[j]=0;
  q = q+1;
  cout<<"j="<<j<<"  ";
  if(i%50==0 && j!=0)
   cout<<endl;
 }
 i = i + 1;
}
j++;}
}


/**
* Author:KINPOO Date:05/09/12
*/

/**
* 题目:设A与B分别为两个带有头结点的有序循环链表(所谓有序是指链表结点按数据
* 域值大小链接,本题不妨设按数据域从小到大排列),head1和head2分别为指向两个链
* 表的指针。请写出并在计算机上实现将这两个链表合并为一个带头结点的有序循环链表的
* 算法。
*/

#define NULL 0
#define TRUE 1
#define FALSE 0
#include "stdio.h"
#include<stdlib.h>
typedef int T_Data;
/*************************/
struct Node;
typedef struct Node  *T_Node;
struct Node
{
    T_Data x;
    T_Node link;
};

typedef struct Node  *T_List;
/*************************/
int List_Delete(T_List List,T_Node Delete_Node);
int List_Insert(T_Node InsertTo,T_Node InsertNode);
T_Node List_FindMini(T_List List);
T_Node List_FindNode(T_List List,T_Data x);
T_Node List_NewNode();
T_List List_NewList();
int List_Input(T_List List);
/*************************/
main()
{
    T_List ListA,ListB,ListC;
    T_Node NodeA=NULL,NodeB=NULL,NodeC=NULL,NodeTemp=NULL;
    ListA=List_NewList();
    ListB=List_NewList();
    ListC=List_NewList();
    printf("/nInput ListA:");
    List_Input(ListA);
    printf("/nInput ListB:");
    List_Input(ListB);

    if(ListA->link!=NULL)
    {
        for(NodeA=ListA->link;NodeA->link!=ListA->link;NodeA=NodeA->link)
        {
            printf("/nA,%d",NodeA->x);
        }
        printf("/nA,%d",NodeA->x);
    }
    if(ListB->link!=NULL)
    {
        for(NodeB=ListB->link;NodeB->link!=ListB->link;NodeB=NodeB->link)
        {
            printf("/nB,%d",NodeB->x);
        }
        printf("/nB,%d",NodeB->x);
    }

    getchar();
    printf("/nType any key to continue:ListC=ListA+ListB(Min-->Max).");
    getchar();
    NodeTemp=ListC;
    for(;;)
    {
        if(ListA->link==NULL && ListB->link==NULL)
        {
            break;
        }

        NodeA=List_FindMini(ListA);
        NodeB=List_FindMini(ListB);
        NodeC=List_NewNode();

        if(ListA->link==NULL)
        {
            NodeC->x=NodeB->x;
            List_Delete(ListB,NodeB);
        }
        else if(ListB->link==NULL)
        {
            NodeC->x=NodeA->x;
            List_Delete(ListA,NodeA);
        }
        else
        {
            if(NodeA->x < NodeB->x)
            {
                NodeC->x=NodeA->x;
                List_Delete(ListA,NodeA);
            }
            else
            {
                NodeC->x=NodeB->x;
                List_Delete(ListB,NodeB);
            }
        }
        List_Insert(NodeTemp,NodeC);
        NodeTemp=NodeC;
    }

    if(ListC->link!=NULL)
    {
        for(NodeC=ListC->link;NodeC->link!=ListC->link && ListC->link!=NULL;NodeC=NodeC->link)
        {
            printf("/nC,%d",NodeC->x);
        }
        printf("/nC,%d",NodeC->x);
    }
    getchar();
}
/*************************/
int List_Delete(T_List List,T_Node Delete_Node)
{
    T_Node RNode;
    if(List->link==NULL)
    {
        return FALSE;
    }
    RNode=List->link;
    if(RNode==Delete_Node)
    {
        if(RNode->link==Delete_Node)
        {
            List->link=NULL;
        }
        else
        {
            List->link=RNode->link;
            for(;RNode->link!=Delete_Node;RNode=RNode->link);
            RNode->link=List->link;
        }
    }
    else
    {
        for(;RNode->link!=Delete_Node;RNode=RNode->link);

        Delete_Node=RNode->link;
        RNode->link=RNode->link->link;
    }
    free(Delete_Node);
    return TRUE;
}
/*************************/
int List_Insert(T_Node InsertTo,T_Node InsertNode)
{
    if(InsertTo->link==NULL)
    {
        InsertTo->link=InsertNode;
    }
    InsertNode->link=InsertTo->link;
    InsertTo->link=InsertNode;
    return TRUE;
}
/*************************/
T_Node List_FindMini(T_List List)
{
    T_Node aNode=List->link,MiniNode=List->link;
    if(List->link==NULL)
    {
        return NULL;
    }
    for(;aNode->link!=List->link;)
    {
        aNode=aNode->link;
        if(MiniNode->x > aNode->x)
        {
            MiniNode=aNode;
        }
    }
    return MiniNode;
}
/*************************/
T_Node List_FindNode(T_List List,T_Data x)
{
    T_Node aNode=List->link,RNode=List->link;
    if(aNode==NULL)
    {
        return NULL;
    }
    if(aNode->x > x)
    {
        return List;
    }
    for(;aNode->link!=List->link;)
    {
        aNode=aNode->link;
        if(aNode->x > x)
        {
            break;
        }
        RNode=aNode;
    }
    return RNode;
}
/*************************/
T_Node List_NewNode()
{
    T_Node aNode;
    aNode=(T_Node)malloc(sizeof(struct Node));

    if(aNode!=NULL)
    {
        aNode->link=NULL;
    }
    return aNode;
}
/*************************/
T_List List_NewList()
{
    return List_NewNode();
}
/*************************/
int List_Input(T_List List)
{
    int i;
    T_Data Temp;
    T_Node aNode,TempNode;
    printf("/nType /"Enter/" key to continue,type any key to break.");
    for(i=1;getchar()=='/n';i++)
    {
        printf("num[%d]=",i);
        scanf("%d",&Temp);
        getchar();

        aNode=List_NewNode();
        aNode->x=Temp;

        if(List->link==NULL)
        {
            List_Insert(List,aNode);
            aNode->link=aNode;
        }
        else
        {
            TempNode=List->link;
            List_Insert(List_FindNode(List,Temp),aNode);
            if(TempNode->x > Temp)
            {
                for(;TempNode->link!=List->link->link;TempNode=TempNode->link);
                TempNode->link=List->link;
            }
        }
        printf("/nType /"Enter/" key to continue,type any key to break.");
    }
    getchar();
    return TRUE;
 
}

                       
#include<iostream.h>
#include<stdlib.h>

typedef int Elemtype;

typedef struct node{
 Elemtype data;
 struct node *next;
}Lnode,*LinkList;

//假设下面的单循环链表均为带头结点,而且L指向尾结点。

void CreatLinkList(LinkList &L)
{//建立一个单循环链表L,数据为整数,数据由键盘随机输入。
 int i;
 LinkList head;
 L=(LinkList)malloc(sizeof(struct node));
 L->next=L;
 head=L;
 cout<<"please input the data of the node "<<endl
  <<"input 0 means end :";
 cin>>i;
 while(i)
 {
  LinkList p=(LinkList)malloc(sizeof(struct node));
  if(!p)
  {
   cout<<"alloctation error"<<endl;
   exit(1);
  }
  p->data=i;
  L->next=p;
  p->next=head;
  L=p;
  cout<<"please input the data of the node "<<endl
  <<"input 0 means end :";
  cin>>i;
 }
}

void PrintLinkList(LinkList L)
{//输出单循环链表L的数据元素。
 LinkList temp=L->next;
 cout<<"The List Is :"<<endl;
 while(temp->next!=L->next)
 {
  cout<<temp->next->data<<endl;
  temp=temp->next;
 }
}
int LinkListLengh(LinkList L)
{//计算单循环链表L的数据元素个数。
 int i=0;
 LinkList temp=L->next;
 while(temp->next!=L->next)
 {
  i++;
  temp=temp->next;
 }
 return i;
}
void DestroyLinkList(LinkList &L)
{//销毁单循环链表L。
 LinkList temp=L->next;
 while(L!=temp)
 {
  L->next=temp->next;
  free(temp);
  temp=L->next;
 }
 free(L);
 L=NULL;
}

void ClearLinkList(LinkList &L)
{//将单循环链表L置为空表。
 LinkList head=L->next,temp;
 L=L->next;
 while(L->next!=L)
 {
  temp=L->next;
  L->next=temp->next;
  free(temp);
 }

}

void InsertLinkList(LinkList &L, int i, int x)
{//在单循环链表L的第I个元素前插入一个数据元素X。
 int k=1;
 if((i>LinkListLengh(L)) || (i<1) )
 {
  cout<<"Insert Error !"<<endl;
  return ;
 }
 LinkList temp=L->next;
 LinkList p=(LinkList )malloc(sizeof(struct node));
 p->data=x;
 while((temp!=L) && (k<i))
 {
  k++;
  temp=temp->next;
 }
 p->next=temp->next;
 temp->next=p;
}

void DeleteLinkList(LinkList &L,int i)
{//删除单循环链表L的第I个数据元素。
 int k=1;
 if((i>LinkListLengh(L)) || (i<1) )
 {
  cout<<"Delete Error !"<<endl;
  return ;
 }
 LinkList temp=L->next,p;
 while((temp!=L) && (k<i))
 {
  k++;
  temp=temp->next;
 }
 if(temp->next==L)
 {
  temp->next=L->next;
  p=L;
  L=temp;
  free(p);
  return ;
 }
 p=temp->next; 
 temp->next=p->next;
 free(p);
}

void MergeLinkList(LinkList &La, LinkList &Lb)
{//合并两个单循环链表La, Lb为La.从小到大的非递减链表
 LinkList pc=La->next,pa=pc->next,p2=Lb->next,pb=p2->next,p1=La->next;
 while((pa!=p1) && (pb!=p2))
 {
  if(pa->data<=pb->data)
  {
   pc->next=pa;
   pc=pa;
   pa=pa->next;
   pc->next=pb;
  }
  else
  {
   pc->next=pb;
   pc=pb;
   pb=pb->next;
   pc->next=pa;
  }
 }
  if(pa==p1)
  {
   free(p2);
   La=Lb;
   La->next=pa;
  }
  else  free(pb);
}


void main()
{//调用上面的各函数,运行并检验程序是否正确。
 LinkList L,Lb;
 CreatLinkList(L);
 PrintLinkList(L);
 cout<<"The Length Of The List Is :"<<LinkListLengh(L)<<endl;
 InsertLinkList(L,1,1);
 InsertLinkList(L,2,2);
 InsertLinkList(L,3,3);
 DeleteLinkList(L,5);
 PrintLinkList(L);
 CreatLinkList(Lb);
 MergeLinkList(L,Lb);
 PrintLinkList(L);
 ClearLinkList(L);
 DestroyLinkList(L);
}

 

24点游戏

#include <iostream>
#include <string>
#include <cstdio>  // need sprintf() and sscanf()
#include <conio.h> // need getch()
#include <algorithm> // need min() and count()
#include <boost/timer.hpp> // 测试用时

int times=0; // 记录失败的次数

class Combin // 组合类,可得到所有可能的组合
{
public:
    Combin(int count,int select=1) //  从 count 个数中选取 select 个数
    {
        if(select>count)
            throw std::string("Select Must Less Than Count");
           
        m_data=new int[count];
        m_point=new int[select];
        m_descr=new std::string[count];
       
        for(int i=0;i<select;++i)
            m_point[i]=i;
        m_count=count;
        m_select=select;
    }
    ~Combin(){ delete [] m_data; delete [] m_descr; delete [] m_point; }
   
    bool Setdata(int index,int data,std::string desc="") // 设置待组合的数据
    {
        if(index>=m_count)
            return false;
       
        m_data[index]=data;
        if(desc=="")
        {
            char tmb[16];
            sprintf(tmb,"%d",data);
            desc=std::string(tmb);
        }
        m_descr[index]=desc;
        return true;
    }

    int Getdata(int i) // 返回组合后的第 i 个数据
    {
        if(i>=m_select)
            throw std::string("Bad index of data!");
           
        return m_data[m_point[i]];
    }
   
    std::string Getdesc(int i) // 返回组合后第 i 个数据的描述
    {
        if(i>=m_select)
            throw std::string("Bad index of desc!");
        return m_descr[m_point[i]];
    }
   
    int Getleftdata(int i) // 返回组合后第 i 个剩余的数据
    {
        return m_data[getleftid(i)];
    }
   
    std::string Getleftdesc(int i) // 返回组合后第 i 个剩余数据的描述
    {
        return m_descr[getleftid(i)];
    }
   
   
    bool Select() // 进行下一次组合,组合失败返回 false
    {
     for(int i=m_select-1;i>=0;--i)
     {
         if(m_point[i]<m_count-m_select+i)
         {
             m_point[i]++;
             for(int j=i+1;j<m_select;++j)
                 m_point[j]=m_point[j-1]+1;
             return true;
         }
     }
     return false;
 }
 
    void Reset() // 重置组合为初始状态
    {
        for(int i=0;i<m_select;++i)
            m_point[i]=i;
    }
   
    int Getcount() { return m_count; }
    int Getselect() { return m_select; }
   
    void Swapsel(int r1,int r2) // 交换两个组合中的数据,这个操作本不应该放在这个类中的,为了使用方便放弃的设计上的合理!勿笑!
    {
        if(r1>=m_select||r2>=m_select)
            throw std::string("Bad index of swap!");
        int t_int=m_data[m_point[r1]];
        std::string t_str=m_descr[m_point[r1]];
        m_data[m_point[r1]]=m_data[m_point[r2]];
        m_descr[m_point[r1]]=m_descr[m_point[r2]];
        m_data[m_point[r2]]=t_int;
        m_descr[m_point[r2]]=t_str;
    }
       
private:
    int m_count,m_select;
    int * m_data;
    int * m_point;
    std::string * m_descr;
   
    int getleftid(int i)
    {
        if(i>=m_count-m_select)
            throw std::string("Bad index of left data!");
        int ind=-1;
        ++i;
        do
        {
            --i;
            ++ind;
            for(int j=0;j<m_select;++j)
                if(m_point[j]==ind)
                {
                    ++ind;
                    j=0;
                }
        } while(i>0);
        return ind;
    }
};

bool Test24(Combin * num,Combin * oper); // 使用指定数据和操作符的组合能否得到 24

int main(int argc,char *argv[])
try
{
 int input[4];
 if(argc>=5)
  for(int i=1;i<5;++i)
   sscanf(argv[i],"%d",&input[i-1]);
 else
 {
     std::cout<<"Input the data:";
     std::cin>>input[0]>>input[1]>>input[2]>>input[3];
 }
   
    boost::timer mytime;  // 开始计时!!
   
    Combin number(4,2);
    Combin operat(4,1);
    number.Setdata(0,input[0]);
    number.Setdata(1,input[1]);
    number.Setdata(2,input[2]);
    number.Setdata(3,input[3]);
   
    operat.Setdata(0,'+',"+ ");
    operat.Setdata(1,'-',"- ");
    operat.Setdata(2,'*',"×");
    operat.Setdata(3,'/',"÷");
   
    if(!Test24(&number,&operat))
        std::cout<<"inextricability! trid "<<times<<" times."<<std::endl;
   
    std::cout<<"It takes "<<mytime.elapsed()<<" seconds for the task."; // 结束计时并显示
   
    if(argc<=1)  // 如果不是从命令行输入,按键后再结束
     getch();
}
catch(std::string e)
{
    std::cout<<e<<std::endl;
    getch();
    exit(0);
}
catch(...)
{
    std::cout<<"Unknown Except!"<<std::endl;
    getch();
    exit(0);
}

 

bool getresult(char oper,int n1,int n2,int * result) // 返回两个数的运算结果,不能整除返回 false
{
    switch(oper)
    {
        case '+':
            *result=n1+n2;
            return true;
        case '-':
            *result=n1-n2;
            return true;
        case '*':
            *result=n1*n2;
            return true;
        case '/':
            if(n2==0)
                return false;
            if(n1%n2!=0)
                return false;
            else
            { *result=n1/n2;
                return true;
            }
            break;
        default:
            return false;
    }
}

bool Gonext(Combin * num,Combin * oper,int result) // 进入更深一次测试
{
    Combin snum(num->Getcount()-1,min(2,num->Getcount()-1));
    Combin soper(oper->Getcount()-1,1);
    char buff[512];
    if(oper->Getdata(0)=='+'||oper->Getdata(0)=='-')
    {
        std::string s1=num->Getdesc(0),s2=num->Getdesc(1);
        if(s1[0]=='('&&s1[s1.size()-1]==')')
            s1=s1.substr(1,s1.size()-2);
        if(oper->Getdata(0)=='+'&&s2[0]=='('&&s2[s2.size()-1]==')')
            s2=s2.substr(1,s2.size()-2);
        char * format="(%s%s%s)";
        if(num->Getcount()==2)
            format="%s%s%s";
        sprintf(buff,format,s1.c_str(),oper->Getdesc(0).c_str(),s2.c_str());
    }
    else
        sprintf(buff,"%s%s%s",num->Getdesc(0).c_str(),oper->Getdesc(0).c_str(),
        num->Getdesc(1).c_str());
   
    int i;
    snum.Setdata(0,result,buff);
    for(i=0;i<num->Getcount()-num->Getselect();++i)
        snum.Setdata(i+1,num->Getleftdata(i),num->Getleftdesc(i));
   
    for(i=0;i<oper->Getcount()-oper->Getselect();++i)
        soper.Setdata(i,oper->Getleftdata(i),oper->Getleftdesc(i));
   
    if(Test24(&snum,&soper))
        return true;
    return false;
}

bool Test24(Combin * num,Combin * oper)
{
    int result;
   
    do {
        do {
           
            if(num->Getcount()==1)
            {
                if(num->Getdata(0)==24)
                {
                    int c_kh=std::count(&num->Getdesc(0).c_str()[0],&num->Getdesc(0).c_str()[0]+num->Getdesc(0).size(),'(');
                    if(c_kh>1)
                        ++times;
                    else
                    {
                        std::cout<<num->Getdesc(0)<<"= 24"<<std::endl;
                        return true;
                    }   }
                else
                    ++times;
            }
            else
            {
                if(getresult(oper->Getdata(0),num->Getdata(0),num->Getdata(1),&result))
                    if(Gonext(num,oper,result))
                        return true;
                if(oper->Getdata(0)=='-'||oper->Getdata(0)=='/')
                    if(getresult(oper->Getdata(0),num->Getdata(1),num->Getdata(0),&result))
                    {
                        num->Swapsel(0,1);
                        if(Gonext(num,oper,result))
                            return true;
                        num->Swapsel(0,1);
                    }
               
            }
           
           
        } while(oper->Select());
        oper->Reset();
    } while(num->Select());
    return false;
}


原创粉丝点击