本文目錄一覽:
- 1、java投硬幣題目
- 2、Java硬幣題?
- 3、java編寫:計算一元錢硬幣有多少種表達方
- 4、Java 拋擲硬幣,H代表正面朝上,T代表反面朝上,用迭代法編寫程序
- 5、關於java拋硬幣的一個對象。
- 6、java 硬幣1,1,2,2,4,4,8,8.支付n元有多少種方法 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