一亩三分地论坛

 找回密码
 获取更多干货,去instant注册!

扫码关注一亩三分地公众号
查看: 924|回复: 13
收起左侧

[CS61B_Spring 2015] Discussion1-Intro to Java

[复制链接] |试试Instant~ |关注本帖
hurricane_e 发表于 2015-5-25 14:18:59 | 显示全部楼层 |阅读模式

[其他]CS61B Data Structures #14 - 2015-05-25@Berkeley

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看,没有帐号?获取更多干货,去instant注册!

x
Rt~
 楼主| hurricane_e 发表于 2015-5-26 14:15:12 | 显示全部楼层

1.

1.

2.

2.


3.

3.



回复 支持 1 反对 0

使用道具 举报

whosays 发表于 2015-5-26 07:38:22 | 显示全部楼层
1. declare the int variable ‘size' with the value of 27
declare the string variable ’name' with the value of “Fido”
declare a new Dog variable ‘myDog’, and ‘myDog’ can use ‘name’ and ‘size’
declare the int variable ‘x’, and assign it to the value of ‘size’ abstracted by 5
if the value of ‘x’ is less than 15, ‘myDog’ will bark 8 times

while loop keeps going when ‘x’ value is bigger than 3
inside the loop, ‘myDog’ is asked to play

declare the integer list ’numList' and put the values 2, 4, 6, 8 into the list
print out “Hello”
print out “Dog: Fido”

print out “4”
if the fourth element of ’numList’ is equal to 8, print out “potato"


2. MYSTERY
2.1 mystery will return 1.
2.2 mystery returns the smallest number indexed from the kth
2.3 the array will become sorted as 0, 1, 3, 3, 6. mystery2 can sort the inputArray in the ascending order

3. import java.util.Arrays;
public class dis1Fib {
        public static int fib(int N) {
                int k = 0;
                int[] fibN = new int[N+1];
                fibN[0] = 0;
                fibN[1] = 1;
                System.out.print(fibN[0] + " " + fibN[1] + " ");
                while ((k >= 0) && (k <= N-2)) {
                        fibN[k+2] = fibN[k+1] + fibN[k];
                        System.out.print(fibN[k+2] + " ");
                        k += 1;
                }

                return 0;
        }

        public static void main(String[] args) {
       
                fib(13);
       
        }
}

评分

1

查看全部评分

回复 支持 反对

使用道具 举报

iPhD 发表于 2015-6-2 00:53:46 | 显示全部楼层
whosays 发表于 2015-5-26 07:38
1. declare the int variable ‘size' with the value of 27
declare the string variable ’name' with t ...

第3题return的值有些小问题吧?题目里说这个method要return nth Fib(N)的值,但你的程序貌似只是把第n个数print出来呀,并没有做到return这个数。所以感觉还是用递归每次return fib(N-1)+fib(N-2)更符合题意一点。: P
回复 支持 反对

使用道具 举报

doood2015 发表于 2015-6-2 02:13:05 | 显示全部楼层
        public static int fib2(int num){
                int prev1 = 0;
                int prev2 = 1;
                for (int i =0; i <num ; i++){
                        int save = prev1;
                        prev1 = prev2;
                        prev2 = save + prev1;
                }
                return prev1;
        }

public static int fib(int num) {
                if (num <= 1){
                        return num;
                } else {
                        return fib(num-1)+ fib(num-2);
                }
               
        }

我感觉fibonacci 数列用递归还是很容易实现的,但是用iteration的话并不是那么直接可以想出来,
另外 to楼上:
* fib(N) returns the Nth Fibonacci number, for N ≥ 0.
你的方程返回的是void,估计是你理解和我不同:)

评分

1

查看全部评分

回复 支持 反对

使用道具 举报

whosays 发表于 2015-6-3 02:29:17 | 显示全部楼层
感谢楼上两位的提醒。你们说的是对的。因为我还在做别的题,自己调试时把很多题的答案有点弄混,写了两次菲波那切数列,return处有差别。

