本文共 2399 字,大约阅读时间需要 7 分钟。
/**1、题目描述
给你六种面额1、5、10、20、50、100元的纸币,假设每种币值的数量都足够多,编写程序求组成N员(N为0-10000的非负整数)的不同组合的个数。 输入描述: 输入为一个数字N,即需要拼凑的面额 输出描述: 输出也是一个数字,为组成N的组合个数。 示例1 输入5
输出2
* * @author */public class Money { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here int[] a={ 1,5,10,20,50,100}; Scanner in=new Scanner(System.in); int m=in.nextInt(); games(a,m); } private static void games(int[] a, int m) { //采用动态规划,将金额分成两份,即当前金额数加上 //大于面额的部分的数(之前已经计算过),依次调用。 long[] b=new long[m+1]; //用Long类型,组合数可能会很大,确保所有用例都通过。 b[0]=1; for(int i=0;i<6;i++){ for(int j=1;j<=m;j++){ if(j>=a[i]){ b[j]=b[j]+b[j-a[i]]; } } } System.out.println(b[m]); }}
import java.math.BigInteger;
import java.util.Scanner;/**
* 2、题目描述 请设计一个算法能够完成两个用字符串存储的整数进行相加操作,对非法的输入则返回error 输入描述: 输入为一行,包含两个字符串,字符串的长度在[1,100]。 输出描述: 输出为一行。合法情况输出相加结果,非法情况输出error 示例1 输入123 123
abd 123 输出246
Error * @author */public class BigNum { /** * BigInteger 任意大的整数,原则上是,只要你的计算机的内存足够大,可以有无限位的 * * BigInteger 任意大的实数,可以处理小数精度问题。 使用心得如下: * * 1,BigInteger属于java.math.BigInteger,因此在每次使用前都要import * 这个类。偶开始就忘记import了,于是总提示找不到提示符。 * * 2,其构造方法有很多,但现在偶用到的有: BigInteger(String val) 将 BigInteger 的十进制字符串表示形式转换为 * BigInteger。 BigInteger(String val, int radix) 将指定基数的 BigInteger * 的字符串表示形式转换为 BigInteger。 如要将int型的2转换为BigInteger型,要写为BigInteger two=new * BigInteger("2"); //注意2双引号不能省略 * * 3,BigInteger类模拟了所有的int型数学操作,如add()==“+”,divide()==“-”等,但注意其内容进行数学运算时不能直接使用数学运算符进行运算,必须使用其内部方法。而且其操作数也必须为BigInteger型。 * 如:two.add(2)就是一种错误的操作,因为2没有变为BigInteger型。 * * 4,当要把计算结果输出时应该使用.toString方法将其转换为10进制的字符串,详细说明如下: String toString() 返回此 * BigInteger 的十进制字符串表示形式。 输出方法:System.out.print(two.toString()); * * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here Scanner in = new Scanner(System.in); try { BigInteger a = in.nextBigInteger(); BigInteger b = in.nextBigInteger(); System.out.println(a.add(b)); } catch (Exception e) { System.out.println("error"); } }}
转载地址:http://pvyhz.baihongyu.com/