繁體
|
簡體
Sclub交友聊天~加入聊天室當版主
(檢舉)
分享
新浪微博
QQ空间
人人网
腾讯微博
Facebook
Google+
Plurk
Twitter
Line
標題:
c++練習題7
[打印本頁]
作者:
Smallcat
時間:
2018-11-7 18:36
標題:
c++練習題7
相信第二題一定有更簡潔的方式,期待助教公布。
下載
(138.39 KB)
2018-11-7 18:36
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main(void){
int num=0,a1=0,a2=0,a3=0,a4=0,a5=0,a6=0,ran;
srand(time(NULL));
printf("simulate 6000 rolls of six-sided dice \n");
while(num<=6000){
ran = rand() % 6 + 1;
num += 1;
switch(ran){
case 1:
a1 += 1;
break;
case 2:
a2 += 1;
break;
case 3:
a3 += 1;
break;
case 4:
a4 += 1;
break;
case 5:
a5 += 1;
break;
case 6:
a6 += 1;
break;
}
}
printf("1:%d \n",a1);
printf("2:%d \n",a2);
printf("3:%d \n",a3);
printf("4:%d \n",a4);
printf("5:%d \n",a5);
printf("6:%d \n",a6);
return 0;
}
複製代碼
n(void){
int num=0;
int a2=0,a3=0,a4=0,a5=0,a6=0;
int a7=0,a8=0,a9=0,a10=0,a11=0,a12=0;
int ran1,ran2,max=0,ans;
srand(time(NULL));
printf("tosses a pair of six-sided dice. \n");
while(num<=10000){
ran1 = rand() % 6 + 1;
ran2 = rand() % 6 + 1;
num += 1;
switch(ran1+ran2){
case 2:
a2 += 1;
break;
case 3:
a3 += 1;
break;
case 4:
a4 += 1;
break;
case 5:
a5 += 1;
break;
case 6:
a6 += 1;
break;
case 7:
a7 += 1;
break;
case 8:
a8 += 1;
break;
case 9:
a9 += 1;
break;
case 10:
a10 += 1;
break;
case 11:
a11 += 1;
break;
case 12:
a12 += 1;
break;
}
}
if(a2>max){
max = a2;
ans = 2;
}
if(a3>max){
max = a3;
ans = 3;
}
if(a4>max){
max = a4;
ans = 4;
}
if(a5>max){
max = a5;
ans = 5;
}
if(a6>max){
max = a6;
ans = 6;
}
if(a7>max){
max = a7;
ans = 7;
}
if(a8>max){
max = a8;
ans = 8;
}
if(a9>max){
max = a9;
ans = 9;
}
if(a10>max){
max = a10;
ans = 10;
}
if(a11>max){
max = a11;
ans = 11;
}
if(a12>max){
max = a12;
ans = 12;
}
printf("%d is most likely to thrown",ans);
return 0;
}
複製代碼
圖片附件:
1541586588800.jpg
(2018-11-7 18:36, 138.39 KB) / 下載次數 303
http://smallcat.utmall.com/attachment.php?aid=456&k=261129b8cff61c7b4e1bb01b160fd253&t=1752873364&sid=yNL7YL
作者:
Smallcat
時間:
2018-11-7 18:37
下載
(184.38 KB)
2018-11-7 18:37
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main(void){
int a=0,i;
double ranx,rany,sign1,sign2=0;
srand(time(NULL));
for(i=1;i<=1000;i++){
sign2 = 0;
ranx = (double)rand()/(RAND_MAX+1);
rany = (double)rand()/(RAND_MAX+1);
sign1 = (ranx - 0.5)*(ranx - 0.5) + (rany - 0.5)*(rany - 0.5);
if(sign1 <= 0.25){
sign2 = 1;
}
if(sign2){
a++;
}
}
printf("4*p = %lf",4.0*a/1000);
}
複製代碼
圖片附件:
1541586600825.jpg
(2018-11-7 18:37, 184.38 KB) / 下載次數 4947
http://smallcat.utmall.com/attachment.php?aid=457&k=3c9a2e5869b31dc2b9e1776470802e16&t=1752873364&sid=yNL7YL
作者:
Smallcat
時間:
2018-11-7 18:39
此題不是用原題提供的方法算,是用
這裡的牛頓法
。
下載
(89.97 KB)
2018-11-7 18:38
#include<stdio.h>
int main(void){
int x,i;
double n=1;
double pro;
printf("find out the square root of this input positive integer: ");
scanf("%d",&x);
if(n*n<x){
n *= 3;
}
n /= 3; //n為比x的平方根小的某數
pro = x;
for(i=1;i<=20;i++){ //求更精確可用20以上的數值
n = (n+pro/n)/2; //牛頓法求平方根
}
printf("%lf",n);
}
複製代碼
圖片附件:
1541586800532.jpg
(2018-11-7 18:38, 89.97 KB) / 下載次數 282
http://smallcat.utmall.com/attachment.php?aid=458&k=886de739c0ab9ae4c8981c878abeaef3&t=1752873364&sid=yNL7YL
作者:
Smallcat
時間:
2018-11-7 18:40
下載
(278.73 KB)
2018-11-7 18:39
下載
(122.92 KB)
2018-11-7 18:39
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main(void){
int roca,x,num1=0,numR=0,numL=0; //總步數,R4逃出數,L5逃出數
int sign;
srand(time(NULL));
for(int i=1;i<=1000;i++){
sign=1;
roca=0;
while(roca>-5 && roca<4 && sign){
x = rand() % 2 + 1;
if(x==1){
roca += 1;
num1++;
}else{
roca -= 1;
num1++;
}
if(roca==-2){
roca = -4;
}
if(roca==-5){
sign = 0;
numL++;
}
if(roca==4){
sign = 0;
numR++;
}
}
}
printf("R4:%d L5:%d \n",numR,numL);
printf("the odds exit from R4 is %lf \n",(double)numR/1000);
printf("the odds exit from L5 is %lf \n",(double)numL/1000);
printf("on the average,the dog stay %lf \n",(double)num1/1000);
//case c
printf("\n----------case c-----------\n");
num1=0;
numR=0;
numL=0;
for(int i=1;i<=1000;i++){
sign=1;
roca=0;
while(roca>-5 && roca<4 && sign){
x = rand() % 2 + 1;
if(x==1){
roca += 1;
num1++;
}else{
roca -= 1;
num1++;
}
if(roca==-2 && x==2){ //只有往左時推進
roca = -4;
}
if(roca==-5){
sign = 0;
numL++;
}
if(roca==4){
sign = 0;
numR++;
}
}
}
printf("\nR4:%d L5:%d \n",numR,numL);
printf("the odds exit from R4 is %lf \n",(double)numR/1000);
printf("the odds exit from L5 is %lf \n",(double)numL/1000);
printf("on the average,the dog stay %lf \n",(double)num1/1000);
}
複製代碼
圖片附件:
1541586814124.jpg
(2018-11-7 18:39, 278.73 KB) / 下載次數 254
http://smallcat.utmall.com/attachment.php?aid=459&k=c684abe9b441c168d33197993244f887&t=1752873364&sid=yNL7YL
圖片附件:
1541586849504.jpg
(2018-11-7 18:39, 122.92 KB) / 下載次數 303
http://smallcat.utmall.com/attachment.php?aid=460&k=d8b23c99ce5253e05cbc9ef990203ac2&t=1752873364&sid=yNL7YL
作者:
Smallcat
時間:
2018-11-7 18:40
下載
(149.77 KB)
2018-11-7 18:40
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main(void){
int x,y;
srand(time(NULL));
x = rand()%13 + 1;
y = rand()%4 + 1;
switch(x){ //rank
case 1:
printf("ace of ");
break;
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
printf("%d of ",x);
break;
case 11:
printf("jack of ");
break;
case 12:
printf("queen of ");
break;
case 13:
printf("king of ");
break;
}
switch(y){ //suit
case 1:
printf("clubs");
break;
case 2:
printf("diamond");
break;
case 3:
printf("hearts");
break;
case 4:
printf("spades");
break;
}
}
複製代碼
圖片附件:
1541586906667.jpg
(2018-11-7 18:40, 149.77 KB) / 下載次數 270
http://smallcat.utmall.com/attachment.php?aid=461&k=eb16708dbc35904b97256f5d5f58e856&t=1752873364&sid=yNL7YL
歡迎光臨 小貓貓大聯盟!幻與想的境界\(0w0)/ (http://smallcat.utmall.com/)
Powered by Discuz! 7.2