1.使用递归输出30位的斐波那契数列
见day03作业
2.设计一个函数,计算两个日期之间相差多少天。两个日期的年月日由参数控制。
import java
.util
.Scanner
;
class Demo13
{
public static void main(String
[] args
)
{
int year1
,month1
,day1
;
int year2
,month2
,day2
;
int all_days
;
Scanner sc
= new Scanner(System
.in
);
System
.out
.print("please input year1、month1、day1:");
year1
= sc
.nextInt();
month1
= sc
.nextInt();
day1
= sc
.nextInt();
System
.out
.print("please input year2、month2、day2:");
year2
= sc
.nextInt();
month2
= sc
.nextInt();
day2
= sc
.nextInt();
if(year1
!= year2
)
{
all_days
= calRestDays(year1
,month1
,day1
) + calDays(year2
,month2
,day2
) + calYear(year1
,year2
);
}
else
all_days
= calDays(year2
,month2
,day2
) - calDays(year1
,month1
,day1
);
System
.out
.print("The totall days are:" + all_days
);
}
public static boolean isLeap(int year
)
{
if ((year
%4 == 0 && year
%100 != 0) || year
%400 == 0)
return true;
return false;
}
public static int calRestDays(int year
, int month
, int day
)
{
int[] mon
= new int[]{31,28,31,30,31,30,31,31,30,31,30,31};
int total_days
= 0;
int rest_days
;
for (int i
=0; i
<month
-1; i
++)
{
total_days
+= mon
[i
];
}
total_days
+= day
;
if(month
>2 && isLeap(year
))
{
total_days
++;
rest_days
= 366-total_days
;
}
else
rest_days
= 365-total_days
;
return rest_days
;
}
public static int calDays(int year
, int month
, int day
)
{
int[] mon
= new int[]{31,28,31,30,31,30,31,31,30,31,30,31};
int total_days
= 0;
for (int i
=0; i
<month
-1; i
++)
{
total_days
+= mon
[i
];
}
total_days
+= day
;
if(month
>2 && isLeap(year
))
total_days
++;
return total_days
;
}
public static int calYear(int year1
, int year2
)
{
int sum
=0;
for (int i
= (year1
+1); i
<year2
; i
++)
{
if(!isLeap(i
))
sum
+= 365;
else
{
sum
+= 365;
sum
++;
}
}
return sum
;
}
}
3.设计一个函数,判断一个数组是不是一个升序的数组。
class Demo8
{
public static void main(String
[] args
)
{
int[] a
= new int[]{10,2,5,8};
boolean boo
= inc(a
);
if(boo
== true)
System
.out
.print("是升序");
else
System
.out
.print("不是升序");
}
public static boolean inc(int[] arr
)
{
int low
=0, high
=1;
while (high
< arr
.length
)
{
if (arr
[low
++] > arr
[high
++])
return false;
}
return true;
}
}
4.设计一个函数,将一个数组中的元素倒序排列(注意,不是降序)。
import java
.util
.Scanner
;
class Demo9
{
public static void main(String
[] args
)
{
Scanner sc
= new Scanner(System
.in
);
int[] arr
= new int[10];
System
.out
.print("input 10 numers:");
for (int i
=0; i
<arr
.length
; i
++)
{
arr
[i
] = sc
.nextInt();
}
reverse(arr
);
for (int i
=0; i
<arr
.length
; i
++)
{
System
.out
.print(arr
[i
] + " ");
}
}
public static void reverse(int[] arr
)
{
int low
=0, high
=arr
.length
-1;
while (low
<high
)
{
int temp
= arr
[low
];
arr
[low
] = arr
[high
];
arr
[high
] = temp
;
low
++;
high
--;
}
}
}
5.将一个数组中的元素拷贝到另外一个数组中
import java
.util
.Scanner
;
class Demo10
{
public static void main(String
[] args
)
{
Scanner sc
= new Scanner(System
.in
);
int[] arr1
= new int[10];
int[] arr2
= new int[10];
System
.out
.print("input 10 numers:");
for (int i
=0; i
<arr1
.length
; i
++)
{
arr1
[i
] = sc
.nextInt();
}
copyArray(arr1
, arr2
);
for (int i
=0; i
<arr1
.length
; i
++)
{
System
.out
.print(arr1
[i
] + " ");
}
System
.out
.println();
for (int i
=0; i
<arr2
.length
; i
++)
{
System
.out
.print(arr2
[i
] + " ");
}
}
public static void copyArray(int[] arr1
, int[] arr2
)
{
int j
=0;
for (int i
=0; i
<arr1
.length
; i
++)
{
arr2
[j
++] = arr1
[i
];
}
}
}
6. 设计一个函数,比较两个数组中的元素是否相同(数量、每一个数值都相同,才认为是相同的数组)
class Demo11
{
public static void main(String
[] args
)
{
int[] arr1
= {1, 5, 3, 4, 5};
int[] arr2
= {1, 2, 3, 4, 5};
boolean boo
=isEqual(arr1
, arr2
);
if(boo
)
System
.out
.println("isEqual");
else
System
.out
.println("isNotEqual");
}
public static boolean isEqual(int[] arr1
, int[] arr2
)
{
int i
=0,j
=0;
while (i
<arr1
.length
&& j
<arr2
.length
)
{
if (arr1
[i
++] != arr2
[j
++])
return false;
}
if (i
<arr1
.length
|| j
<arr2
.length
)
return false;
return true;
}
}
7.设计一个函数,找出一个整型数组中的第二大的值。
(1)不可以通过排序实现,不能修改数组中的数据顺序 (2)要考虑到最大的数字可能出现多次
class Demo12
{
public static void main(String
[] args
)
{
int[] arr
= {6,5,5,4,3,5};
int max
= nextMax(arr
);
System
.out
.println(arr
[max
]);
}
public static int nextMax(int[] arr
)
{
int max1
=0, max2
=0;
int flag
=0;
for (int i
=1; i
<arr
.length
; i
++)
{
if (arr
[i
] > arr
[max1
])
{
flag
= 1;
max2
= max1
;
max1
= i
;
}
}
if (flag
== 0)
{
max2
=1;
for (int i
=2; i
<arr
.length
; i
++)
if (arr
[i
] > arr
[max2
])
max2
= i
;
}
return max2
;
}
}
转载请注明原文地址:https://ipadbbs.8miu.com/read-57655.html