thieumao

Liệt kê n kí tự

Jul 25th, 2013
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.76 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class testtunhiphan {
  4.  
  5.     public static void main(String[] arg) {
  6.         int dem = 1;
  7.         System.out.print("Nhap chieu dai: ");
  8.         Scanner scn = new Scanner(System.in);
  9.         int n = scn.nextInt();
  10.         char a[] = new char[n];
  11.         int last = 0;
  12.  
  13.         for (int i = 0; i < n; i++) {
  14.             a[i] = 'a';
  15.         }
  16.  
  17.         while (last < n) {
  18.             // in ra dãy bit
  19.             System.out.print(dem++ + " ");
  20.             for (int i = 0; i < n; i++) {
  21.                 System.out.print(a[i]);
  22.             }
  23.             System.out.println();
  24.             // nếu bit ở vị trí last chuyển thành z thì nhảy lên một bước.
  25.             if (a[last] == 'z')
  26.                 last += 1;
  27.  
  28.             for (int i = n - 1; i >= 0; i--) {
  29.                 if (a[i] != 'z') {
  30.                     a[i]++;
  31.                     break;
  32.                 } else {
  33.                     a[i] = 'a';
  34.                 }
  35.             }
  36.         }
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment