📣 黑五价提前开抢|VIP一年通行证5折,蓝莓、Offer多多同步特惠
查看: 3457| 回复: 13
跳转到指定楼层
上一主题 下一主题
收起左侧

[CS61B_Spring 2015] Discussion1-Intro to Java

全局:
公开课
学校名称: Berkeley
Unit号: 14
开课时间: 2015-05-25
课程全名: CS61B Data Structures
平台: 其他

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

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
Rt~

上一篇:[CS61B_Spring 2015] HW0 加分讨论帖
下一篇:南洋理工大学 法医学导论
推荐
 楼主| hurricane_e 2015-5-26 14:15:12 | 只看该作者
全局:






回复

使用道具 举报

推荐
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行吧,但是不知道参数的含义,大家好像不是按我的理解写的..
回复

使用道具 举报

推荐
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);
       
        }
}
回复

使用道具 举报

🔗
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,估计是你理解和我不同:)
回复

使用道具 举报

无效楼层,该帖已经被删除
无效楼层,该帖已经被删除
🔗
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;

谢谢提醒!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

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