jeudi 18 novembre 2021

How to add total number of combinations in my code? C#

Here I have the code that solves the following riddle. A farmer goes to a market with 100 bucks. He wants to spend exactly 100 bucks and exactly 100 animals to buy. Sheep cost 8 bucks, chickens cost 3 bucks, rabbits cost only 0.50 bucks each. In the code I have some of the vaild combinations printed out. The problem is now how do I make it show the total number of combinations that have been checked.

using System;


    namespace riddle
    {
    class Program
    {

    static void Main(string[] args)
    {
        int priceSheep = 8;
        int priceChicken = 3;
        float priceRabbit = 0.5F;

        for (int i = 0; i <= 100 / priceSheep; ++i)
        {
            for (int j = 0; j <= ((100 - i * priceSheep) / priceChicken); ++j)
            {
                int money = (100 - priceSheep * i - priceChicken * j);
                if (priceRabbit * (100 - i - j) == money)
                {
                    Console.WriteLine(i + " " + "sheeps" + " " + j + " " + "chickens" + " " + (100 - i - j) + " " + "rabbits");
                }
            }
        }

      }
   }
 }

Current output:

0 sheeps 20 chickens 80 rabbits
1 sheeps 17 chickens 82 rabbits
2 sheeps 14 chickens 84 rabbits
3 sheeps 11 chickens 86 rabbits
4 sheeps 8 chickens 88 rabbits
5 sheeps 5 chickens 90 rabbits
6 sheeps 2 chickens 92 rabbits

What the final output should look like:

0 sheeps 20 chickens 80 rabbits
1 sheeps 17 chickens 82 rabbits
2 sheeps 14 chickens 84 rabbits
3 sheeps 11 chickens 86 rabbits
4 sheeps 8 chickens 88 rabbits
5 sheeps 5 chickens 90 rabbits
6 sheeps 2 chickens 92 rabbits
1030301 combinations checked

Aucun commentaire:

Enregistrer un commentaire