免費論壇 繁體 | 簡體
Sclub交友聊天~加入聊天室當版主
分享
返回列表 發帖

c++練習題7

相信第二題一定有更簡潔的方式,期待助教公布。
1541586588800.jpg
2018-11-7 18:36
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<time.h>
  4. int main(void){
  5.         int num=0,a1=0,a2=0,a3=0,a4=0,a5=0,a6=0,ran;
  6.        
  7.         srand(time(NULL));
  8.        
  9.         printf("simulate 6000 rolls of six-sided dice \n");
  10.        
  11.         while(num<=6000){
  12.                 ran = rand() % 6 + 1;
  13.                 num += 1;
  14.                 switch(ran){
  15.                         case 1:
  16.                                 a1 += 1;
  17.                                 break;
  18.                         case 2:
  19.                                 a2 += 1;
  20.                                 break;
  21.                         case 3:
  22.                                 a3 += 1;
  23.                                 break;
  24.                         case 4:
  25.                                 a4 += 1;
  26.                                 break;
  27.                         case 5:
  28.                                 a5 += 1;
  29.                                 break;
  30.                         case 6:
  31.                                 a6 += 1;
  32.                                 break;
  33.                 }
  34.         }
  35.        
  36.         printf("1:%d \n",a1);
  37.         printf("2:%d \n",a2);
  38.         printf("3:%d \n",a3);
  39.         printf("4:%d \n",a4);
  40.         printf("5:%d \n",a5);
  41.         printf("6:%d \n",a6);
  42.        
  43.         return 0;
  44. }
複製代碼
  1. n(void){
  2.         int num=0;
  3.         int a2=0,a3=0,a4=0,a5=0,a6=0;
  4.         int a7=0,a8=0,a9=0,a10=0,a11=0,a12=0;
  5.         int ran1,ran2,max=0,ans;
  6.        
  7.         srand(time(NULL));
  8.        
  9.         printf("tosses a pair of six-sided dice. \n");
  10.        
  11.         while(num<=10000){
  12.                 ran1 = rand() % 6 + 1;
  13.                 ran2 = rand() % 6 + 1;
  14.                 num += 1;
  15.                 switch(ran1+ran2){
  16.                         case 2:
  17.                                 a2 += 1;
  18.                                 break;
  19.                         case 3:
  20.                                 a3 += 1;
  21.                                 break;
  22.                         case 4:
  23.                                 a4 += 1;
  24.                                 break;
  25.                         case 5:
  26.                                 a5 += 1;
  27.                                 break;
  28.                         case 6:
  29.                                 a6 += 1;
  30.                                 break;
  31.                         case 7:
  32.                                 a7 += 1;
  33.                                 break;
  34.                         case 8:
  35.                                 a8 += 1;
  36.                                 break;
  37.                         case 9:
  38.                                 a9 += 1;
  39.                                 break;
  40.                         case 10:
  41.                                 a10 += 1;
  42.                                 break;
  43.                         case 11:
  44.                                 a11 += 1;
  45.                                 break;
  46.                         case 12:
  47.                                 a12 += 1;
  48.                                 break;
  49.                 }
  50.         }
  51.        
  52.         if(a2>max){
  53.                 max = a2;
  54.                 ans = 2;
  55.         }
  56.         if(a3>max){
  57.                 max = a3;
  58.                 ans = 3;
  59.         }
  60.         if(a4>max){
  61.                 max = a4;
  62.                 ans = 4;
  63.         }
  64.         if(a5>max){
  65.                 max = a5;
  66.                 ans = 5;
  67.         }
  68.         if(a6>max){
  69.                 max = a6;
  70.                 ans = 6;
  71.         }
  72.         if(a7>max){
  73.                 max = a7;
  74.                 ans = 7;
  75.         }
  76.         if(a8>max){
  77.                 max = a8;
  78.                 ans = 8;
  79.         }
  80.         if(a9>max){
  81.                 max = a9;
  82.                 ans = 9;
  83.         }
  84.         if(a10>max){
  85.                 max = a10;
  86.                 ans = 10;
  87.         }
  88.         if(a11>max){
  89.                 max = a11;
  90.                 ans = 11;
  91.         }
  92.         if(a12>max){
  93.                 max = a12;
  94.                 ans = 12;
  95.         }

  96.         printf("%d is most likely to thrown",ans);

  97.         return 0;
  98. }
複製代碼
分享到: QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友

小貓貓2024了喔!
(點一下露米亞傳送到小貓貓2024大事記)

1541586600825.jpg
2018-11-7 18:37
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<time.h>
  4. int main(void){
  5.         int a=0,i;
  6.         double ranx,rany,sign1,sign2=0;
  7.        
  8.         srand(time(NULL));
  9.        
  10.         for(i=1;i<=1000;i++){
  11.                 sign2 = 0;
  12.                 ranx = (double)rand()/(RAND_MAX+1);
  13.                 rany = (double)rand()/(RAND_MAX+1);
  14.                
  15.                 sign1 = (ranx - 0.5)*(ranx - 0.5) + (rany - 0.5)*(rany - 0.5);
  16.                
  17.                 if(sign1 <= 0.25){
  18.                         sign2 = 1;
  19.                 }
  20.                 if(sign2){
  21.                         a++;
  22.                 }
  23.         }
  24.         printf("4*p = %lf",4.0*a/1000);
  25. }
