【人工智能】实验课随堂作业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;

}

运行结果:

 

 

转载于:https://www.cnblogs.com/dragonir/p/5822416.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值