1、每个非素数都可以唯一地被分解为若干素数地乘积,请编程对其进行验证。
个人答案:
package 实验1;
import java.util.Scanner;
public class sushu {
public static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
System.err.println("Please enter a number:");
int x = input.nextInt();
System.out.print(x+"=");
int i=2;
while(i<=x){ //使用循环来找到可以被整除的数,然后通过out函数输出
if(i == x){ //如果相等的话,就说明这个数没有因数,只有1和它自己;
System.out.println(i);
break;
}
else if(x % i ==0){ //如果这个数有因数,然后找到除去这个因数后的值,继续循环
System.out.println(i+"*");
x=x/i;
}
else{ //如果都不满足,则继续循环
i++;
}
}
}
}
参考答案:
package primeDot;
import java.util.*;
public class primeDot {
public static void main(String[] args)
{
System.out.println("请输入一个大于2的非素数。输入0退出");
Scanner scn=new Scanner(System.in);
int n=scn.nextInt();
while(n!=0)
{
int i=2;//i是除数
int g=2;//g是商
for(;n%i!=0;i++)
{
}
g=n/i;//第一个能被整除的最小数肯定是素数
System.out.print(i+"\t");
for(;i<g;i++)
{
for(;g%i!=0;i++)
{
}
g=g/i;//更新g,当前的i是能被g整除的最小素数
if(isPrime(i))
{
System.out.print(i+"\t");
}
if(isPrime(g))
{
System.out.print(g+"\t");
}
i=1;//因为前面for要++。所以更新为1,经过for+后就会成为2
}
System.out.println("请继续输入");
n=scn.nextInt();
}
System.out.println("结束");
}
public static boolean isPrime(int x)
{
if(x<2)
{
return false;
}
for(int i=2;i<=Math.sqrt(x);i++)
{
if(x%i==0)
{
return false;
}
}
return true;
}
}
2、已经2017年的第一天是星期日,打印2017年某月份的月历。书上38页实验题目2
个人答案:
package 实验1;
import java.util.Scanner;
public class Calander {
//主函数入口
public static void main(String[] args) {
try (Scanner input = new Scanner(System.in)) {
//获得用户输入的年月数据
System.out.print("请输入年月(格式:年.月,如2019.9): ");
String yearAndMonth = input.nextLine();
//把客户输入的数据分离出年与月,并且转为整数类型
int year,month;
year = Integer.parseInt(yearAndMonth.substring(0,yearAndMonth.indexOf('.')));
month = Integer.parseInt(yearAndMonth.substring(yearAndMonth.indexOf('.')+1));
//打印日历的头部
printMonthTitle(year,month);
//打印日历的日期部分
printMonthbody(year,month);
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//打印日历的头部部分
public static void printMonthTitle(int year, int month) {
// TODO Auto-generated method stub
String nameOfMonth = getNameOfMonth(month);
System.out.printf("\n%13s-%-13d\n",nameOfMonth,year);
for(int i=0;i<27;i++) System.out.print('=');
System.out.println("\nMon Tue Wed Thu Fri Sat Sun");
}
//获得某个月的英文名称
public static String getNameOfMonth(int month) {
// TODO Auto-generated method stub
String nameOfMonth="January";
switch(month) {
case 1: nameOfMonth = "January";break;
case 2: nameOfMonth = "February";break;
case 3: nameOfMonth = "March";break;
case 4: nameOfMonth = "April";break;
case 5: nameOfMonth = "May";break;
case 6: nameOfMonth = "June";break;
case 7: nameOfMonth = "July";break;
case 8: nameOfMonth = "August";break;
case 9: nameOfMonth = "September";break;
case 10: nameOfMonth = "October";break;
case 11: nameOfMonth = "November";break;
case 12: nameOfMonth = "Decmber";break;
}
return nameOfMonth;
}
//输出打印的月份日期部分
public static void printMonthbody(int year, int month) {
// TODO Auto-generated method stub
int count=0;
int firstDayOfMonth=getFirstDayOfMonth(year,month);
int totalDayOfMonth=getTotalDayOfMonth(year,month);
//打印1号之前的日历空格
for(int i=1;i<firstDayOfMonth;i++) {
System.out.print(" ");
count++;
}
//开始从1号开始打印,一直到整个月结束
for(int day=1;day<=totalDayOfMonth;day++) {
if(count==7) {
System.out.print("\n");
count = 0;
}
System.out.printf("%-4d",day);
count++;
}
}
//获得某个月的总天数
public static int getTotalDayOfMonth(int year, int month) {
//返回各个月的天数
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
return 31;
else if(month==4||month==6||month==9||month==11)
return 30;
else{
if(isLeapYear(year)) return 29;
else return 28;
}
}
//算出某月第一天是星期几
public static int getFirstDayOfMonth(int year, int month) {
// 返回某个月的第一天是星期几,已知2019-1-1是周二
int totalDayFrom2019_1_1=0,day=2;
if(year>=2019) {
//累加2019年到year之间的所有整年总天数
if(year>2019) {
for(int i=2019;i<year;i++) {
if(isLeapYear(i)){
totalDayFrom2019_1_1 += 366;
}else {
totalDayFrom2019_1_1 += 365;
}
}
}
//累加该月份到该年1月的总天数
for(int j=1;j<month;j++) {
totalDayFrom2019_1_1 += getTotalDayOfMonth(year,j);
}
}
else {
//累加2019年到year之间的所有整年总天数
for(int i=year+1;i<2019;i++) {
if(isLeapYear(i)){
totalDayFrom2019_1_1 += 366;
}else {
totalDayFrom2019_1_1 += 365;
}
}
//累加该月份到该年12月的总天数
for(int j=12;j>=month;j--) {
totalDayFrom2019_1_1 += getTotalDayOfMonth(year,j);
}
}
//计算得到该月第一天星期几,如果结果为0即刚好被7整除,为星期天
if(year>=2019) {
day = ((totalDayFrom2019_1_1%7)+day)%7;
}else {
day = (day-(totalDayFrom2019_1_1%7)+7)%7;
}
if(day==0) day=7;
return day;
}
//计算某年是否为闰年
public static boolean isLeapYear(int year) {
//判断一个年份是否是闰年
boolean leapYearOrNot=false;
if ((year%4==0 && year%100!=0)||year%400==0) {
leapYearOrNot=true;
}
else
leapYearOrNot=false;
return leapYearOrNot;
}
}
参考答案:
/***
*
* 打印某年某月日历
*
*/
import java.util.*;
public class Calender{
public static void main(String[] args)
{
System.out.println("请输入年和月");
Scanner scn=new Scanner(System.in);
int year=scn.nextInt();
int month=scn.nextInt();
title(year,month);
int day=getDay(year,month);
int st=getWeek(year,month);
int i=1;
for(;i<=st;i++)
System.out.printf("%4s"," ");
// System.out.print("\t");
for(int d=1;d<=day;d++)
{
System.out.printf("%4s",d);
// System.out.print("\t"+d);
if((d+st)%7==0)
{
System.out.println();
}
}
}
public static void title(int year,int month)
{
//System.out.println("\t"+"\t"+"\t"+"*****"+year+"年"+month+"月份"+"*****");
System.out.println("*****"+year+"年"+month+"月份"+"*****");
//System.out.print("\t"+"日"+"\t"+"一"+"\t"+"二"+"\t"+"三"+"\t"+"四"+"\t"+"五"+"\t"+"六"+"\t");
System.out.printf("%3s%4s%4s%4s%4s%4s%3s","日","一","二","三","四","五","六");
System.out.println();
}
public static int getWeek(int year,int month)//查看这个月开始那天是星期几
{
int x=0;
for(int m=1;m<month;m++)
{
x=x+getDay(year,m);
}
int st=x%7;
return st;
}
public static int getDay(int year,int month)
{
switch(month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return 31;
case 4:
case 6:
case 9:
case 11:
return 30;
case 2:
if (isLeap(year))
{
return 29;
}
else
{
return 28;
}
}
return 0;
}
public static boolean isLeap(int year)
{
if((year%4==0&&year%100!=0)||(year%400==0))
{
return true;
}
else
return false;
}
}