关于coinjava的信息
本篇文章给大家谈谈coinjava,以及对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、Java硬币题?
- 2、java 硬币1,1,2,2,4,4,8,8.支付n元有多少种方法 java
- 3、java中public coin()是什么意思
- 4、参考文件Coin.java ,写一程序查找在100次掷硬币过程中连续heads面朝上的次数。
Java硬币题?
//硬币类
public class Coin {
private int head = 1;
private int tail = 0;
private int result;
public int getResult() {
return result;
}
public void setResult(int result) {
this.result = result;
}
public Coin(){
result = 1;
}
public void throwCoin(){
int random = (int) (Math.random() * 10 / 5);
if(head == random){
result = head;
}else{
result = tail;
}
}
public int getHead() {
return head;
}
public void setHead(int head) {
this.head = head;
}
public int getTail() {
return tail;
}
public void setTail(int tail) {
this.tail = tail;
}
}
// 测试类
public class Test {
public static void main(String[] args) {
Coin coin = new Coin();
coin.throwCoin();
if(coin.getResult() == coin.getHead()){
System.out.println("You Win.");
}else if(coin.getResult() == coin.getTail()){
System.out.println("You Lose.");
}
}
}
java 硬币1,1,2,2,4,4,8,8.支付n元有多少种方法 java
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map.Entry;
import java.util.Scanner;
public class Test {
static HashSetInteger[] resultSet = new HashSetInteger[]();//存放结果
static HashMapInteger, Integer coinsMap;//将硬币分类 种类——数量
static int max = 0;
public static void main(String[] args) throws IOException {
int coins[] = {1,1,2,2,4,4,8,8};//硬币集合
// Scanner sc = new Scanner(System.in);
// int n = sc.nextInt();//输入n的值
int n = 16;
count(coins, n);
System.out.println();
System.out.println(resultSet.size());
/* 输出结果 每种硬币的数量*/
for(Integer[] ints : resultSet){
int i = 0;
for(EntryInteger, Integer entry : coinsMap.entrySet()){
System.out.println("coins:" + entry.getKey() + " num:" + ints[i++] );
}
System.out.println();
}
}
/**
* 将硬币归类并统计每类的数量
* @param coins
* @param n
* @throws IOException
*/
public static void count(int[] coins, int n) throws IOException{
coinsMap = new HashMapInteger,Integer();
for(int i = 0;i coins.length;i ++){
int count = 0;
if(coinsMap.containsKey(coins[i])){
count = coinsMap.get(coins[i]);
}
++ count;
max += coins[i];
coinsMap.put(coins[i], count);
}
Integer[][] pool = new Integer[coinsMap.size()][2];
int index = 0;
for(EntryInteger, Integer entry: coinsMap.entrySet()){
pool[index][0] = entry.getKey();
pool[index][1] = entry.getValue();
index ++;
}
if(n max){
System.out.println("超过所以硬币总额");
}
else{
recursive(pool, 0, 0, n,new Integer[pool.length]);
}
}
/**
* 利用递归的方式求硬币的组合
* @param pool
* @param coin
* @param sum
* @param n
* @param result
* @throws IOException
*/
public static void recursive(Integer[][] pool,int coin,int sum,int n,Integer result[]) throws IOException{
if(coin == pool.length){//已经考虑了所以种类的硬币时,结束递归
return;
}
int tSum = sum;
for(int i = 0;i = pool[coin][1];i ++){
int temp = i * pool[coin][0];
tSum = sum + temp;
Integer[] _result = new Integer[pool.length];
for(int j = 0; j coin;j ++){
_result[j] = result[j];
}
_result[coin] = i;
// for(int k = 0;k coin;k++) System.out.print(" ");
// System.out.println("coin:"+pool[coin][0] + " num:"+ i + " " + sum + "_" + tSum + " " );
if(tSum == n coin == pool.length - 1){//已经考虑到最后一种硬币类型且硬币组合的面值与n相等
resultSet.add(_result);
// System.out.println("here is an answer");
}
else{
recursive(pool, coin + 1, tSum, n, _result);
}
}
}
}
好久没写这种的,写了好久。如有不对,欢迎指正!
java中public coin()是什么意思
多半是构造方法吧,不过这方法不需要写,默认都会有无参的构造器的
参考文件Coin.java ,写一程序查找在100次掷硬币过程中连续heads面朝上的次数。
public static void main(String[] args) {
final int FLIPS = 100; // number of coin flips
int currentRun = 0; // length of the current run of HEADS
int maxRun = 0; // length of the maximum run so far
// Create a coin object
Coin coin = new Coin();
// Flip the coin FLIPS times
for (int i = 0; i FLIPS; i++) {
// Flip the coin print the result
coin.flip();
if(coin.isHeads()){
currentRun++;
}else{
currentRun=0;
}
// Update the run information
if(currentRunmaxRun) maxRun = currentRun;
}
// Print the results
System.out.print("maxRun="+maxRun);
}
关于coinjava和的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
发布于:2022-12-28,除非注明,否则均为
原创文章,转载请注明出处。