谢谢!
回复 支持 反对

使用道具 举报

whosays 发表于 2015-6-3 02:29:40 | 显示全部楼层
iPhD 发表于 2015-6-2 00:53
第3题return的值有些小问题吧?题目里说这个method要return nth Fib(N)的值,但你的程序貌似只是把第n个 ...

谢谢提醒!
回复 支持 反对

使用道具 举报

whosays 发表于 2015-6-3 02:29:53 | 显示全部楼层
doood2015 发表于 2015-6-2 02:13
public static int fib2(int num){
                int prev1 = 0;
                int prev2 = 1;

谢谢提醒!
回复 支持 反对

使用道具 举报

Casualet 发表于 2015-6-7 14:58:15 | 显示全部楼层
int size = 27;  //declare and integer variable, and give it the value 27
String name = "Fido";  //declare an String variable named ‘name’ and give it the value “Fido”
Dog myDog = new Dog(name, size);//declare an Dog variable named myDog, and specify the size and name of the dog.
int x = size - 5;//declare and integer variable named “x”, and subtract 5 from size, and then assign the result to x.
if (x < 15) {
myDog.bark(8);
}//if x is less than 15, then invoke the method bard with an argument 8

while (x > 3) {
myDog.play();
}//while x is bigger than 3, invoke the method play

int[] numList = {2, 4, 6, 8};//declare an integer array named numList,and put 2 4 6 8 into it.
System.out.print("Hello ");//print out String “Hello” in the console.
System.out.println("Dog: " + name);//print out “Dog”+name

System.out.println(numList[1]);//print out the second item in the array numList
if (numList[3] == 8) {
System.out.println("potato");// if numList[3]equals 8, then print out “potato ” in the console.
}

================================================================================  

public static int mystery(int[] inputArray, int k) {
int x = inputArray[k];
int answer = k;
int index = k + 1;
while (index < inputArray.length) {
if (inputArray[index] < x) {
x = inputArray[index];
answer = index;
}
index = index + 1;
}
return answer;
}
/** Extra for experts. This is another function. It takes an
* array of integers and returns nothing at all. */
public static void mystery2(int[] inputArray) {
int index = 0;
while (index < inputArray.length) {
int targetIndex = mystery(inputArray, index);
int temp = inputArray[targetIndex];
inputArray[targetIndex] = inputArray[index];
inputArray[index] = temp;
index = index + 1;
}
}

What does mystery return if inputArray is the array 3, 0, 1, 6, 3, and k is 2?
2
Describe, in English, what mystery returns.
The smallest integer in the inputArray, starting from index x to the end of the array.
Extra for experts: What does mystery2 do if inputArray is the array 3, 0, 1, 6, 3?
Reorder the inputArray so that it starts from the smallest one to the biggest. In this case, the inputArray becomes 0 1 3 3 6.
Describe, in English, what mystery2 does to the array
First, it initialize the value of variable index to 0, then it finds the index of the smallest integer in the array, starting from the variable ‘index’.
Second, it swap the smallest value found with the value in InputArray[index].
Thrid, it increment index.
This method continues until index equals inputArray.length. Since each time, it finds the smallest value and put it in the right place. The inputArray becomes sorted, from the smallest to the largest.
=================================================================================  1:
public static int fib(int N){
                if(n==0) return 0;
                if(n==1) return 1;
                return fb(n-1)+fb(n-2);
}
2:
public static int fib2(int n, int k, int f0, int f1){
      int temp=f0+f1;
      k=2;
      while(n>k){
    f0=f1;
    f1=temp;
    temp=f0+f1;
    k++;
}
return temp;
}
拖了这么久,应该过时了=_=  第三个的第二题是给定了函数头要求写5行吧,但是不知道参数的含义,大家好像不是按我的理解写的..

评分

1

查看全部评分

回复 支持 反对

使用道具 举报

pjh4168789 发表于 2015-6-16 04:58:33 | 显示全部楼层
楼主用的是甚么软件CODING的,看截图和老师上课的一样诶
回复 支持 反对

使用道具 举报

 楼主| hurricane_e 发表于 2015-6-16 08:20:33 | 显示全部楼层
pjh4168789 发表于 2015-6-16 04:58
楼主用的是甚么软件CODING的,看截图和老师上课的一样诶

sublime 是text editor~
回复 支持 反对

使用道具 举报

西风吹草 发表于 2015-6-17 00:02:19 | 显示全部楼层
2. Mystery
What does mystery return if inputArray is the array 3, 0, 1, 6, 3, and k is 2?
2
Describe, in English, what mystery returns.
Mystery returns the index of the smallest number from inputArray[k] to the end of inputArray.
Extra for experts: What does mystery2 do if inputArray is the array 3, 0, 1, 6, 3?
0 1 3 3 6
Describe, in English, what mystery2 does to the array.
Mystery2 rearranges the inputArray ascendingly.

3. Writing Your First Program
public class fib {
        public static int fibo(int N){
                if (N <= 1){
                        return N;
                }else{
                        return fibo(N - 1) + fibo(N - 2);
                }
        }
       
        public static void main(String[] args){
                System.out.println(fibo(6));
        }

The returned value is 8.

评分

1

查看全部评分

回复 支持 反对

使用道具 举报

geminiiiiiii 发表于 2015-6-18 15:32:14 | 显示全部楼层
1 int size = 27; //declare an integer size,ssign it size with 27
2 String name = "Fido"; //declare a string name,assign it with Fido
3 Dog myDog = new Dog(name, size); //decalare a Dog object named myDog with instant variable name and size
4 int x = size - 5; //declare an integer x, assign x with size-5
5 if (x < 15) {
6 myDog.bark(8);
7 }//if x is less than 15,then invoke myDog's method bark() with argument 8
8
9 while (x > 3) {
10 myDog.play();
11 }//while x is greater than 3, then invoke myDog's method play()
12
13 int[] numList = {2, 4, 6, 8};//declare an integer array numList with 2,4,6,8
14 System.out.print("Hello ");//print 'Hello'
15 System.out.println("Dog: " + name); //print 'Dog:'+ name(Fido)
16
17 System.out.println(numList[1]); //print the integer in numList with index 1
18 if (numList[3] == 8) {
19 System.out.println("potato");
20 }//if numlist[3] equals 8, then print potato



2  
returns 2
return the smallest value's index after kth value
sorting the array in ascending order


3.
public static int fib(int N)
    if (N < 2){ return N;}
    return fib(N-1)+fib(N-2)

评分

1

查看全部评分

回复 支持 反对

使用道具 举报

HNAKXR 发表于 2016-1-28 15:38:04 | 显示全部楼层
2 Mystery
2
Find the index of the minimum from the k-th element to the end.
Change it to 0,1,3,3,6. Sort in ascending order.

3 Writing Your First Program
  1. public static int fib (int N) {
  2.         if (N == 0) { return 0;}
  3.         if (N == 1 || N == 2) { return 1;}
  4.         return fib(N) + fib(N-1);
  5. }
复制代码

评分

1

查看全部评分

回复 支持 反对

使用道具 举报

本版积分规则

请点这里访问我们的新网站:一亩三分地Instant.

Instant搜索更强大,不扣积分,内容组织的更好更整洁!目前仍在beta版本,努力完善中!反馈请点这里

关闭

一亩三分地推荐上一条 /5 下一条

手机版|小黑屋|一亩三分地论坛声明 ( 沪ICP备11015994号 )

custom counter

GMT+8, 2016-12-6 22:40

Powered by Discuz! X3

© 2001-2013 Comsenz Inc. Design By HUXTeam

快速回复 返回顶部 返回列表