java硬幣(java硬幣找零問題)

本文目錄一覽:

java投硬幣題目

硬幣面枚舉:

public enum CoinSide {

    HEAD(0), TAIL(1);

    private int value;

    CoinSide(int value) {

        this.value = value;

    }

}

2.硬幣類

import java.util.Random;

public class Coin {

    private String name;

    private CoinSide coinSide;

    public Coin(String name) {

        this.name = name;

    }

    public CoinSide getCoinSide() {

        return coinSide;

    }

    public void setCoinSide(CoinSide coinSide) {

        this.coinSide = coinSide;

    }

    public CoinSide tossCoin() {

        Random random = new Random();

        int value = random.nextInt(2);

        if (value == 0) {

            this.coinSide = CoinSide.HEAD;

        } else {

            this.coinSide = CoinSide.TAIL;

        }

        this.printCoinSide();

        return this.coinSide;

    }

    public void printCoinSide() {

        System.out.println(this.name + ” toss ” + this.coinSide.name());

    }

}

3.主類

public class Main {

    public static void main(String[] args) {

        Coin coin1 = new Coin(“coin1”);

        Coin coin2 = new Coin(“coin2”);

        while (true) {

            coin1.tossCoin();

            coin2.tossCoin();

            if (coin1.getCoinSide() == coin2.getCoinSide()  coin1.getCoinSide() == CoinSide.TAIL) {

                break;

            }

            System.out.println(“——————-“);

        }

    }

}

運行演示:

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編寫:計算一元錢硬幣有多少種表達方

import java.util.ArrayList;

/*

 * 一元錢硬幣有多少種表達方式 

 * 可供選擇:1分,2分,5分,1角,2角,5角,1元

 * 如:

 * 1元=1元;

 * 1元=5角+5角;

 * …. 

 */

public class Test {

 private static int count;

 public static void main(String args[]){

 int max = 100;//一元

 int[] cents = {100,50,20,10,5,2,1};//幣值

 String[] money = {“1元”,”5角”,”2角”,”1角”,”5分”,”2分”,”1分”};

 ArrayList collect = new ArrayList();

 collectMoney(cents, money,0, max, 0, collect);

 System.out.println(“總共有”+count+”種搭配方法!”);

 }

 public static void collectMoney(int[] cents,String[] money,int beginIndex,int max,int result,ArrayList collect){

 if(resultgt;=max){

 if(result==max){

 count++;

 System.out.print(“1元=”);

 for(int i=0;ilt;collect.size();i++){

 System.out.print(money[(Integer) collect.get(i)]);

 if(ilt;collect.size()-1){

 System.out.print(“+”);

 }

 }

 System.out.println();

 }

 return;

 }

 for(int i=beginIndex;ilt;cents.length;i++){

 int cent = cents[i];

 collect.add(i);

 collectMoney(cents, money,i, max, result+cent, collect);

 collect.remove(Integer.valueOf(i));

 }

 }

}

Java 拋擲硬幣,H代表正面朝上,T代表反面朝上,用迭代法編寫程序

JAVA練習之——拋硬幣

import java.util.*;

import java.io.*;

interface game{

String get(int a);

}

interface gamefactory{

game getgame();

}

class yingbi implements game{

public String get(int a){

if(a==0) return “正面”;

else return “反面”;

}

}

class yingbifactory implements gamefactory{

public game getgame(){

return new yingbi();

}

}

class shazi implements game{

public String get(int a){

return String.valueOf(a+1);

}}

class shaizifactory implements gamefactory{

public game getgame(){return new shazi(){

public void a(){System.out.println(“new shazi()”);

}

};}

}

public class Game {

public static void games(gamefactory factory,int a){

game s1=factory.getgame();

System.out.println(s1.get(a));

}

public static void print(ArrayListString e){

for(String y:e)

System.out.print(y);

}

/**

* @param args

*/

public static void main(String[] args) throws IOException{

DataInputStream in=new DataInputStream(System.in);

while(true){

System.out.println(“請選擇1:開始”);

System.out.println(“1:拋硬幣”);

int opinion=Integer.parseInt(in.readLine());

switch(opinion){

case 1:

System.out.println(“開始?Y/N”);

String a=in.readLine();

ArrayListString yin=new ArrayListString();

while(a.equals(“Y”)||a.equals(“y”)){

Random rand=new Random();

yingbifactory q=new yingbifactory();

int w=rand.nextInt(2);

yin.add(q.getgame().get(w));

games(q,w);

a=in.readLine();

}

print(yin);

System.out.println();

break;

default:

System.exit(0);

}

// TODO 自動生成方法存根

}

}

}

