求一个小编程题答案,今天面试遇到的!在线等~~~~

gpz520 2009-03-13 06:23:04
题目是这样的:现在有一种货币,由C1,C2,C3...Cn元组成,现在请写出一个程序求出用最少的货币组成K元!
为了大家明白举个例子吧:比如这组货币由1元2元5元组成,求用最少的货币数量组成6元,6元=5元+1元;而不是6元=2元+2元+2元,更不是6个1元组成!
...全文
78 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
aruaru77 2009-03-13
  • 打赏
  • 举报
回复



using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{

class Program
{
static void Main(string[] args)
{
int totalValue=Convert.ToInt32(Console.ReadLine());

int amountOfFive = 0;
int amountOfTwo = 0;
int amountOfOne = 0;

amountOfFive = totalValue / 5;

int amountButFive = totalValue % 5;

if(amountButFive!=0)
{
amountOfTwo = amountButFive / 2;

int amountButTwo = amountButFive % 2;

if (amountButTwo != 0)
{
amountOfOne = 1;
}
}

Console.WriteLine("{0}个5;{1}个2;{2}个1", amountOfFive, amountOfTwo, amountOfOne);
Console.ReadKey();

}
}
}
gpz520 2009-03-13
  • 打赏
  • 举报
回复
嗯 好强悍!
delphiPB 2009-03-13
  • 打赏
  • 举报
回复
关注中。
xiaoyuelinxi 2009-03-13
  • 打赏
  • 举报
回复
package net.moon;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
/**没有注意速率什么的,大概把一个解法给你。其实方法很多
*/
public class MathSourse {
public static void main(String[] args){
int[] dolls = {1,2,5,10,20,50,100,500,1000};//当然这里只要是数字什么的都可以,你可以自己搞随机的算法
int result = 5343;//你要的K
ArrayList minResult = MathSourse.metch(dolls,result);
Iterator iterator = minResult.iterator();//用什么放随便啦
while(iterator.hasNext()){
System.out.println(String.valueOf(iterator.next()));//就打印下吧
}
}
public static ArrayList metch(int[] dolls,int result){
Arrays.sort(dolls);
ArrayList re = new ArrayList();
re = MathSourse.getMaxNum(re,dolls,result);
return re;
}
//递归下就可以了
public static ArrayList getMaxNum(ArrayList re,int[] dolls,int result){
if(result==0){
return re;
}
int temp = 0;
for(int i =dolls.length-1;i>=0;i--){
if(dolls[i]<=result){
temp=dolls[i];
break;
}
}
result = result - temp;
re.add(temp);
return getMaxNum(re,dolls,result);
}
}
混沌君子 2009-03-13
  • 打赏
  • 举报
回复
public static void compition(int pos,int value) { 
int[] array = {100,50,20,10,5,2,1};
if(value==array[pos]) {
System.out.println(value);
} else if(value>array[pos]) {
System.out.print(array[pos]+" ");
compition(pos+1, value-array[pos]);
} else if(value<array[pos]) {
compition(pos+1, value);
}
}
xiaoyuelinxi 2009-03-13
  • 打赏
  • 举报
回复
哎呀。学校的时候最喜欢玩这些东西啦
有空写着玩。
写不出来不要打我啦(*^__^*) 嘻嘻……
Asinzy 2009-03-13
  • 打赏
  • 举报
回复
关注一下,思路

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