- 論壇徽章:
- 0
|
初學(xué)者做習(xí)題
15.設(shè)計一個程序,如以百元鈔付款,應(yīng)找回最少的錢幣個數(shù)50元,10元,5元,1元各為多少?
//設(shè)計一個程序,如以百元鈔付款,應(yīng)找回最少的錢幣個數(shù)50元,10元,5元,1元各位多少?
public class Xiti4q15 {
//方法一:
/*public static void main(String args[]) {
int n = Integer.parseInt(args[0]); // 假設(shè)n是買東西所要付的款
int zhao,wushi,shi,wu; //找零
zhao=100-n;
System.out.println("找您人民幣共計" +zhao +"元");
if (zhao>=50){
{wushi=1;
System.out.println("需要50元人民幣" +wushi +"張");
zhao=zhao-50;}
}
if (zhao>0 &&zhao>=10){
shi=zhao/10;
System.out.println("需要10元人民幣" +shi +"張");
zhao=zhao-(10*shi);
}
if (zhao>0 && zhao>=5){
wu=zhao/5;
System.out.println("需要5元人民幣" +wu +"張");
zhao=zhao-(wu*5);}
if (zhao>0 && zhao>=1)
System.out.println("需要1元人民幣" +zhao +"張");
} */
// 方法二:
public static void main(String args[]) {
int n = Integer.parseInt(args[0]); // 假設(shè)n是買東西所要付的款
int zhao,k; //zhao是 找回零錢的總數(shù),k是找回人民幣的張數(shù)。
int j=0;
int a[]={50,10,5,1};
zhao=100-n;
System.out.println("找您人民幣共計" +zhao +"元");
do {
if (a[j]> zhao)
{j=j+1;
// System.out.println(a[j]);
}
else{
k=zhao/a[j];
System.out.println(a[j]+"元人民幣需要" +k +"張");
zhao=zhao-(a[j]*k);
}
}while (zhao>0);
}}
-----------------------------------------------------------------
##################################################################
16.由命令行輸入一個正整數(shù)n,求下列式子的和。
(a)s=1+1/2+1/3+1/4+...+1/n
(b)s=1-1/2+1/3-1/4+...+1/n
(c)s=1+1/(1*2)+1/(2*3)+...+1/(n*(n+1))
(d)s=1/1!+1/2!+1/3!+...1/n!
/*由命令行輸入一個正整數(shù)n,求下列式子的和。
(a)s=1+1/2+1/3+1/4+...+1/n
(b)s=1-1/2+1/3-1/4+...+1/n
(c)s=1+1/(1*2)+1/(2*3)+...+1/(n*(n+1))
(d)s=1/1!+1/2!+1/3!+...1/n!
*/
public class Xiti4q16 {
public static void main(String args[]) {
// a
int n = Integer.parseInt(args[0]);
double sum_a = 0;
for (double i = 1; i
-----------------------------------------------------------------
###############################################################(未完待續(xù))
本文來自ChinaUnix博客,如果查看原文請點:http://blog.chinaunix.net/u/19637/showart_2153094.html |
|