關於java拋硬幣的一個對象。

package test;

public class Coin{

private final int HEADS=0;//正面

private final int TAILS=1;//反面

private int face;//存儲flip方法得到的隨機值

public Coin()//調用硬幣這個方法

{

flip();

}

public void flip()//硬幣發放中的拋硬幣方法

{

face=(int) (Math.random()*2);// Math.random()會得到0到1之間的數,乘2得到0到2之間的小數,因為強制轉換int類型所以,小於1的小數轉換成了0,同理大於等於1小於2的轉換成了1.代表了正反面

}

public boolean isHeads()//判斷是否正面,未調用過,可能在其他類會用到判斷方法。

{

return (face==HEADS); //返回布爾類型值

}

public String toString()//將正反面的0,1轉換成字元串的方法

{

String faceName;

if(face==HEADS)//之前得到了face整數0或1,當face等於HEADS即等於0時,將faceName賦值為Heads

faceName=”Heads”;

else

faceName=”Tails”;//同上

return faceName;

}

}

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);

}

}

}

}

好久沒寫這種的,寫了好久。如有不對,歡迎指正!

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/183022.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-11-24 16:25
下一篇 2024-11-24 16:26

相關推薦

  • java client.getacsresponse 編譯報錯解決方法

    java client.getacsresponse 編譯報錯是Java編程過程中常見的錯誤,常見的原因是代碼的語法錯誤、類庫依賴問題和編譯環境的配置問題。下面將從多個方面進行分析…

    編程 2025-04-29
  • Python官網中文版:解決你的編程問題

    Python是一種高級編程語言,它可以用於Web開發、科學計算、人工智慧等領域。Python官網中文版提供了全面的資源和教程,可以幫助你入門學習和進一步提高編程技能。 一、Pyth…

    編程 2025-04-29
  • Java JsonPath 效率優化指南

    本篇文章將深入探討Java JsonPath的效率問題,並提供一些優化方案。 一、JsonPath 簡介 JsonPath是一個可用於從JSON數據中獲取信息的庫。它提供了一種DS…

    編程 2025-04-29
  • Java騰訊雲音視頻對接

    本文旨在從多個方面詳細闡述Java騰訊雲音視頻對接,提供完整的代碼示例。 一、騰訊雲音視頻介紹 騰訊雲音視頻服務(Cloud Tencent Real-Time Communica…

    編程 2025-04-29
  • Java Bean載入過程

    Java Bean載入過程涉及到類載入器、反射機制和Java虛擬機的執行過程。在本文中,將從這三個方面詳細闡述Java Bean載入的過程。 一、類載入器 類載入器是Java虛擬機…

    編程 2025-04-29
  • 如何解決WPS保存提示會導致宏不可用的問題

    如果您使用過WPS,可能會碰到在保存的時候提示「文件中含有宏,保存將導致宏不可用」的問題。這個問題是因為WPS在默認情況下不允許保存帶有宏的文件,為了解決這個問題,本篇文章將從多個…

    編程 2025-04-29
  • Java Milvus SearchParam withoutFields用法介紹

    本文將詳細介紹Java Milvus SearchParam withoutFields的相關知識和用法。 一、什麼是Java Milvus SearchParam without…

    編程 2025-04-29
  • Java 8中某一周的周一

    Java 8是Java語言中的一個版本,於2014年3月18日發布。本文將從多個方面對Java 8中某一周的周一進行詳細的闡述。 一、數組處理 Java 8新特性之一是Stream…

    編程 2025-04-29
  • Java判斷字元串是否存在多個

    本文將從以下幾個方面詳細闡述如何使用Java判斷一個字元串中是否存在多個指定字元: 一、字元串遍歷 字元串是Java編程中非常重要的一種數據類型。要判斷字元串中是否存在多個指定字元…

    編程 2025-04-29
  • VSCode為什麼無法運行Java

    解答:VSCode無法運行Java是因為默認情況下,VSCode並沒有集成Java運行環境,需要手動添加Java運行環境或安裝相關插件才能實現Java代碼的編寫、調試和運行。 一、…

    編程 2025-04-29

發表回復

登錄後才能評論