Zbiory potęgowe

Liczby binarne

Konwersja liczb binarnych na system dziesiętny:

 3210
(1010)2 = 0*20 + 1*21 + 0*22 + 1*23 = 2 + 8 = 10

Konwersja przy pomocy tabeli:

 8  4  2  1
[1][0][1][0]   (1010)2 = 2 + 8 = 10

Dodawanie binarne

0 + 0 = 0
0 + 1 = 1
1 + 0 = 1
1 + 1 = 10
1 + 1 + 1 = 11

Generowanie podzbiorów

a b c

0 0 0   ->   { }
0 0 1   ->   { c }
0 1 0   ->   { b }
0 1 1   ->   { b c }
1 0 0   ->   { a }
1 0 1   ->   { a c }
1 1 0   ->   { a b }
1 1 1   ->   { a b c }
/* Podzbiory1.java */

public class Podzbiory1
{
  public static void main(String[] args)
  {
    int b0, b1, b2;
    
    for (b0 = 0; b0 <= 1; b0++)
      for (b1 = 0; b1 <= 1; b1++)
        for (b2 = 0; b2 <= 1; b2++)
        {
          System.out.print("{ ");
          
          if (b0 == 1) System.out.print("a ");
          if (b1 == 1) System.out.print("b ");
          if (b2 == 1) System.out.print("c ");
          
          System.out.println("}");
        }
  }
}
{ }
{ c }
{ b }
{ b c }
{ a }
{ a c }
{ a b }
{ a b c }
Press any key to continue...

Zadanie Dokonaj analizy programu Podzbiory1.

b0, b1, b2

b0 = 0
0 <= 1   b1 = 0
         0 <= 1   b2 = 0
                  0 <= 1   '{ '  0 == 1  false  0 == 1  false  0 == 1  false  "}"   b2 = 1
                  1 <= 1   '{ '  0 == 1  false  0 == 1  false  1 == 1  'c '   "}"   b2 = 2
                  2 <= 1   false
         b1 = 1
         1 <= 1   b2 = 0
                  0 <= 1   '{ '  0 == 1  false  1 == 1  'b '   0 == 1  false  "}"   b2 = 1
                  1 <= 1   '{ '  0 == 1  false  1 == 1  'b '   1 == 1  'c '   "}"   b2 = 2
                  2 <= 1   false
         b1 = 2
         2 <= 1   false
b0 = 1
1 <= 1   b1 = 0
         0 <= 1   b2 = 0
                  0 <= 1   '{ '  1 == 1  'a '   0 == 1  false  0 == 1  false  "}"   b2 = 1
                  1 <= 1   '{ '  1 == 1  'a '   0 == 1  false  1 == 1  'c '   "}"   b2 = 2
                  2 <= 1   false
         b1 = 1
         1 <= 1   b2 = 0
                  0 <= 1   '{ '  1 == 1  'a '   1 == 1  'b '   0 == 1  false  "}"   b2 = 1
                  1 <= 1   '{ '  1 == 1  'a '   1 == 1  'b '   1 == 1  'c '   "}"   b2 = 2
                  2 <= 1   false
         b1 = 2
         2 <= 1   false
b0 = 2
2 <= 1   false
/* Podzbiory2.java */

public class Podzbiory2
{
  public static void main(String[] args)
  {
    final int N = 3; // moc zbioru postaci {a, b, c, ...}
    int[] b = new int[N];
		
    int i = b.length - 1;
		
    while (i >= 0)
    {
      System.out.print("{ ");
      
      for (int j = 0; j < b.length; j++)
      	if (b[j] == 1) System.out.print((char)('a' + j) + " ");
      
      System.out.println("}");
      
      i = b.length - 1;
      
      while (i >= 0)
      {
        if (b[i] == 1)
        {
          b[i] = 0;
          i--;
        }
        else
        {
          b[i] = 1;
          break;
        }
      }
    }
  } 
}
{ }
{ c }
{ b }
{ b c }
{ a }
{ a c }
{ a b }
{ a b c }
Press any key to continue...

Zadanie Dokonaj analizy programu Podzbiory2.

N = 3
b = {0, 0, 0}

i = 3 - 1 = 2

2 >= 0   '{ '
         
         j = 0
         0 < 3   b[0] == 1   false   j = 1
         1 < 3   b[1] == 1   false   j = 2
         2 < 3   b[2] == 1   false   j = 3
         3 < 3   false
         
         "}"

         i = 3 - 1 = 2
         
         2 >= 0   b[2] == 1   false   b[2] = 1   break
         
2 >= 0   '{ '

         j = 0
         0 < 3   b[0] == 1   false   j = 1
         1 < 3   b[1] == 1   false   j = 2
         2 < 3   b[2] == 1   'c '    j = 3
         3 < 3   false

         "}"
         
         i = 3 - 1 = 2
         
         2 >= 0   b[2] == 1   true    b[2] = 0   i = 1
         1 >= 0   b[1] == 1   false   b[1] = 1   break
         
1 >= 0   '{ '

         j = 0
         0 < 3   b[0] == 1   false   j = 1
         1 < 3   b[1] == 1   'b '    j = 2
         2 < 3   b[2] == 1   false   j = 3
         3 < 3   false

         "}"
         
         i = 3 - 1 = 2
         
         2 >= 0   b[2] == 1   false   b[2] = 1   break
         
2 >= 0   '{ '

         j = 0
         0 < 3   b[0] == 1   false   j = 1
         1 < 3   b[1] == 1   'b '    j = 2
         2 < 3   b[2] == 1   'c '    j = 3
         3 < 3   false

         "}"

         i = 3 - 1 = 2

         2 >= 0   b[2] == 1   true    b[2] = 0   i = 1
         1 >= 0   b[1] == 1   true    b[1] = 0   i = 0
         0 >= 0   b[0] == 1   false   b[0] = 1   break
         
0 >= 0   '{ '

         j = 0
         0 < 3   b[0] == 1   'a '    j = 1
         1 < 3   b[1] == 1   false   j = 2
         2 < 3   b[2] == 1   false   j = 3
         3 < 3   false

         "}"

         i = 3 - 1 = 2

         2 >= 0   b[2] == 1   false   b[2] = 1   break

2 >= 0   '{ '

         j = 0
         0 < 3   b[0] == 1   'a '    j = 1
         1 < 3   b[1] == 1   false   j = 2
         2 < 3   b[2] == 1   'c '    j = 3
         3 < 3   false

         "}"

         i = 3 - 1 = 2

         2 >= 0   b[2] == 1   true    b[2] = 0   i = 1
         1 >= 0   b[1] == 1   false   b[1] = 1   break

1 >= 0   '{ '

         j = 0
         0 < 3   b[0] == 1   'a '    j = 1
         1 < 3   b[1] == 1   'b '    j = 2
         2 < 3   b[2] == 1   false   j = 3
         3 < 3   false

         "}"

         i = 3 - 1 = 2

         2 >= 0   b[2] == 1   false   b[2] = 1   break
         
2 >= 0   '{ '

         j = 0
         0 < 3   b[0] == 1   'a '    j = 1
         1 < 3   b[1] == 1   'b '    j = 2
         2 < 3   b[2] == 1   'c '    j = 3
         3 < 3   false

         "}"

         i = 3 - 1 = 2

         2 >= 0   b[2] == 1   true   b[2] = 0   i = 1
         1 >= 0   b[1] == 1   true   b[1] = 0   i = 0
         0 >= 0   b[0] == 1   true   b[0] = 0   i = -1
         
-1 >= 0  false

Strona główna