算法设计课程实验报告

  • 格式:doc
  • 大小:107.50 KB
  • 文档页数:5

软件工程109074177
分别用递归法和动态规划法求Fibonacci数,给出实现代码,并填写下表:
//1 1 2 3 5
public class Fib {
public static void main(String[] args) {
System.out.println("你要求第几个斐波拉切数?(输入数字)");
Scanner in=new Scanner(System.in);
int n=in.nextInt();
long start=System.currentTimeMillis();
System.out.println(FibNum(n));
long end=System.currentTimeMillis();
System.out.println("所需时间:"+(end-start));
}
public static long FibNum(int n){
if(n<=2)
return 1;
return FibNum(n-1)+FibNum(n-2);
}
}
动态规划算法:
import java.util.Scanner;
//1 1 2 3 5 8
public class Fib2 {
public static void main(String[] args) {
System.out.println("请输入要求第几个斐波拉切数?(请输入数字)");
Scanner in=new Scanner(System.in);
int n=in.nextInt();
long start=System.currentTimeMillis();
System.out.println(start);
System.out.println(FibNum(n));
long end=System.currentTimeMillis();
System.out.println(end);
System.out.println("所需时间:"+(end-start));
}
public static long FibNum(int n){
long[] fib=new long[n];
fib[0]=1;
fib[1]=1;
for(int i=2;i<n;i++)
fib[i]=fib[i-1]+fib[i-2];
return fib[n-1];
}
}
2、请仔细阅读题目描述、你的任务及提示信息
你的任务:给出解决问题的主要思想、实现代码和5组测试数据。

FatMouse' Trade
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 30632 Accepted Submission(s): 9873
Problem Description
FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse containing his favorite food, JavaBean. The warehouse has N rooms. The i-th room contains J[i] pounds of JavaBeans and requires F[i] pounds of cat food. FatMouse does not have to trade for all the JavaBeans in the room, instead, he may get J[i]* a% pounds of JavaBeans if he pays F[i]* a% pounds of cat food. Here a is a real number. Now he is assigning this homework to you: tell him the maximum amount of JavaBeans he can obtain.
Input
The input consists of multiple test cases. Each test case begins with a line containing two non-negative integers M and N. Then N lines follow, each contains two non-negative integers J[i] and F[i] respectively. The last test case is followed by two -1's. All integers are not greater than 1000.
Output
For each test case, print in a single line a real number accurate up to 3 decimal places, which is the maximum amount of JavaBeans that FatMouse can obtain.
Sample Input
5 3
7 2
4 3
5 2
20 3
25 18
24 15
15 10
-1 -1
Sample Output
13.333
31.500
程序清单:
package exercise;
import java.util.Arrays;
import java.util.Scanner;
public class javabean {
public static void main(String[] args) {
int catFood;
int roomNum;
double maxJavaBean=0.0;
Scanner in=new Scanner(System.in);
System.out.print("请输入老鼠所拥有catFood数量:");
catFood=in.nextInt();
System.out.print("请输入房间的数量:");
roomNum=in.nextInt();
int[][] javaBean=new int[roomNum][2];
double[] percent=new double[roomNum];
for(int i=0;i<roomNum;i++){
System.out.println("请输入第"+(i+1)+"间房间可兑换JavaBean数量和所需CatFood:");
javaBean[i][0]=in.nextInt();
javaBean[i][1]=in.nextInt();
percent[i]=(double)javaBean[i][0]/javaBean[i][1];
}
double[] percentcopy=Arrays.copyOfRange(percent, 0, percent.length);
Arrays.sort(percentcopy);
for(int k=percentcopy.length-1;k>=0;k--){
for(int i=0;i<roomNum;i++){
if(percent[i]==percentcopy[k]){
if(javaBean[i][1]<=catFood){
maxJavaBean+=javaBean[i][0];
catFood-=javaBean[i][1];
}else{
maxJavaBean+=percent[i]*catFood;
catFood=0;
break;
}
}
}
}
System.out.println("老鼠可以获得最大数量的JavaBean是:
"+maxJavaBean);
}
}。