複製代碼

小貓貓2024了喔!
(點一下露米亞傳送到小貓貓2024大事記)

TOP

此題不是用原題提供的方法算,是用這裡的牛頓法
1541586800532.jpg
2018-11-7 18:38
  1. #include<stdio.h>
  2. int main(void){
  3.         int x,i;
  4.         double n=1;
  5.         double pro;
  6.         printf("find out the square root of this input positive integer: ");
  7.         scanf("%d",&x);
  8.        
  9.         if(n*n<x){
  10.                 n *= 3;
  11.         }
  12.         n /= 3; //n為比x的平方根小的某數
  13.         pro = x;
  14.        
  15.         for(i=1;i<=20;i++){  //求更精確可用20以上的數值
  16.                 n = (n+pro/n)/2; //牛頓法求平方根
  17.         }
  18.        
  19.         printf("%lf",n);
  20. }
複製代碼

小貓貓2024了喔!
(點一下露米亞傳送到小貓貓2024大事記)

TOP

1541586814124.jpg
2018-11-7 18:39

1541586849504.jpg
2018-11-7 18:39
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<time.h>
  4. int main(void){
  5.         int roca,x,num1=0,numR=0,numL=0; //總步數,R4逃出數,L5逃出數
  6.         int sign;
  7.        
  8.         srand(time(NULL));
  9.        
  10.         for(int i=1;i<=1000;i++){
  11.                 sign=1;
  12.                 roca=0;
  13.                 while(roca>-5 && roca<4 && sign){
  14.                         x = rand() % 2 + 1;
  15.                         if(x==1){
  16.                                 roca += 1;
  17.                                 num1++;
  18.                         }else{
  19.                                 roca -= 1;
  20.                                 num1++;
  21.                         }
  22.                         if(roca==-2){
  23.                                 roca = -4;
  24.                         }
  25.                         if(roca==-5){
  26.                                 sign = 0;
  27.                                 numL++;
  28.                         }
  29.                         if(roca==4){
  30.                                 sign = 0;
  31.                                 numR++;
  32.                         }
  33.                 }
  34.         }
  35.        
  36.         printf("R4:%d L5:%d \n",numR,numL);
  37.         printf("the odds exit from R4 is %lf \n",(double)numR/1000);
  38.         printf("the odds exit from L5 is %lf \n",(double)numL/1000);
  39.         printf("on the average,the dog stay %lf \n",(double)num1/1000);
  40.        
  41.         //case c
  42.                 printf("\n----------case c-----------\n");
  43.                 num1=0;
  44.                 numR=0;
  45.                 numL=0;
  46.                
  47.                 for(int i=1;i<=1000;i++){
  48.                 sign=1;
  49.                 roca=0;
  50.                 while(roca>-5 && roca<4 && sign){
  51.                         x = rand() % 2 + 1;
  52.                         if(x==1){
  53.                                 roca += 1;
  54.                                 num1++;
  55.                         }else{
  56.                                 roca -= 1;
  57.                                 num1++;
  58.                         }
  59.                         if(roca==-2 && x==2){  //只有往左時推進
  60.                                 roca = -4;
  61.                         }
  62.                         if(roca==-5){
  63.                                 sign = 0;
  64.                                 numL++;
  65.                         }
  66.                         if(roca==4){
  67.                                 sign = 0;
  68.                                 numR++;
  69.                         }
  70.                 }
  71.         }
  72.        
  73.         printf("\nR4:%d L5:%d \n",numR,numL);
  74.         printf("the odds exit from R4 is %lf \n",(double)numR/1000);
  75.         printf("the odds exit from L5 is %lf \n",(double)numL/1000);
  76.         printf("on the average,the dog stay %lf \n",(double)num1/1000);
  77. }
複製代碼

小貓貓2024了喔!
(點一下露米亞傳送到小貓貓2024大事記)

TOP

1541586906667.jpg
2018-11-7 18:40
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<time.h>
  4. int main(void){
  5.         int x,y;
  6.        
  7.         srand(time(NULL));
  8.        
  9.         x = rand()%13 + 1;
  10.         y = rand()%4 + 1;
  11.        
  12.         switch(x){ //rank
  13.                 case 1:
  14.                         printf("ace of ");
  15.                         break;
  16.                 case 2:
  17.                 case 3:
  18.                 case 4:
  19.                 case 5:
  20.                 case 6:
  21.                 case 7:
  22.                 case 8:
  23.                 case 9:
  24.                 case 10:
  25.                         printf("%d of ",x);
  26.                         break;
  27.                 case 11:
  28.                         printf("jack of ");
  29.                         break;
  30.                 case 12:
  31.                         printf("queen of ");
  32.                         break;
  33.                 case 13:
  34.                         printf("king of ");
  35.                         break;
  36.         }
  37.         switch(y){ //suit
  38.                 case 1:
  39.                         printf("clubs");
  40.                         break;
  41.                 case 2:
  42.                         printf("diamond");
  43.                         break;
  44.                 case 3:
  45.                         printf("hearts");
  46.                         break;
  47.                 case 4:
  48.                         printf("spades");
  49.                         break;
  50.         }
  51. }
複製代碼

小貓貓2024了喔!
(點一下露米亞傳送到小貓貓2024大事記)

TOP

返回列表