C + + implementation of N-Dice algorithm _c language

Source: Internet
Author: User

This article describes the C + + implementation of N-Dice algorithm, share for everyone for reference. The specific methods are as follows:

Title Requirements: Put n dice still on the ground, all points

The implementation code is as follows:

#include <iostream>

using namespace std;

const int g_maxvalue = 6;
const int number = 6;

int array[(number-1) * g_maxvalue + 1];

void probility (int original, int current, int sum, int *array)
{
 if (current = = 0)
 {
 array[sum-original] ++;
 return;
 }

 for (int i = 1; I <= g_maxvalue i++)
 {
 probility (original, current-1, sum + i, array);
 }
}

void Generatevalue (int *array, int size)
{
 if (array = NULL | | size <= 0) return
 ;

 for (int i = number; I <= number * g_maxvalue; i++)
 {
 Array[i-number] = 0;
 }

 Probility (number, number, 0, array);

 for (int i = 0; i < (number-1) * g_maxvalue + 1; i++)
 {
 cout << array[i] << "";
 }

void Main ()
{
 Generatevalue (array, (number-1) * g_maxvalue + 1);
}

The loop solution is the same as the code above, but slightly different.

The specific implementation code is as follows:

#include <iostream>

using namespace std;

void printprobability (int number)
{
 const int g_maxvalue = 6;

 int *array[2];
 for (int i = 0; i < 2; i++)
 {
 Array[i] = new int[g_maxvalue * number];
 }

 for (int i = 0; i < g_maxvalue * number; i++)
 {
 Array[0][i] = 0;
 Array[1][i] = 0;
 }
 
 int flag = 0;
 for (int i = 0; i < g_maxvalue i++)
 {
 array[flag][i] = 1;
 }

 for (int i = 1; i < number; i++)
 {for
 (int j = 0; J < i; j +)
  array[1-flag][j] = 0;
 for (int j = i; J < (i + 1) * G_maxvalue; j + +)
 {
  array[1-flag][j] = 0;
  for (int k = 1; k <= J && k <= G_maxvalue; k++)
  array[1-flag][j] + = array[flag][j-k]
 ;

 flag = 1-flag;
 }

 for (int i = Number-1 I < g_maxvalue * number; i++)
 {
 cout << array[flag][i] << "";
 }
}

void Main ()
{
 printprobability (2);
}

I hope this article is helpful to the learning of C + + program algorithm design.

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.