中级农民
- 积分
- 251
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2015-4-23
- 最后登录
- 1970-1-1
|
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行吧,但是不知道参数的含义,大家好像不是按我的理解写的.. |
|