例1:one dice
/*
sum-1即可找到對應的編號
count[0]=存放1共出現幾次--->count[1-1]
count[1]=存放2共出現幾次--->count[2-1]
count[2]=存放3共出現幾次--->count[3-1]
count[3]=存放4共出現幾次--->count[4-1]
count[4]=存放5共出現幾次--->count[5-1]
count[5]=存放6共出現幾次--->count[6-1]
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, char *argv[])
{
srand( time(0));
int counter[6]={0};//要統計dice用
int i;
for(i=0;i<=100000;i++)//i代表次數
{
int dice1 =rand()%6+1;
int sum=dice1;
counter[sum-1]++;//最小是2,最大是12 ,共有11個數字
}//counter=counter[sum-2]+1
for(i=1;i<=6;i++)//i代表點數和
{
printf("%d: %d\n",i,counter[i-1]);
//印出2~12,存在一維陣列count[i-2]=對應的值仙出來
}
return 0;
}
例2:two dice
/*
sum-2即可找到對應的編號
count[0]=存放2共出現幾次--->count[2-2]
count[1]=存放3共出現幾次--->count[3-2]
count[2]=存放4共出現幾次--->count[4-2]
count[3]=存放5共出現幾次--->count[5-2]
count[4]=存放6共出現幾次--->count[6-2]
count[5]=存放7共出現幾次--->count[7-2]
count[6]=存放8共出現幾次--->count[8-2]
count[7]=存放9共出現幾次--->count[9-2]
count[8]=存放10共出現幾次-->count[10-2]
count[9]=存放11共出現幾次-->count[11-2]
count[10]=存放12共出現幾次->count[12-2]
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, char *argv[])
{
srand( time(0));
int counter[11]={0};
int i;
for(i=0;i<=100000;i++)//i代表次數
{
int dice1 =rand()%6+1;
int dice2 =rand()%6+1;
int sum=dice1+dice2;
counter[sum-2]++;//最小是2,最大是12 ,共有11個數字
}//counter=counter[sum-2]+1
for(i=2;i<=12;i++)//i代表點數和
{
printf("%d: %d\n",i,counter[i-2]);
//印出2~12,存在一維陣列count[i-2]=對應的值仙出來
}
return 0;
}
2020年2月10日 星期一
訂閱:
張貼留言 (Atom)
algorithm
#include <iostream> #include <string.h> using namespace std; int main(int argc, char** argv) { for(int j=2;j<=100;j++)//j...
-
本文旨在補充《 超圖解Arduino互動設計入門 》第18章「RFID無線識別裝置與問答遊戲製作」單元,書本採用的RFID讀卡機模組是採用9600bps, TTL序列通訊介面,RFID的通訊頻率為125KHz。這種模組的接線和程式都很簡單,每當感測到RFID卡,讀卡機就把...
-
3.Keil C 幫我們寫好 程式:#include <reg51.h> vs c語言 #include <stdio.h> //幫您宣告 P0=0x80 有相對的記憶體以十六進位表示,它定義定reg51.h,我們直接拿來用他的代碼即可 P1=0...
-
四位數七段顯示器 如果不想要自行接線路,那麼市面上有已經銜接好的二合一、四合一等各種位數的七段顯示器,以四合一的七段顯示器為例,由於每個七段顯示器會需要 8 個腳位控制其上的 LED,再加上各 4 個共陰或共...
沒有留言:
張貼留言