Hello, everyone.
Today I learned some about the Java algorithm:
1. algorithm : The basic steps to solve the problem , and the implementation of the solution
algorithmic issues in business processing :
Analyzing problems and finding patterns
Write code for the rule
2.99 Multiplication Table
Problem Analysis: 1*1=1
1*2=2 2*2=2
1*3=3 2*3=6 3*3=9
We find that the first few lines of the rule have several multiplication patterns, which require two traversal, traverse from 1-9 rows, and then 1-9 columns traverse
public class Returntest {public static void main (string[] args) {Returntest.ceshi ();} public static void Ceshi () {for (int. i=1;i<=9;i++) {for (int j=1;j<=i;j++) {System.out.print (i+ "x" +j+ "=" +i*j); System.out.print ("\ t");} System.out.println ();}}}
We call our methods to see the results
This will print out our 99 multiplication table.
The knowledge points involved, the character escape sequence of the use of \ t horizontal tabs.
Nested use of two-tier loops.
Let's look at a second question:
There is a saying in China called "Notwith two Days". Now calculate A. If you start fishing in 2010.1.16, Q: December 1, 2010, is this person fishing or netting?
Let's analyze the problem, the start time and the cutoff time we know. So the total number of days to know, and three days to play fishing two days, we can know five days a cycle, so we use the total number of days to 5, get 0,1,2,3,4 four results, and 123 dozen fishing, 04 nets
So we have a problem is the year common year and leap years, there will be a day difference. So we first determine whether the year is a leap or common year, in the total number of days plus 1 days, or by common year calculation.
public class Returntest {public static void main (string[] args) {Returntest//.ceshi ();. Dayu ();} public static void Dayu () {int sum;//total days int y=2010;if (y%400==0| | (y%4==0&&y%100!=0)) {sum=365-15-30+1;} elsesum=365-15-30+1; String msg= (SUM/5 >=1&& (SUM/5) <=3)? " Fishing ":" Sun Net "; SYSTEM.OUT.PRINTLN ("The employee on this day" + msg);}}
Here side involves: a leap year detection method,
The only ternary expression in Java (expression 1)? Expression 2: expression 3; equivalent if (expression one) true words choose Expression 2, false words choose expression 3;
Let's take a look at the deformation of this problem
Assuming that the person started fishing work from January 1, 2000, should today be fishing or drying nets?
public class Test1 {static int[] msg2={31,28,31,30,31,30,31,31,30,31,30,31};p ublic static void Main (string[] args) {Date Date=new Date (2004, 1, 1); System.out.println (4*365+1); System.out.println (test1.way1 (date));} public static String way1 (date date) {int count1=0,count2=0;//count1: Number of leap years, Count2: Year number int day=0;//Remove the remaining days of the year for (int i= 2001;i<=date.getyear (); i++) {count2=i-2000; System.out.println ("Year:" +count2); if (i%4==0 && i%100!=0) | | i%400 = = 0) {count1++;if (Date.getmonth () <2| | (Date.getmonth () ==2&&date.getday () <29)) count1--;}} for (int j=0;j<date.getmonth () -1;j++) {day=msg2[j]+day; System.out.println ("The remainder of the day:" +day);} int sum;//to today's total days sum=day+date.getday () +365*count2+count1; SYSTEM.OUT.PRINTLN (sum); String msg= (sum%5 >= 1&& sum%5 <= 3)? " Fishing ":" NET "; return msg;}} class date{private int year;private int month;private int day;public int getYear () {return year;} public void Setyear (int.) {this.year = year;} public int GetMonth () {return month;} public void setmonth (int monTH) {this.month = month;} public int GetDay () {return day;} public void Setday (Int. day) {this.day = day;} Public Date (Int. Year,int Month,int Day) {this.year=year;this.month=month;this.day=day;}}
This is what I have just written, do not know right and wrong! Hey, wait until tomorrow to be checked by others to announce. I felt a little trouble writing myself and wrote a time class. I feel there's definitely something inside Java.
A third question
Question: The first day of the monkey picked up a number of peaches, immediately ate half, not addicted, and ate one more, the next morning, the remaining peaches eaten half, and ate one more. Every morning after eating the rest of the day half of the zero one. When I want to eat again in the morning of the 10th day, I see only one peach left. How much did you pick for the first day?
Analyze the topic:
The last day of the Monkey plus 1 times 2 is the number of days before, so we can use a loop to go backwards from day tenth.
public class Returntest {public static void main (string[] args) {Returntest//.ceshi ();//.dayu ();. Houzi ();} public static void Houzi () {int num=1;for (int i=9;i>0;i--) {num= (num+1) * *;} System.out.println ("The total number of monkeys picked is:" +num);}}
This will get the first day the number of monkeys picked peach.
Question Fourth:
There is a funny math problem in Marx's manuscript: There are 30 people, including men, women and children, who eat in a restaurant, a total of 50 shillings, 3 shillings per man, 2 shillings per woman, 1 shillings per child, and how many men, women and children do they have?
Let's analyze the problem:
30 persons must have men X, woman y, child z, and meet x+y+z=30;
A total of 50 shillings were spent, 3*x+2*y+z=50;
We can set up two equations, y=20-2x; Z=10-x, and we can judge the number of x 1-9; We can enumerate the methods to get all possible combinations.
public class Returntest {public static void main (string[] args) {Returntest//.ceshi ();//.dayu ();//.houzi ();. Chifan ();} public static void Chifan () {int x,y,z;for (x=1;x<10;x++) {y=20-2*x;z=10-x; System.out.println ("Man:" +x+ "Woman:" +y+ "Child:" +z);}}}
So we can get all the combinations of three people;
A simple algorithm problem for the third day of Java learning Diary