C++习题(from the teacher)May.

来源:互联网 发布:中国联通云计算 编辑:程序博客网 时间:2024/06/05 22:49

05_01_1

05_01_2

//哥德巴赫猜想

//1既不是素数也不是合数。猜想中N应该从4开始。

//将要分解的i4开始遍历,为了提高效率先采用筛选

//原理:偶数=奇数+奇数=偶数+偶数(只有4=2+2成立);

#include <iostream>

using namespace std;

const int TRUE(1);

const int FALSE(0);

int PrimeN(int n);

 

void main(void)

{

         int n;

         cin>>n;

         cout<<"4=2+2"<<endl;

         int flag;

         for(int i=6;i<=n;i+=2)

         {

                   flag=1;

                   for(int j=3;j<=i/2;j++)

                   {

                           

                            if (PrimeN(j) && PrimeN(i-j) )

                            {

                           

                                     if (flag)

                                     {       

                                               cout<<i<<'='<<j<<'+'<<(i-j);

                                               flag=0;

                                     }

                                     else

                                     {

                                               cout<<'='<<j<<'+'<<(i-j);

                                     }

                            }

                                    

                   }

                   cout<<endl;

        

         }

}

 

//判断一个正整数是否为素数,是返回TRUE,否则返回FALSE

 

int PrimeN(int n)

{

         for(int i=2;i<n;i++)

         {

                   if (n%i == 0)

                   {       

                            return FALSE;

                   }

         }

         return TRUE;

}

Note: 这是我想出的办法,还有其余算法比如利用概率的,效率可能会高些。

05_02_1

 

05_02_2

 

05_03_1

#include <iostream>

#include <cmath>

using namespace std;

int main(void)

{

         double y;

         double x;

         int i,k=1;

       

         //判断列数:2k

         y=-1;

         x=acos(y)*10;

         while(k<=x)

                   ++k;

         //绘制图形

         for(y=1;y>=-1;y=y-0.1)

         {

                   x=acos(y)*10;

                   //left

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

                   {

                            cout<<' ';

                   }

                   cout<<'*';        

                   //right

                   for(;i<=2*k-int(x)-1-1;++i)

                            cout<<" ";

                   cout<<"*/n";

         }

 

         return 0;

}

05_03_2

05_04_1

//穷举

//说谎:0 诚实:1

#include<iostream>

using namespace std;

 

int main()

{

         int a,b,c;

         for(a=0;a<=1;a++)

                   for(b=0;b<=1;b++)

                            for(c=0;c<=1;c++)

                                     if((a&&a+b+c==2||!a&&a+b+c!=2)

                                           &&          (b&&a+b+c==1||!b&&a+b+c!=1)

                                                &&          (c&&a+b+c==1||!c&&a+b+c!=1))

                                     {

                                               cout<<"A is a "<<(a ? "honest" : "lier")<<endl;

                                               cout<<"B is a "<<(b ? "honest" : "lier")<<endl;

                                               cout<<"C is a "<<(c ? "honest" : "lier")<<endl;

                                     }

 

         return 0;

}

05_04_2

//穷举

//说谎:0 诚实:1

#include<iostream>

using namespace std;

 

int main()

{

         int a,b,c;

         int d=1;    //假设D是诚实族的

         if(d==1)

         {

                   for(a=0;a<=1;a++)

                            for(b=0;b<=1;b++)

                                     for(c=0;c<=1;c++)

                                               if((a&&a+b+c+d==0||!a&&a+b+c+d!=0)

                                                     &&          (b&&a+b+c+d==1||!b&&a+b+c+d!=1)

                                                         &&          (c&&a+b+c+d==2||!c&&a+b+c+d!=1))

                                               {

                                                        cout<<"D is a honest."<<endl;

                                               }

         }

         else

                   cout<<"D is a lier."<<endl;

         return 0;

}

 

05_05_1

05_05_1

/*05.05-1诚实与说谎问题(三)

             A=1:左边的人是诚实族的;

        B=1:中间的人是诚实族的;

        C=1:右边的人是诚实族的;

        AA=1:左边的人是两面族的;

        BB=1:中间的人是两面族的;

        CC=1:右边的人是两面族的;

  

          则左边的人是说谎族:       A!=1AA!=1          (不是诚实族且不是两面族)

       中间的人是说谎族:       B!=1BB!=1          (!B&&!BB)

       右边的人是说谎族:       C!=1CC!=1

               

    三人来自三个民族:

    (!b&&!bb)+(!a&&!aa)+(!c&&!cc)==1 a+b+c==1 aa+bb+cc==1

        

         三个人的话,分别判断

*/

 

#include<iostream>

using namespace std;

void main()

{

    int a,b,c,aa,bb,cc;

    for(a=0;a<=1;a++)            //穷举

      for(b=0;b<=1;b++)

        for(c=0;c<=1;c++)

          for(aa=0;aa<=1;aa++)

            for(bb=0;bb<=1;bb++)

              for(cc=0;cc<=1;cc++)

                                       //判断逻辑条件

                if( (!b&&!bb)+(!a&&!aa)+(!c&&!cc)==1  &&  

                    a+b+c==1&&aa+bb+cc==1   &&

 

                                               (a&& b && !c&&!cc || !a&&!b ) &&

                                               (b&& bb && !a && !c || !b )  &&

                                               (c && !b&&!bb &&!a || !c&& b+bb==1 )       )

                {

                 

                                       cout<<"The man stand on left is a "

                                                 <<(aa ? "double--dealer" : (a ?"honest":"lier"))<<endl;

                                      

                                       cout<<"The man stand on left is a "

                                                 <<(bb ? "double--dealer" : (b ?"honest":"lier"))<<endl;

 

                                       cout<<"The man stand on left is a "

                                                 <<(cc ? "double--dealer" : (c ?"honest":"lier"))<<endl;

                                        

                }

}

Note:在逻辑判断代码略显繁琐,没有进行进一步简化。这样写是为了大家更好的理解题意。

05_06

         从此日起进入了C++类的学习,由于题目存在很强的扩展性,所以将不会按日上传。当程序扩展了一定内容时,会整体上传。


原创粉丝点击