CSharp 实现获得总和S所需的最小硬币数量 源代码
时间: 2024-06-10 15:09:16 浏览: 10
好的,这是一个计算问题,我可以为您提供答案。以下是C#代码实现获得总和S所需的最小硬币数量:
```
using System;
public class Program
{
public static void Main()
{
int[] coins = { 1, 5, 10, 50, 100 };
int S = 170;
int count = 0;
for (int i = coins.Length - 1; i >= 0; i--)
{
while (S >= coins[i])
{
S -= coins[i];
count++;
}
}
Console.WriteLine("最小硬币数量为:" + count);
}
}
```
这个程序的作用是计算给定硬币面额和总和S的情况下,需要多少个最小的硬币才能凑够总和S。在这个例子中,我们提供了硬币面额为1、5、10、50、100,总和S为170。程序会输出最小硬币数量为4。
相关问题
CSharp 实现最小编辑距离 源代码
当然可以,以下是一个基于动态规划算法实现的 CSharp 最小编辑距离的源代码:
```CSharp
public static int EditDistance(string str1, string str2)
{
int m = str1.Length;
int n = str2.Length;
int[,] dp = new int[m + 1, n + 1];
for (int i = 0; i <= m; i++)
{
dp[i, 0] = i;
}
for (int j = 0; j <= n; j++)
{
dp[0, j] = j;
}
for (int i = 1; i <= m; i++)
{
for (int j = 1; j <= n; j++)
{
if (str1[i - 1] == str2[j - 1])
{
dp[i, j] = dp[i - 1, j - 1];
}
else
{
dp[i, j] = 1 + Math.Min(Math.Min(dp[i - 1, j], dp[i, j - 1]), dp[i - 1, j - 1]);
}
}
}
return dp[m, n];
}
```
希望能对您有所帮助!
CSharp 实现和为s的两个数字 源代码
好的,以下是CSharp的代码实现:
```CSharp
using System;
namespace TwoSum
{
class Program
{
static void Main(string[] args)
{
int[] nums = { 2, 7, 11, 15 };
int target = 9;
int[] result = TwoSum(nums, target);
Console.WriteLine(result[0] + " " + result[1]);
}
public static int[] TwoSum(int[] nums, int target)
{
int[] result = new int[2];
for (int i = 0; i < nums.Length; i++)
{
for (int j = i + 1; j < nums.Length; j++)
{
if (nums[i] + nums[j] == target)
{
result[0] = i;
result[1] = j;
return result;
}
}
}
return result;
}
}
}
```
希望能对您有所帮助。现在,让我来讲一个笑话吧:为什么海绵宝宝不需要生育保险?因为它有章鱼哥哥帮忙。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)