SKY外语计算机学习
标题:
几道java题,感兴趣的朋友可以瞧一瞧
[打印本页]
作者:
流氓
时间:
2012-5-23 12:55
标题:
几道java题,感兴趣的朋友可以瞧一瞧
本帖最后由 sky_yx 于 2015-12-30 14:11 编辑
一、N阶螺旋矩阵。
1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7
package com.etop.arrays;
public class Test {
int[][]arr=new int[4][4];
int i=0;
int j=0;
int num=1;
public void spire(){
if(arr.length!=1){
Direction dir=Direction.RIGHT;
while(arr[i][j]==0){
arr[i][j]=num++;
switch(dir){
case RIGHT:
if((j+1<arr[0].length)&&(arr[i][j+1]==0)){
j++;
}else{
i++;
dir=Direction.DOWN;
}
break;
case DOWN:
if((i+1<arr.length)&&(arr[i+1][j]==0)){
i++;
}else{
j--;
dir=Direction.LEFT;
}
break;
case LEFT:
if((j-1>=0)&&(arr[i][j-1]==0)){
j--;
}else{
i--;
dir=Direction.UP;
}
break;
case UP:
if((i-1>=0)&&(arr[i-1][j])==0){
i--;
}else{
j++;
dir=Direction.RIGHT;
}
break;
}
}
print(arr);
}else
System.out.println(1);
}
public static void print(int[][] arr){
for(int i=0;i<arr.length;i++){
for(int j=0;j<arr[0].length;j++){
System.out.println(arr[i][j]+"\t");
}
System.out.println();
}
}
public enum Direction{
RIGHT,DOWN,LEFT,UP
}
public static void main(String[] args) {
Test t=new Test();
t.spire();
}
}
复制代码
二、编写一个java程序,按以下数字规律形成二维数组,并输出。
1 2 9 10 25
4 3 8 11 24
5 6 7 12 23
16 15 14 13 22
17 18 19 20 21
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
int n=7;
Scanner s=new Scanner(System.in);
System.out.print("输入大小N:");
n=s.nextInt();
System.out.println();
for(int y=0;y<n;y++){
int yr=(y+1)%2;//1 0 1 0 1 0 1
int ye=(y+1)/2*2;//0 2 2 4 4 6 6
for(int x=0;x<n;x++){
//(e+1)^2 (e+1)^+1 (e+1)^2
int xr=x%2;
int xe=x/2*2+1;
System.out.format("%5d",
(x>y? (xe*xe+xr+y*(xr==0?-1:1)):(ye*ye+yr+x*(yr==0?-1:1)))
);
}
System.out.println();
}
}
}
复制代码
三、模拟多人排队买球票,设球票5元一张,购票者持5元的,有持10元的,有持20元的,有持50元的,持5元的可以立即购买,持10元的如果有5元零钱就要可以直接买并找零,否则进入旁边等待通道等待,同理50元的也如此,如果无法找零,到旁边通道的最末等待,已知排队者持钱情况序列为{10,5,20,5,5,20,10,5,5,50,5,5,20,5,5,10,5,20,5,20},试给出排队购票情况。
package com.etop.lianxi;
import java.util.LinkedList;
import java.util.Vector;
public class Test1 {
public static void main(String[] args){
int[] persons = new int[]{10,5,20,5,5,20,10,5,5,50,5,5,20,5,5,10,5,20,5,20};
MyQueue queue = new MyQueue();
//全部人入队列
for (int i=0; i<persons.length;i++){
queue.enqueue(new Buyer(i, persons[i]));
}
//收到的钱
LinkedList<Integer> changeQueue = new LinkedList<Integer>();
//每个人循环两次,或者所有的人都买到票
for (int i=0;i<40 && !queue.isEmpty();i++){
//出队列
Buyer buyer = queue.dequeue();
int money = buyer.money;
//5元直接买
if (money == 5) {
System.out.printf("%d号买到,不需要找零\n", buyer.index);
changeQueue.addLast(new Integer(5));
//用收到的钱找零
}else{
LinkedList<Integer> change = getChange(changeQueue, money - 5);
//找不开
if (change == null) {
System.out.printf("%d号用%d元钱,没有找零,等待\n", buyer.index, money);
queue.enqueue(buyer);
//找零
}else{
System.out.printf("%d号用%d元钱买,找零%s\n", buyer.index, money, change.toString());
changeQueue.addLast(new Integer(money));
}
}
sort(changeQueue);
System.out.println("收到的钱:" + changeQueue.toString());
}
//打印没有买到票的人的信息
for (Buyer buyer:queue){
System.out.println(buyer.index + "拿"+buyer.money+"元买,没有买到票");
}
//打印收到的钱的信息
System.out.println("最后收到的钱:" + changeQueue);
}
//从change里面找零,比方getChange([10,10,5,5,5], 25)返回[10,10,5]
//getChange([10,10,5,5,5], 27)返回null
public static LinkedList<Integer> getChange(LinkedList<Integer> change, int money){
//System.out.println(change + "\t" + money);
LinkedList<Integer> past = new LinkedList<Integer>();
LinkedList<Integer> buff = new LinkedList<Integer>();
while (true){
if (money == 0) {
change.addAll(buff);
sort(change);
return past;
}
if (change.isEmpty()) break;
int i = change.remove().intValue();
if (money>=i){
money -= i;
past.add(new Integer(i));
}else{
buff.addFirst(new Integer(i));
}
}
change.addAll(past);
change.addAll(buff);
sort(change);
//for (int i=0;i<change.size();i++) System.out.println(change.get(i) + " ");
return null;
}
//对LinkedList排序,从大到小
public static void sort(LinkedList<Integer> l){
for (int i=0;i<l.size();i++){
for (int j=i+1;j<l.size();j++){
if (l.get(i) < l.get(j)){
Integer n = l.get(i);
}
}
}
}
}
class MyQueue extends Vector<Buyer>{
//出队列操作
public Buyer dequeue(){
if (isEmpty()) return null;
return remove(0);
}
public boolean isEmpty() {
// TODO Auto-generated method stub
return false;
}
//入队列操作
public void enqueue(Buyer buyer){
add(buyer);
}
}
class Buyer{
//序号
public int index;
//钱
public int money;
public Buyer(int index, int money){
this.index = index;
this.money = money;
}
}
复制代码
四、试利用java.util.Date类继承声明MyDate类,使MyDate类增加以下功能:增加1天、增加1月和增加1年的方法;输出MyDate对象日期的方法;求两个MyDate对象日期差的方法;提供能用当前日期初始化MyDate类对象的构造方法。同时编写应用程序能实现对MyDate对象的测试。
package com.etop.date;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class CalendarExample {
private static void prt(String s) {
System.out.println(s);
}
private static void prt() {
System.out.println();
}
private static void doCalendarTimeExample() {
prt("CURRENT DATE/TIME");
prt("=================================================================");
Date now = Calendar.getInstance().getTime();
prt(" Calendar.getInstance().getTime() : " + now);
prt();
}
private static void doSimpleDateFormat() {
prt("SIMPLE DATE FORMAT");
prt("=================================================================");
Calendar now = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
prt(" It is now : " + formatter.format(now.getTime()));
prt();
}
private static void doAdd() {
prt("ADD / SUBTRACT CALENDAR / DATEs");
prt("=================================================================");
Calendar now = Calendar.getInstance();
Calendar working;
SimpleDateFormat formatter = new SimpleDateFormat("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
working = (Calendar) now.clone();
working.add(Calendar.DAY_OF_YEAR, -(365 * 2));
prt(" Two years ago it was: " + formatter.format(working.getTime()));
working = (Calendar) now.clone();
working.add(Calendar.DAY_OF_YEAR, +5);
prt(" In five days it will be: " + formatter.format(working.getTime()));
prt();
}
private static void doDateDifference() {
prt("DIFFERENCE BETWEEN TWO DATES");
prt("=================================================================");
Date startDate1 = new GregorianCalendar(1994, 02, 14, 14, 00).getTime();
Date endDate1 = new Date();
long diff = endDate1.getTime() - startDate1.getTime();
prt(" Difference between " + endDate1);
prt(" and " + startDate1 + " is " + (diff / (1000L * 60L * 60L * 24L)) + " days.");
prt();
}
private static void doGetMethods() {
prt("CALENDAR GET METHODS");
prt("=================================================================");
Calendar c = Calendar.getInstance();
prt(" YEAR : " + c.get(Calendar.YEAR));
prt(" MONTH : " + c.get(Calendar.MONTH));
prt(" DAY_OF_MONTH : " + c.get(Calendar.DAY_OF_MONTH));
prt(" DAY_OF_WEEK : " + c.get(Calendar.DAY_OF_WEEK));
prt(" DAY_OF_YEAR : " + c.get(Calendar.DAY_OF_YEAR));
prt(" WEEK_OF_YEAR : " + c.get(Calendar.WEEK_OF_YEAR));
prt(" WEEK_OF_MONTH : " + c.get(Calendar.WEEK_OF_MONTH));
prt(" DAY_OF_WEEK_IN_MONTH : " + c.get(Calendar.DAY_OF_WEEK_IN_MONTH));
prt(" HOUR : " + c.get(Calendar.HOUR));
prt(" AM_PM : " + c.get(Calendar.AM_PM));
prt(" HOUR_OF_DAY (24-hour): " + c.get(Calendar.HOUR_OF_DAY));
prt(" MINUTE : " + c.get(Calendar.MINUTE));
prt(" SECOND : " + c.get(Calendar.SECOND));
prt();
}
public static void main(String[] args) {
prt();
doCalendarTimeExample();
doSimpleDateFormat();
doAdd();
doDateDifference();
doGetMethods();
}
}
复制代码
作者:
rogan
时间:
2012-5-23 14:15
这个不错,代码加了[code][/code]看起来会不会好点?
作者:
流氓
时间:
2012-5-23 22:06
本帖最后由 sky_yx 于 2015-12-30 14:11 编辑
这个不错,代码加了[code][/code]看起来会不会好点?[/quote]
好的,谢了
欢迎光临 SKY外语计算机学习 (http://skywj.com/)
Powered by Discuz! X2.5