Advertisement
realever15

程設練習2-6

Nov 12th, 2014
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.46 KB | None | 0 0
  1. #include<iostream>
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #include<time.h>
  5. using namespace std;
  6. int flip(void);
  7. int main(){
  8.     int ans=0;
  9.     srand(time(NULL));
  10.     for(int i=1;i<=100;i++)
  11.     {
  12.      if(flip()==1)
  13.      {
  14.       ans++;
  15.       cout<<"Heads ";
  16.      }
  17.      else if(flip()==0)
  18.       cout<<"Tails ";
  19.     }
  20.     printf("\n\nThe total number of Heads was %d\n",ans);
  21.     printf("The total number of Tails was %d\n",100-ans);
  22. }
  23. int flip(void){
  24.     int a;
  25.     a = rand()%2;
  26.      return a;
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement