首页 > 代码库 > 【人工智能】实验课随堂作业1

【人工智能】实验课随堂作业1

作业1

选择题:

1.  C

 

编程题:

1.

代码:

 

#include<iostream>

#include<cstdio>

#include<cmath>

#include<algorithm>

#include<climits>

 

using namespace std;

 

int isPrime(int n){

         int i,j; 

         if(n==2){

                   return true;

         }

         else if (n<2||n%2==0){

                   return false;

         }

         else{

                   j = (int)sqrt(n+1);

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

                      if (n%i==0)

                      return false;

         }

         return true;

}

 

int main(){

        

         int count = 0;

        

      for(int i=101; i<200; i++){

           if(isPrime(i)){

                    cout<<i<<" ";

                    count+=1;

           }

      }

      cout<<endl;

      cout<<count<<endl;

      return 0;

      //24747380@qq.com

}

 

 

运行结果:

 技术分享

 

 

2.

杰克和露丝是一对恋人,他们每天都会玩一个数字游戏,游戏规则如下:

【1】       杰克从区间[a,b],中随机选择一个数字x

【2】       露丝从区间[c,d],中随机选择一个数字x

【3】       如果(x+y)mod p = m ,他们就会外出看电影

【4】       否则就在图书馆学习

给定整数a,b,c,d,p,m他们想知道外出看电影的概率。

 

代码:

 

#include<iostream>

#include<cstdio>

#include<cmath>

#include<algorithm>

#include<climits>

 

using namespace std;

int main(){

         int t;

         cin>>t;

         while(t--){

                   int a,b,c,d,p,m;

                   int count = 0;

                   int res;

                   cin>>a>>b>>c>>d>>p>>m;

                   for(int x=a; x<=b; x++) {

                            for(int y=c; y<=d; y++){

                                     if((x+y)%p==m){

                                               count++;

                                     }

                            }

                   }

                   res = ((b-a+1)*(d-c+1));

                   if (count==0){

                            cout<<0<<"/"<<1<<endl;

                   }

                   else{

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

                             if(count%i==0 && res%i==0){

                                   count/=i;

                                   res/=i;

                                   i--;      

                              }

                            cout<<count<<"/"<<res<<endl;      

                   }       

         }

        

         return 0;

}

运行结果:

 技术分享

 

【人工智能】实验课随堂作业1