寒假写的几个简单的程序

来源:互联网 发布:mac切换windows快捷键 编辑:程序博客网 时间:2024/05/16 10:38

问题一、菜单选择程序

1、设计要求:程序运行后,给出如下4个菜单项内容和输入提示,

1.      冒泡排序法

2.      选择排序法

3.      快速排序法

4.      退出

选择 1-4__

程序运行后,使用数字1-4来选择菜单项,其他输入不起作用。

 

#include<iostream.h>

#include<stdlib.h>

void BubbleSort()

{   int iTemp;

    int* pData, Count;

       cin>>Count;

       pData= new int[Count];

       for(int i=0;i<Count;i++)

              cin>>pData[i];

    for(i=1;i<Count;i++)

    {

        for(int j=Count-1;j>=i;j--)

        {

            if(pData[j]<pData[j-1])

            {

                iTemp = pData[j-1];

                pData[j-1] = pData[j];

                pData[j] = iTemp;

            }

        }

    }

for(i=0;i<Count;i++)

cout<<pData[i]<<" ";

cout<<endl;

delete[]pData;

}

 

void SelectSort()

{

    int iTemp;

       int * pData,Count;

    int iPos;

       cin>>Count;

       pData= new int[Count];

       for(int i=0;i<Count;i++)

              cin>>pData[i];

    for(i=0;i<Count-1;i++)

    {

        iTemp = pData[i];

        iPos = i;

        for(int j=i+1;j<Count;j++)

        {

            if(pData[j]<iTemp)

            {

                iTemp = pData[j];

                iPos = j;

            }

        }

        pData[iPos] = pData[i];

        pData[i] = iTemp;

    }

for(i=0;i<Count;i++)

cout<<pData[i]<<" ";

cout<<endl;

delete[]pData;

}

 

void QuickSort(int* pData,int left,int right)

{

    int i,j;

       int middle,iTemp;

    i = left;

    j = right;

       middle = pData[(left+right)/2]; //求中间值

    do{

        while((pData[i]<middle) && (i<right))//从左扫描大于中值的数

            i++;         

        while((pData[j]>middle) && (j>left))//从右扫描大于中值的数

            j--;

        if(i<=j)//找到了一对值

        {

            //交换

            iTemp = pData[i];

            pData[i] = pData[j];

            pData[j] = iTemp;

            i++;

            j--;

        }

    }while(i<=j);//如果两边扫描的下标交错,就停止(完成一次)

 

    //当左边部分有值(left<j),递归左半边

    if(left<j)

        QuickSort(pData,left,j);

    //当右边部分有值(right>i),递归右半边

    if(right>i)

        QuickSort(pData,i,right);

 

}

 

 

 

 

int main()

{int m,i,* pData,Count,left,right;

cout<<"1.冒泡排序法"<<endl;

cout<<"2.选择排序法"<<endl;

cout<<"3.快速排序法"<<endl;

cout<<"4.退出"<<endl;

cout<<"选择 1-4_______"<<endl;

loop:

cin>>m;

if (m==1)

BubbleSort();

 

 

if(m==2)

SelectSort();

 

if(m==3)

{

cin>>Count;

pData= new int[Count];

for(i=0;i<Count;i++)

cin>>pData[i];

left=0;

right=Count-1;

QuickSort(pData,left,right);

for(i=0;i<=right;i++)

cout<<pData[i]<<" ";

cout<<endl;

delete[]pData;

 

}

 

if(m==4)

{

exit(1);

}

else

goto loop;

return 0;

}

 

 

 

2.设计要求:程序在0-9中,任意不重复地选择4个数字排列成四位数,游戏者将猜使用的哪4个数字和数字排列,程序根据游戏者的猜测给出对猜测结果的判断,下图是一次游戏的示意图:

假设:预设数字为1352

次数

回馈

1234

1

1A2B

5678

2

0A1B

0123

3

0A3B

1362

4

3A0B

1352

5

4A0B

说明:字母A表示数字和数字的位置都猜对了,B表示数字猜对了,但位置错了;字母前的数字则表示处于该情况下的数字的个数

 

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

int mypow10(int i)              //简单的计算10的N次方的函数
{
int j,k=1;
        for(j=0;j<i;j++)
{
        k*=10;
}
        return k;
}

void getnum(int answer[])      //通过数组保存每一位上的数方便逐个比较
{
    int i,j;
    for(i=0;i<=3;i++)
    {
        srand( time(NULL) );
        answer[i]=rand()%10;
        for(j=0;j<i;j++)
        {
            while(answer[i]==answer[j])    //避免出现重复数字
            {
                answer[i]=rand()%10;
    j=-1;
               
            }
        }
    }
}


bool judge(int guess)
{int a,b,c,d;
a=guess/1000;
b=(guess-a*1000)/100;
c=(guess-a*1000-b*100)/10;
d=guess%10;
if((a==b)||(b==c)||(c==d)||(a==d)||(a==c)||(b==d))
return 0;
else
return 1;
}

int main()
{
int answer[4],answer1,guess,a,b,times,i,j,judge1;
system("cls");                            //清屏
guess=0;
times=0;
getnum(answer);
answer1=answer[0]*1000+answer[1]*100+answer[2]*10+answer[3];
cout<<"开始猜数(0-9不重复的四位数)"<<endl;
a=0;

while(a!=4)
  {
        a=0;
        b=0;
        times++;
        if(times>10)
        break;
       
        do
        { 
      cin>>guess;
   judge1=judge(guess);
   if (judge1==1)
            cout<<times<<"  ";
   else
    guess=-1;
   
   
  }while(guess<0||guess>9999);
      
        for(i=3;i>=0;i--)
        {
            for(j=0;j<4;j++)
            {
                if((guess/mypow10(i))==answer[j])
                {
if(i+j==3)
//i此处相当于高位,而存储在数组里面的数字也是高到低排列的
                        a=a+1;
                    else
                        b=b+1;
                }
            }
            guess=guess-(guess/mypow10(i))*mypow10(i);
        }
      cout<<a<<"A"<<b<<"B"<<endl;
    }
      cout<<"answer is "<<answer1<<endl;
   return 0;
}

 


不足之处大家尽管拍砖,一起进步!!!

 

 

 

原创粉丝点击