① c語言,編寫模擬翻硬幣得程序。
#include<stdio.h>
int main(void)
{
int n;//讀入一個數,為結束的次數
int i;
int a[10];//10個硬幣
int biaoji = 0;//當這個標記能被3或7整除,說明恰好數了3次或7次
scanf("%d", &n);
for (i = 0; i < 10; i++) //將硬幣都初始化為正面的狀態,即1
a[i] = 1;
biaoji = 1;//因為a[]的下標不允許超過9,故要重置
a[biaoji - 1] = !a[biaoji - 1];//改變硬幣的狀態
for (i = 0; i < 10; i++)
printf("%d", a[i]);
return 0;
}
主要特點
C語言是一種結構化語言,它有著清晰的層次,可按照模塊的方式對程序進行編寫,十分有利於程序的調試,且c語言的處理和表現能力都非常的強大,依靠非常全面的運算符和多樣的數據類型,可以輕易完成各種數據結構的構建,通過指針類型更可對內存直接定址以及對硬體進行直接操作,因此既能夠用於開發系統程序,也可用於開發應用軟體。
② C語言程序題
修改如下,有問題hi我
#include<stdio.h>
#include<time.h>
#include <stdlib.h>
void main()
{
int a=0,b=0,i=0,j=0,sum=0;
srand((unsigned)time(0));
while(j!=3&&j!=-3)
{
i=rand()%2;
if(i==1) a++;//判斷是否相等用==,=是賦值
if(i==0) b++;
j=a-b;
sum++;
printf("%d",i);//這才是列印的正確格式
}
printf("總共拋硬幣次數為%d次\n",sum);
}
③ C語言編程 要求一,輸入拋硬幣次數,輸出該次實驗硬幣正面的概率,要求小數點保留2位. 要求二、輸入拋硬幣次
簡單呀。但是要求二是什麼意思呢?拋硬幣次數不是實驗次數,最好把題目說明白,你可能不知道怎麼輸出保留倆位小數估計printf("%.2f",x);
④ 用c語言模擬拋硬幣的過程
C++:
class Program
{
int neg = 0, pos = 0;
int turn = 0;
public Program(int turn)
{
this.turn = turn;
}
public void start()
{
Random rand = new Random();
for (int i = 0; i < turn; i++)
{
result(rand.Next(0, 2));
}
}
public void result(int i)
{
if (i == 0)
neg++;//正面的次數
else if (i == 1)
pos++;//反面的次數
else
Console.WriteLine("error!");
}
public void print()
{
Console.WriteLine("拋硬幣的次數:{0}", turn);
Console.WriteLine("正面出現的次數:{0}", neg);
Console.WriteLine("正面出現的概率:{0}%", neg * 100 / turn);
Console.WriteLine("反面出現的次數:{0}", pos);
Console.WriteLine("反面出現的概率:{0}%", pos * 100 / turn );
}
public void process()
{
int j;
Console.WriteLine("請輸入1開始拋硬幣");
j = Convert.ToInt32(Console.ReadLine());
if (j == 1)
{
Console.WriteLine("請輸入拋硬幣的次數:");
turn = Convert.ToInt32(Console.ReadLine());
start();
Console.WriteLine("輸入數字2顯示拋擲的結果:");
j = Convert.ToInt32(Console.ReadLine());
}
if (j == 2)
print();
}
static void Main(string[] args)
{
Program play = new Program(0);
play.process();
Console.Read();
}
}
C:
拋10次硬幣構成一個事件,每次事件記錄得到正面的個數。反復模擬這個事件50,000次,然後對這50,000L次進行輸出頻率圖,比較每次事件得到正面次數的比例。
以下是總的代碼:
頭文件 RandomNumber.h:
代碼
// RandomNumber.h
const unsigned long maxshort = 65535L;
const unsigned long multiplier = 1194211693L;
const unsigned long adder = 12345L;
#ifndef RANDOMNUMBER_H
#define RANDOMNUMBER_H
class RandomNumber{
private:
// 當前種子
unsigned long randSeed;
public:
// 構造函數,默認值0表示由系統自動產生種子
RandomNumber(unsigned long s = 0);
// 產生0 ~ n-1之間的隨機整數
unsigned short Random(unsigned long n);
// 產生[0, 1) 之間的隨機實數
double fRandom();
};
#endif
類實現文件RandomNumber.cpp :
代碼
// RandomNumber.cpp
#include "RandomNumber.h"
#include
#include
#include
using namespace std;
// 產生種子
RandomNumber::RandomNumber(unsigned long s)
{
if(s == 0)
randSeed = time(0); //用系統時間產生種子
else
randSeed = s;
}
// 產生0 ~ n-1 之間的隨機整數
unsigned short RandomNumber::Random(unsigned long n)
{
randSeed = multiplier * randSeed + adder;
return (unsigned short)((randSeed >> 16) % n);
}
// 產生[0, 1)之間的隨機實數
double RandomNumber::fRandom()
{
return Random(maxshort) / double(maxshort);
}
主文件Main :
代碼
// 主文件main
/*
* Author: Tanky woo
* Blog: www.WuTianQi.com
* Date: 2010.12.7
* 代碼來至《計算機演算法設計與分析》
*/
#include "RandomNumber.h"
#include
#include
#include
using namespace std;
int TossCoins(int numberCoins)
{
// 隨機拋硬幣
static RandomNumber coinToss;
int i, tosses = 0;
for(i = 0; i < numberCoins; ++i)
tosses += coinToss.Random(2);
return tosses;
}
int main()
{
// 模擬隨機拋硬幣事件
const int NCOINS = 10;
const long NTOSSES = 50000L;
// heads[i]得到的i次正面的次數
long i, heads[NCOINS+1];
int j, position;
// 初始化數組heads
for(j = 0; j < NCOINS+1; ++j)
heads[j] = 0;
// 重復50,000次模擬事件
for(i = 0; i < NTOSSES; ++i)
heads[TossCoins(NCOINS)] ++;
// 輸出頻率圖
for(i = 0; i <= NCOINS; ++i)
{
position = int (float(heads[i]) / NTOSSES*72);
cout << setw(6) << i << " ";
for(j = 0; j< p>
cout << " ";
cout << "*" << endl;
}
return 0;
}
( 輸出頻率圖:)
⑤ 用C語言編寫一個程序,求擲硬幣1000次,至少有一次連續出現10次正面的概率。或者講講演算法。
有兩種辦法:
第一種就是用隨機數進行模擬,然後進行至少100輪的模擬,取平均數
第二種就是用公式,根據概率學進行公式推導,然後使用C語言進行計算。
第一種並不能很准確,但是有說服力,模擬輪數越多越趨向於准確
第二種准確並有說服力,但是需要理論支持去計算。
公式的話:
全部可能出現的排列,是2的1000次方
特定位置可能出現滿足的排列,比如前10個都是正面,剩下的隨便排列,就是2的990次方
特定位置數,一共是991個
所以,幾率就是:991 * 2^990 / 2^1000
(好久沒有算過了,可能不太對,但是思路差不多,你好好想想吧)
因為,前11個都是正面的,同時滿足第一個和第二個位置的,是重復,需要排除一個,所以還需要額外考慮排除,需要進行處理一下……實際比上述的幾率小,具體公式仍需推導……非專業人員幫不到你
⑥ c++模仿十次拋硬幣的隨機結果,代碼有些不明白
for 的最後有全形字元! 最後的)改成)就好了
⑦ 用c語言模擬拋硬幣的試驗
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#defineINT_MAX100
voidmain()
{
intn,zhengsum=0,fansum=0;
srand(time(NULL));
scanf("%d",&n);
while(n--)
{
if(rand()%INT_MAX*1.0/INT_MAX>0.5)
zhengsum++;
else
fansum++;
}
printf("正面數:%d反面數:%d ",zhengsum,fansum);
}
⑧ C語言拋兩個硬幣,都是正面我贏,都是反面你贏,一正一反重來。這個代碼怎麼打
代碼可以這樣打:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int f=1,n;
srand (time(NULL));
while(f)
{
f=0;
n=rand()%2+rand()%2;
if(n==2)
printf("都是正面,我贏了 ");
else if(n==0)
printf("都是反面,你贏了 ");
else
{
printf("一正一反,重來 ");
f=1;
}
}
return 0;
}
這是運行結果的截圖:
4
⑨ 硬幣游戲:寫一個程序模擬反復拋硬幣,直到連續出現三次正面或反面為止,此時你的程序應該顯示拋硬幣的總次
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
int count_z = 0,count_s = 0,count_x = 0;
int flag;
srand((unsigned int)time(NULL));//以時間作為種子。
while(count_s < 3 && count_x < 3)
{
flag = rand()%2;//產生0和1兩個隨機數,作為正反面。
if(flag)
{
count_x++;
count_s = 0;
printf("國徽向下 !\n");
}
else
{
count_s++;
count_x = 0;
printf("國徽向上 !\n");
}
count_z++;
}
printf("========================================================\n");
printf("總共拋了 %d 次!\n", count_z);
system("pause");
return 0;
}