Skip to content

Commit

Permalink
Merge pull request onlyliuxin#22 from Memory-Cunese/master
Browse files Browse the repository at this point in the history
提交第二次作业
  • Loading branch information
BlindingDark authored Mar 19, 2017
2 parents 170c8a2 + 1fdfccc commit 2eb574a
Show file tree
Hide file tree
Showing 4 changed files with 312 additions and 2 deletions.
2 changes: 0 additions & 2 deletions group26/1778842360/.gitignore

This file was deleted.

239 changes: 239 additions & 0 deletions group26/1778842360/second homework/ArrayUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,239 @@
package array;

import java.util.Arrays;

public class ArrayUtil {
/**
* 给定一个整形数组a , 对该数组的值进行置换
例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7]
如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7]
* @param origin
* @return
*/
public static void reverseArray(int[] origin){
for(int i=0;i<origin.length/2;i++)
{
int temp=origin[i];
origin[i]=origin[origin.length-1-i];
origin[origin.length-1-i]=temp;
}
}

/**
* 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}
* 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为:
* {1,3,4,5,6,6,5,4,7,6,7,5}
* @param oldArray
* @param newArray
* @return
*/

public static int[] removeZero(int[] oldArray){
int[] newArray=new int[oldArray.length];
int j=0;
for(int i=0;i<oldArray.length;i++)
{
if(oldArray[i]!=0)
{
newArray[j++]=oldArray[i];
}
}
newArray=Arrays.copyOf(newArray, j);
return newArray;
}

/**
* 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的
* 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复
* @param array1
* @param array2
* @return
*/

public static int[] merge(int[] array1, int[] array2){
int[] array3=new int[array1.length+array2.length];
int i=0,j=0,k=0;
while(i<array1.length&&j<array2.length)
{
if(array1[i]<array2[j])
{
array3[k++]=array1[i];
i++;
}
else if(array1[i]>array2[j]){
array3[k++]=array2[j];
j++;
}
else{
array3[k++]=array1[i];
i++;
j++;
}
}
while(i<array1.length)
{
array3[k++]=array1[i];
i++;
}
while(j<array2.length)
{
array3[k++]=array1[j];
j++;
}
array3=Arrays.copyOf(array3, k);
return array3;
}
/**
* 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size
* 注意,老数组的元素在新数组中需要保持
* 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为
* [2,3,6,0,0,0]
* @param oldArray
* @param size
* @return
*/
public static int[] grow(int [] oldArray, int size){
int[] newArray=new int[oldArray.length+size];
System.arraycopy(oldArray, 0, newArray, 0, oldArray.length);
for(int i=oldArray.length;i<newArray.length;i++)
{
newArray[i]=0;
}
return newArray;
}

/**
* 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列
* 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13]
* max = 1, 则返回空数组 []
* @param max
* @return
*/
public static int[] fibonacci(int max){
int[] array=new int[0];
if(max==1)
{
return array;
}
else{
//System.arraycopy(array, 0, array1, 0, 2);
array=Arrays.copyOf(array, 2);
//System.out.println(array.length);
array[0]=1;
array[1]=1;
int i=2;
while(true)
{
//System.arraycopy(array, 0, array, 0, array.length+1);

int t=array[i-2]+array[i-1];
if(t<max)
{
array=Arrays.copyOf(array, array.length+1);

array[i]=t;
i++;
}
else{
return array;
}
}
}
//return array;
}


/**
* 返回小于给定最大值max的所有素数数组
* 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19]
* @param max
* @return
*/
public static int[] getPrimes(int max){
//初始化一个数组
int[] array=new int[0];
int k=0;
if(max<2)
{
return array;
}
else{
array=Arrays.copyOf(array, array.length+1);
array[k++]=2;
for(int i=3;i<max;i++)
{
//定义一个标志
boolean flag=true;
for(int j=2;j<=i/2;j++)
{
if(i%j==0)
{
flag=false;
}
}
if(flag)
{
array=Arrays.copyOf(array, array.length+1);
array[k++]=i;
}
}
}
return array;
}

/**
* 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3
* 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数
* @param max
* @return
*/
public static int[] getPerfectNumbers(int max){
int[] array=new int[0];
int k=0;//记录符合条件的元素的个数
if(max<1)
{
return array;
}
for(int i=1;i<max;i++)
{
int s=0;
for(int j=1;j<i;j++)
{
if(i%j==0)
{
s+=j;
}
}
if(s==i)
{
array=Arrays.copyOf(array, array.length+1);
array[k++]=i;
}

}
return array;
}

/**
* 用seperator 把数组 array给连接起来
* 例如array= [3,8,9], seperator = "-"
* 则返回值为"3-8-9"
* @param array
* @param s
* @return
*/
public static String join(int[] array, String seperator){
StringBuilder s=new StringBuilder();
for(int i=0;i<array.length;i++)
{
if(i==array.length-1)
{
s.append(array[array.length-1]);
}
else{
s.append(array[i]).append(seperator);
}
}
return s.toString();
}
}
71 changes: 71 additions & 0 deletions group26/1778842360/second homework/ArrayUtilTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package array;

import static org.junit.Assert.*;

import org.junit.Assert;
import org.junit.Test;

public class ArrayUtilTest {
@Test
public void testReverseArray() {
int[] array={3,5,7,8};
ArrayUtil.reverseArray(array);
int[] newArray={8,7,5,3};
Assert.assertArrayEquals(newArray, array);
}

@Test
public void testRemoveZero() {
int[] array={3,5,0,0,0,7,8};
int[] array1=ArrayUtil.removeZero(array);
int[] newArray={3,5,7,8};
Assert.assertArrayEquals(newArray, array1);
}

@Test
public void testMerge() {
int[] a1 = {3, 5, 7,8} ;
int[] a2 = {4, 5, 6,7};
int[] expecteds={3,4,5,6,7,8};
int[] actuals=ArrayUtil.merge(a1, a2);
Assert.assertArrayEquals(expecteds, actuals);
}

@Test
public void testGrow() {
int[] a1 = {3, 5, 7,8} ;
int[] actuals=ArrayUtil.grow(a1, 3);
int[] expecteds={3,5,7,8,0,0,0};
Assert.assertArrayEquals(expecteds, actuals);
}

@Test
public void testFibonacci() {
int[] actuals=ArrayUtil.fibonacci(15);
int[] expecteds={1,1,2,3,5,8,13};
Assert.assertArrayEquals(expecteds, actuals);
}

@Test
public void testGetPrimes() {
int[] expecteds= {2,3,5,7,11,13,17,19};
int[] actuals=ArrayUtil.getPrimes(23);
Assert.assertArrayEquals(expecteds, actuals);
}

@Test
public void testGetPerfectNumbers() {
int[] actuals=ArrayUtil.getPerfectNumbers(100);
int[] expecteds={6,28};
Assert.assertArrayEquals(expecteds, actuals);
}

@Test
public void testJoin() {
int[] array={3,5,7,8};
String actuals=ArrayUtil.join(array, "-");
String expecteds="3-5-7-8";
Assert.assertEquals(expecteds, actuals);
}

}
2 changes: 2 additions & 0 deletions group26/26组情况统计.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@
| | http://blog.csdn.net/u012759397/article/details/61618612 | | | | |
|191191717 |已完成|       |           |     |     |     |     |     |     |     |
| | | | | | | | | | | |
| 1778842360 | 已完成 |完成80% | | | | | | | | |
| | 文章1 http://note.youdao.com/noteshare?id=7e8faf2bd2a7a3e5259d83bb1e6d9d8f | | | | | | | | | |

0 comments on commit 2eb574a

Please sign in to comment.