1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
|
/**
* Programmbeschreibung:
*
* Komplexitaet der Methoden in der O-Notation
*
public class test {
/*-------------------------------Methode a ------------------------------*/
static int a(int n) {
int t = 1, z = 0;
while (n > 0) {
n -= t; //ungerade Zahl wird von n abgezogem
t += 2; //ungerade Zahlen, 1, 3, 5, 7
z++;
}
return z; //Anzahl der Schritte zurückgeben
}
/*-------------------------------Methode ENDE ------------------------------*/
/*-------------------------------Methode b ------------------------------*/
//Quadriert die eingegebene Zahl.
public static int b(int n) {
int i = 0;
int b = 1;
while (++i < n) //i wird zuerst erhöht, dann damit gerechnet
b = b + 2 * i + 1;
return b; //Zahl zum Quadrat zurückgeben
}
/*-------------------------------Methode ENDE ------------------------------*/
/*-------------------------------Methode c ------------------------------*/
//Zählt die Schritte, wie oft n durch 2 geteilt wird,bis sie <=1 wird
static int c(int n) {
int z = 0;
while (n > 1) {
n /= 2; //...3/2 = 1
z++; //...dann zählt er noch eine hinzu und hört auf
}
return z;
}
/*-------------------------------Methode ENDE ------------------------------*/
/*-------------------------------Methode d ------------------------------*/
//Anzahl der Schritte, die er braucht, die quadrierte Zahl mit ungeraden
//Zahlen zu subtrahieren
static int d(int n) {
return a(b(n)); //erst wird die Zahl quadriert, dann an a weitergegeben
}
/*-------------------------------Methode d ENDE ------------------------------*/
/*-------------------------------Methode e ------------------------------*/
static int e(int n) {
return b(a(n));
}
/*-------------------------------Methode e ENDE ------------------------------*/
static int f(int n) {
return a(c(n));
}
/*-------------------------------Methode c ------------------------------*/
static int g(int n) {
return a(n) + c(n);
}
/*-------------------------------Methode c ------------------------------*/
static int h(int n) {
return b(b(n));
}
/*-------------------------------Methode c ------------------------------*/
static int i(int n) {
int z = 0;
int y = a(n);
for (int i = 1; i <= y; i++)
z += a(n);
return z;
}
/*-------------------------------Methode c ------------------------------*/
static int j(int n) {
int z = 0;
for (int i = 1; i <= a(n); i++)
z += a(n);
return z;
}
/*-------------------------------Methode c ------------------------------*/
/** ---------------------Hauptprogramm-------------------- */
public static void main(String argv[]) {
int n;
do {
n = IO.readInt("Eingabe von n: ");
}
while (n < 1);
IO.print("a(" + n + ") =");
IO.println(a(n), 8);
IO.print("b(" + n + ") =");
IO.println(b(n), 8);
IO.print("c(" + n + ") =");
IO.println(c(n), 8);
IO.print("d(" + n + ") =");
IO.println(d(n), 8);
IO.print("e(" + n + ") =");
IO.println(e(n), 8);
IO.print("f(" + n + ") =");
IO.println(f(n), 8);
IO.print("g(" + n + ") =");
IO.println(g(n), 8);
IO.print("h(" + n + ") =");
IO.println(h(n), 8);
IO.print("i(" + n + ") =");
IO.println(i(n), 8);
IO.print("j(" + n + ") =");
IO.println(j(n), 8);
}
} |