Lee87
Tổng số bài gửi : 71 Points : 107 Reputation : 5 Join date : 10/07/2009 Age : 37 Đến từ : Hà Nội
| Tiêu đề: Bài tập trên lớp số 4 đây. 13/7/2009, 8:37 pm | |
| Lee xin post bài Lee+UyenNhi đã làm với nhau nè.Các bạn ai có làm các bài tập này với những thuật toán khác nhau thì post lên cho mọi người cùng tham khảo nhé.
Ex1.java - Code:
-
import java.util.Scanner; public class Ex1 { //su dung thuat toan de quy static int GT(int n){ if(n==0 ||n==1) return(1); return(n*GT(n-1)); // tra ve gia tri n giai thua } //ham in ra hinh vuong static void In(int n){ if(GT(n)%5==0){ for(int j=1;j<=n*n;j++) { System.out.printf("%02d ",j); if(j%n==0) System.out.print("\n"); } } } public static void main(String[] chuoi){ int n; Scanner input = new Scanner(System.in); System.out.print("Nhap n = "); n = input.nextInt(); System.out.print("\n"+n+"! = "+GT(n)+"\n\n"); In(n); } }
Ex2.java - Code:
-
import java.util.Scanner; public class Ex2 { static Scanner scan = new Scanner(System.in); static Scanner input = new Scanner(System.in); static String Ten; static int Dt,Dl,Dh; static void Iput(){ System.out.print("\n\t\tNhap ten: "); Ten = input.nextLine(); } static void Oput(){ System.out.print("\n\t\t"+Ten); } static void Idiem(){ System.out.print("\n\t\tDiem Toan: "); Dt = scan.nextInt(); System.out.print("\n\t\tDiem Ly: "); Dl = scan.nextInt(); System.out.print("\n\t\tDiem Hoa: "); Dh = scan.nextInt(); } static void DTB(){ System.out.print("\n\t\tDiem TB: "+(float)(Dt+Dl+Dh)/3); } static void Duyet(){ int choose; while(true){ System.out.print("\n\n\t0.Thoat khoi CT"); System.out.print("\n\t1.Nhap ten."); System.out.print("\n\t2.Hien thi ten."); System.out.print("\n\t3.Nhap diem."); System.out.print("\n\t4.Tinh diem trung binh."); System.out.print("\n\tchoose:"); choose = scan.nextInt(); switch(choose){ case 1: { Iput();break; } case 2: { Oput();break; } case 3: { Idiem();break; } case 4: { DTB();break; } case 0: System.exit(0); } } } public static void main(String[] chuoi){ Duyet(); } }
Ex3.java - Code:
-
import java.util.Scanner; public class Ex3 { static void Kim_Tu_Thap(int n) { for(int dong = 1;dong<=n;dong++) { for(int cach=1;cach<=(n-dong);cach++) System.out.print(" "); int dem =dong-1; for(int cot = 1;cot<=((2*dong)-1);cot++) { if(cot<=dong) System.out.printf("%5d",(int)Math.pow(2, cot-1)); else{ dem--; System.out.printf("%5d",(int)Math.pow(2,dem)); if(dem==0) break; } } System.out.print("\n"); } } public static void main(String [] chuoi){ int n; Scanner scan = new Scanner(System.in); System.out.print("Tam giac co so dong = "); n = scan.nextInt(); Kim_Tu_Thap(n); } }
Ex4.java - Code:
-
import java.util.Scanner;
public class Ex4 { // Phan a static void Tam_Giac_A(int n) { for(int dong = 1;dong<=n;dong++){ for(int cot = 1;cot<=dong;cot++) { System.out.print("*"); } System.out.print("\n"); } } //Phan b static void Tam_Giac_B(int n) { for(int dong = 1;dong<=n;dong++){
for(int cot=(n-dong+1);cot>0;cot--) { System.out.print("*"); } System.out.print("\n"); } } //Phan c static void Tam_Giac_C(int n) { for(int dong = 1;dong<=n;dong++){ for(int cach=1;cach System.out.print(" "); for(int cot = 1;cot<=(n-dong+1);cot++) { System.out.print("*"); } System.out.print("\n"); } } //Phan d static void Tam_Giac_D(int n) { for(int dong = 1;dong<=n;dong++){ for(int cot = 1;cot<=n;cot++) { if(cot<=(n-dong)) System.out.print(" "); else System.out.print("*"); } System.out.print("\n"); } } // Duyet tat ca cac chuong trinh static void Duyet(int n){ Scanner input = new Scanner(System.in); //Input(n); while(true){ System.out.print("\n0. Thoat khoi CT."); System.out.print("\n1. Phan a."); System.out.print("\n2. Phan b."); System.out.print("\n3. Phan c."); System.out.print("\n4. Phan d."); int choose; System.out.print("\nChoose: "); choose = input.nextInt(); switch(choose){ case 1: {Tam_Giac_A(n);break;} case 2: {Tam_Giac_B(n);break;} case 3: {Tam_Giac_C(n);break;} case 4: {Tam_Giac_D(n);break;} case 0: System.exit(0); } } }
// Ham Main public static void main(String[] chuoi){ int n; Scanner input = new Scanner(System.in); System.out.print("\nSo dong cua tam giac: "); n = input.nextInt(); Duyet(n); } }
Chúc các bạn học tốt. | |
|