110,535
社区成员
发帖
与我相关
我的任务
分享
class Class1
{
public static void Main(string[] args)
{
PrintHowToDivide(100);
System.Console.ReadKey();
}
public static void PrintHowToDivide(int value)
{
int temp = value;
int five = value / 5;
value %= 5;
int two = value / 2;
value %= 2;
int one = value;
PrintHowToDivide(temp, five, two, one);
}
/*
* value为需要分解的钱,num_five, num_two, num_one 分别为当前的 5 2 1块的张数
*
* */
private static void PrintHowToDivide(int value, int num_five, int num_two, int num_one)
{
System.Console.WriteLine(" {0} 块钱有如下分法: {1} 张 5 元,{2} 张 2 元,{3} 张 1 元", value, num_five, num_two, num_one);
if (num_one == value) return;
if (num_two > 0)
{
PrintHowToDivide(value, num_five, num_two - 1, num_one + 2);
}
else if (num_five > 0)
{
PrintHowToDivide(value, num_five - 1, num_two + (num_one + 5) / 2, (num_one + 5) % 2);
}
}
}
class Class1
{
public static void Main(string[] args)
{
PrintHowToDivide(100);
System.Console.ReadKey();
}
public static void PrintHowToDivide(int value)
{
int temp = value;
int five = value / 5;
value %= 5;
int two = value / 2;
value %= 2;
int one = value;
PrintHowToDivide(temp, five, two, one);
}
/*
* value为需要分解的钱,num_five, num_two, num_one 分别为当前的 5 2 1块的张数
*
* */
private static void PrintHowToDivide(int value, int num_five, int num_two, int num_one)
{
System.Console.WriteLine(" {0} 块钱有如下分法: {1} 张 5 元,{2} 张 2 元,{3} 张 1 元", value, num_five, num_two, num_one);
if (num_one == value) return;
if (num_two > 0)
{
PrintHowToDivide(value, num_five, num_two - 1, num_one + 2);
}
else if (num_five > 0)
{
if (num_one > 0)
{
PrintHowToDivide(value, num_five-1, num_two + 3, num_one - 1);
}
else
{
PrintHowToDivide(value, num_five-1, num_two + 2, num_one + 1);
}
}
}
}
class Class1
{
public static void Main(string[] args)
{
PrintHowToDivide(100);
System.Console.ReadKey();
}
public static void PrintHowToDivide(int value)
{
int temp = value;
int five = value / 5;
value %= 5;
int two = value / 2;
value %= 2;
int one = value;
PrintHowToDivide(temp, five, two, one);
}
/*
* value为需要分解的钱,num_five, num_two, num_one 分别为当前的 5 2 1块的张数
*
* */
private static void PrintHowToDivide(int value, int num_five, int num_two, int num_one)
{
System.Console.WriteLine(" {0} 块钱有如下分法: {1} 张 1 元,{2} 张 2 元,{3} 张 5 元",value,num_one,num_two,num_five);
if (num_one == value) return;
if (num_five > 0)
{
if (num_one > 0)
{
PrintHowToDivide(value, num_five - 1, num_two + 3, num_one - 1);
}
else
{
PrintHowToDivide(value, num_five - 1, num_two + 2, num_one + 1);
}
}
else if(num_two > 0)
{
PrintHowToDivide(value, num_five, num_two - 1, num_one + 2);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections;
using System.Xml.Linq;
namespace ConsoleCSharp
{
class Program
{
static void Main(string[] args)
{
int[] arrInt = new int[] { 1, 2, 5 };
count(100 , arrInt,"");
}
public static void count(int n, int[] arr,string s)
{
if (n < 0)
return;
if (n == 0)
{
Console.WriteLine(s);
}
for (int i = 0; i <arr.Length; i++)
{
count(n - arr[i], arr,s + arr[i]);
}
}
}
}
for (int i = 0; i <= 20; i++)
{
for (int j = 0; j <= (100 - 5 * i) / 2;j++ )
{
for (int k = 0; k <= 100 - 5 * i - 2 * j; k++)
{
if (5 * i + 2 * j + k == 100)
{
Console.WriteLine(i + " " + j + " " + k);
}
}
}
}
//f(n) = f(n-5) + f(n-2) + f(n-1);
//任何数量 n 的钱,可有有3种方式累加到
//1. 由 n-5 的钱 加 5 块得到(大于5时)
//2. 由 n-2 的钱 加 2 块得到(大于2时)
//3. 由 n-1 的钱 加 1 块得到(大于1时)
//如果这里直接用递归的话,效率比较低,当参数比较大时也容易溢出
public static int GetTheNum(int value)
{
int[] nums = {1,2,3,5,8};//这里投机了下 先把1-5块的方式初始化了
if (value < 1) return 0;
if(value < 6) return nums[value-1];
int temp = 0;
while (value - 5 > 0)
{
temp = nums[4] + nums[3] + nums[0];
for (int i = 0; i < 4; i++)
{
nums[i] = nums[i + 1];
}
nums[4] = temp;
value--;
}
return temp;
}