Two ways to throw a dice problem

Source: Internet
Author: User

About the two ways to throw the dice, just and a classmate chatting up,

I just made it. Two methods, all very simple!

In this record,

The first method is entered once each time:

#include <stdio.h>

#define N 5

int main () {

Define a variable to hold the number of dice

int num;

Define the number variables that appear

int count_1=0, count_2=0, count_3=0, count_4=0, count_5=0, count_6=0;

for (int i=1; i<=n; i++) {

printf ("Please enter the number of the dice to be thrown at%d times: \ n", i);

scanf ("%d", &num);

if (num==1) {

count_1++;

}else if (num==2) {

count_2++;

}else if (num==3) {

count_3++;

}else if (num==4) {

count_4++;

}else if (num==5) {

count_5++;

}else if (num==6) {

count_6++;

}//here because you entered it once, so you don't need to judge not 1-6,

}

printf ("1 occurrences of%d\n2 occurrences of the number of occurrences of the number of times%d\n3 appears%d\n4 the number of occurrences of the number of%d\n5 occurrences%d\n6 the number of occurrences%d\n", Count_1,count_2,count_3,count_4, Count_5,count_6);

return 0;

}

The second method, with random numbers:

#include <stdio.h>

#include <stdlib.h>

#define N 100

int main () {

int count_1=0, count_2=0, count_3=0, count_4=0, count_5=0, count_6=0;

for (int i=1; i<=n; i++) {

int num= Arc4random_uniform (6) +1; (N) The random 0~n-1,

if (num==1) {

count_1++;

}else if (num==2) {

count_2++;

}else if (num==3) {

count_3++;

}else if (num==4) {

count_4++;

}else if (num==5) {

count_5++;

}else if (num==6) {

count_6++;

}else{

i--;

Continue

}

}

printf ("1 occurrences of%d\n2 occurrences of the number of occurrences of the number of times%d\n3 appears%d\n4 the number of occurrences of the number of%d\n5 occurrences%d\n6 the number of occurrences%d\n", Count_1,count_2,count_3,count_4, Count_5,count_6);

return 0;

}

Two ways to throw a dice problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.