Skip to content

Instantly share code, notes, and snippets.

@LongClipeus
Last active July 24, 2019 08:01
Show Gist options
  • Save LongClipeus/9a272f6a4b09eb44ca5ce69e46f9adb3 to your computer and use it in GitHub Desktop.
Save LongClipeus/9a272f6a4b09eb44ca5ce69e46f9adb3 to your computer and use it in GitHub Desktop.
Training java
import java.util.Scanner;
/**
* Display the calendar of any year
*
* @author LongClipeus
*
*/
public class Date {
static int day;
static int month;
static int year;
/**
* Biggest day of the month
*/
static int dayMaxMonth;
static String can[] = { "Canh", "Tân", "Nhâm", "Quy", "Giáp", "Ất", "Bính",
"Đinh", "Mậu", "Kỷ" };
static String chi[] = { "Thân", "Dậu", "Tuất", "Hợi", "Tý", "Sửu", "Dần",
"Mão", "Thìn", "Tỵ", "Ngọ", "Mùi" };
/**
* enter year
*/
public static void inputYear() {
System.out.print("Nhập vào năm: ");
Scanner input = new Scanner(System.in);
year = input.nextInt();
while (year < 1) {
System.out.print("Bạn nhập không đúng, xin mời nhập lại: ");
input = new Scanner(System.in);
year = input.nextInt();
}
}
/**
* enter month
*/
public static void inputMonth() {
System.out.print("Nhập vào tháng: ");
Scanner input = new Scanner(System.in);
month = input.nextInt();
while (month < 1 || month > 12) {
System.out.print("Bạn nhập không đúng, xin mời nhập lại: ");
input = new Scanner(System.in);
month = input.nextInt();
}
}
/**
* request to enter the correct date and must be in the calendar
*/
public static void inputDay() {
System.out.print("Nhập vào ngày: ");
Scanner input = new Scanner(System.in);
day = input.nextInt();
dayMaxMonth();
while (day < 1 || day > dayMaxMonth) {
System.out.print("Bạn nhập không đúng, xin mời nhập lại: ");
input = new Scanner(System.in);
day = input.nextInt();
}
}
/**
*
* @return The name of the lunar year
*/
public static String getLunarCalendar() {
return can[year % 10] + " " + chi[year % 12];
}
/**
* Is the year a leap year?
*
* @return true if it is leap year else return false
*/
public static boolean checkLeapYear() {
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
return true;
else
return false;
}
/**
* Calculate the number of days in the month
*/
public static void dayMaxMonth() {
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8
|| month == 10 || month == 12) {
dayMaxMonth = 31;
} else if (month == 2) {
if (checkLeapYear())
dayMaxMonth = 29;
else
dayMaxMonth = 28;
} else
dayMaxMonth = 30;
}
/**
* Calculate the weekday by day, month and year
*
* @return The number is valid from 0 to 6 (from Sunday to Saturday)
*/
public static int calendar() {
int a = (14 - month) / 12;
int y = year - a;
return (day + y + y / 4 - y / 100 + y / 400
+ (31 * (month + 12 * a - 2)) / 12) % 7;
}
/**
* Print out the calendar of the year
*/
public static void showCalendar() {
for (int i = 0; i < 12;) {
++i;
System.out.println("\n\nTháng " + i + ":");
System.out.println("CN\tThứ 2\tThứ 3\tThứ 4\tThứ 5\tThứ 6\tThứ 7");
day = 1;
month = i;
int skip = calendar();
// show space before the 1st of every month
for (int j = 0; j < skip; ++j) {
System.out.print("\t");
}
dayMaxMonth();
// show the day of the month
for (int j = 0; j < dayMaxMonth;) {
++j;
System.out.print(" " + j);
if (skip == 6) {
System.out.println();
skip = 0;
} else {
System.out.print("\t");
skip++;
}
}
}
}
public static void main(String[] args) {
inputYear();
System.out.println("\nNăm " + year + " là năm " + getLunarCalendar());
showCalendar();
}
}
import java.util.Scanner;
/**
* Chương trình kiểm tra ma trận có hàng hoặc cột chứa các phần tử 0
*
*/
public class Matrix {
static double[][] matrix;
static int n;
public static boolean isZero() {
if (isZeroCol())
return true;
if (isZeroRow())
return true;
return false;
}
public static boolean isZeroRow() {
int i, cnt;
for (i = 0; i < n; ++i) {
cnt = 0;
while (cnt < n && matrix[i][cnt] == 0) {
++cnt;
}
if (cnt == n)
return true;
}
return false;
}
public static boolean isZeroCol() {
int i, cnt;
for (i = 0; i < n; ++i) {
cnt = 0;
while (cnt < n && matrix[cnt][i] == 0) {
++cnt;
}
if (cnt == n)
return true;
}
return false;
}
public static void showMatrix() {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
System.out.print(matrix[i][j] + "\t");
}
System.out.println();
}
}
public static void inputMatrix() {
Scanner in = new Scanner(System.in);
System.out.print("n = ");
n = in.nextInt();
matrix = new double[n][n];
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
System.out.print("Matrix[" + i + "][" + j + "] = ");
matrix[i][j] = in.nextDouble();
}
}
}
public static void main(String[] args) {
inputMatrix();
showMatrix();
System.out.println(isZero());
}
}
/**
* Tìm phần tử lẻ đầu tiên của mảng
* Không dùng break
*/
public class OddArr {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n;
boolean check = true;
System.out.print("n = ");
n = in.nextInt();
while(n < 1) {
System.out.print("Nhap lai n: ");
n = in.nextInt();
}
int[] numbers = new int[n];
for (int i = 0; i < n; ++i) {
System.out.print("nhap vao phan tu thu " + i + ": ");
numbers[i] = in.nextInt();
}
for(int i = 0; i < n; ++i) {
if(numbers[i] % 2 != 0 && check) {
System.out.println("\nSo le dau tien cua mang la: " + numbers[i]);
check = false;
}
}
if (check) {
System.out.println("\nKhong co so le trong mang");
}
}
}
import java.util.Scanner;
/**
* Nhập vào một mảng phần tử độ dài n bất kỳ, đảo ngược mảng vừa nhập vào
*/
public class SwapArr {
public static void main(String[] args) {
int n;
Scanner in = new Scanner(System.in);
System.out.print("n = ");
n = in.nextInt();
while(n < 2) {
System.out.print("Nhap lai n (n > 1): ");
n = in.nextInt();
}
int[] numbers = new int[n];
for (int i = 0; i < n; ++i) {
System.out.print("Numbers[" + i + "] = ");
numbers[i] = in.nextInt();
}
System.out.println("\nTrước khi đảo ngược mảng:");
for(int i : numbers) {
System.out.print(i + "\t");
}
int left = 0, right = n - 1;
int temp;
while (left < right) {
temp = numbers[right];
numbers[right] = numbers[left];
numbers[left] = temp;
++left;
--right;
}
System.out.println("\nSau khi đảo ngược mảng:");
for(int i : numbers) {
System.out.print(i + "\t");
}
}
}
import java.util.Scanner;
/**
* Viết chương trình phân tích một số nguyên thành các thừa số nguyên tố
* vd: Số 28 được phân tích thành 2 x 2 x 7
*/
public class ThuaSoNguyenTo {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("n = ");
int n = in.nextInt();
int m = n;
boolean check = true;
for (int i = 2; i <= n; ++i) {
while (m % i == 0) {
if (check) {
System.out.print(i);
check = false;
} else
System.out.print(" x " + i);
m /= i;
}
}
// System.out.println("n");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment