import java.math.*; class Rectangle{ private int w, h; static int AllCnt; static int AllArea; public Rectangle(int w_, int h_) { w = w_; h = h_; AllCnt++; AllArea += w * h; } static void Show() { System.out.println("AllCnt = " + AllCnt + " " + " AllArea = " + AllArea); } int CirRec() { return 2 * (w + h); } int AreRec() { return w * h; } } class Circle{ final double pi = Math.acos(-1); private double r; protected Circle(double rr) { r = rr; } double GetCir() { return 2.0 * pi * r; } double GetAre() { return pi * r * r; } public void Show() { System.out.println("Circle = " + GetCir() + " Area = " + GetAre()); } } public class Main6 { public static void main(String[] args) { System.out.println("下面测试矩形类:"); Rectangle.AllArea = 0; Rectangle.AllCnt = 0; Rectangle r1 = new Rectangle(1, 2); Rectangle.Show(); System.out.println("r1 : Cir = " + r1.CirRec() + " Area = " + r1.AreRec()); Rectangle r2 = new Rectangle(3, 4); Rectangle.Show(); System.out.println("r2 : Cir = " + r2.CirRec() + " Area = " + r2.AreRec()); System.out.println("下面测试圆形类:"); Circle c1 = new Circle(2.0); c1.Show(); Circle c2 = new Circle(3.0); c2.Show(); } }第二题:1 到 100的素数之和
import java.util.Arrays; public class Main1 { static int primeS() { int cnt = 0, ans = 0; boolean[] vis = new boolean[105]; int[] isPrime = new int[105]; Arrays.fill(isPrime, 0); Arrays.fill(vis, true); for(int i = 2;i <= 100;++i) { if(vis[i]) { isPrime[cnt++] = i; ans += i; } for(int j = 0;j < cnt && i * isPrime[j] <= 100;++j) { vis[i * isPrime[j]] = false; if(0 == i % isPrime[j]) break; } } return ans; } public static void main(String[] args) { int ans = primeS(); System.out.println(ans); } }第三题:
编写一个程序,程序提供两种功能:
Weekday = (dd + 2 * dm + 3 * (dm + 1) / 5 + dy + dy / 4 - dy / 100 + dy / 400) % 7 + 1
dy = 年份
dm = 月份
dd = 天
这里需要注意的是:
如果月份小于3!需要借一年,给月份加上12!(重点!)
(这套计算方法我没看懂月份的计算,天和年的好理解!希望有懂得的伙伴们能够评论或者私信指点我一下!小弟感激不尽!!!)
当然如果不用这个公式,依赖循环也可写出答案。
import java.util.Scanner; public class Main2 { static boolean checkLeap(int Ly) { if((0 == Ly % 4 && 0 != Ly % 100) || 0 == Ly % 400) return true; else return false; } public static int GetWeek(int dy, int dm, int dd) { return (dd + 2 * dm + 3 * (dm + 1) / 5 + dy + dy / 4 - dy / 100 + dy / 400) % 7 + 1; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请选择万年历的功能:1-> 查询日历? 2->查询某日期是星期几?"); int cmd = sc.nextInt(); if(1 == cmd) { System.out.println("请输入您要查询日历的年份:"); int Year = sc.nextInt(); int day = GetWeek(Year - 1, 13, 1); for(int i = 1;i <= 12;++i) { int TempDay = 0, cnt = 1; switch(i) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: TempDay = 31; break; case 2: if(checkLeap(Year)) TempDay = 29; else TempDay = 28; break; default: TempDay = 30; } System.out.println("万年历:======第 " + i + " 月份:=========="); System.out.println("日\t一\t二\t三\t四\t五\t六"); boolean book = false; for(int j = 0;j < 6;++j) { for(int k = 0;k < 7;++k) { if(0 == j && k < day) { System.out.print("\t"); continue; } if(cnt > TempDay) { book = true; break; } System.out.print(cnt + "\t"); cnt++; } if(book) break; System.out.println(); } day = (day + TempDay) % 7; System.out.println(); System.out.println(); } } else { System.out.println("请按照如下格式:YYYY-MM-DD输入,如果非法输入将不受理!"); int dy = 0, dm = 0, dd = 0, i = 0, j = 0; @SuppressWarnings("unused") String getChar = sc.nextLine(); String DataInfo = sc.nextLine(); while(i < DataInfo.length()) { if(0 == dy) { j = i; while(j < DataInfo.length() && '-' != DataInfo.charAt(j)) { dy = dy * 10 + (DataInfo.charAt(j) - '0'); j++; } } else if(0 == dm) { j = i; while(j < DataInfo.length() && '-' != DataInfo.charAt(j)) { dm = dm * 10 + (DataInfo.charAt(j) - '0'); j++; } } else if(0 == dd) { j = i; while(j < DataInfo.length() && '-' != DataInfo.charAt(j)) { dd = dd * 10 + (DataInfo.charAt(j) - '0'); j++; } } i = ++j; } if(dm < 3) { dm += 12; dy--; } int AnsDay = GetWeek(dy, dm, dd); System.out.println("您输入的日期对应的星期是:" + AnsDay); } sc.close(); } }