๐ฅ ์ ๋ณด์ฒ๋ฆฌ ๊ธฐ์ฌ ์ค๊ธฐ ํ๋ก๊ทธ๋๋ฐ-์ ์ด๋ฌธ
#include <stdio.h>
main() {
int s, el = 0;
for (int i = 6; i <= 30; i++) {
s = 0;
for (int j = 1; j <= i / 2; j++)
if (i % j == 0)
s = s + j;
if (s == i)
el++;
}
printf("%d", el);
}
j <= i / 2;
-> ๋๋จธ์ง๊ฐ ๋ช์ด๋ ๋ฒ๋ฆฐ๋ค.public class Test {
public static void main(String[] args) {
int r = 0;
for (int i = 1; i < 999; i++) {
if (i % 3 == 0 && i % 2 == 0)
r = i;
}
System.out.print(r);
}
}
public class Test {
public static void main(String[] args) {
int i = 3, k = 1;
switch(i) {
case 1:
k++;
case 2:
k -= 3;
case 3:
k = 0;
case 4:
k += 3;
case 5:
k -= 10;
default:
k--;
}
System.out.print(k);
}
}
1234์ ์ญ์์ 4321์ด๋ค. ๋จ 1230์ฒ๋ผ 0์ผ๋ก ๋๋๋ ์ ์๋ ๊ณ ๋ คํ์ง ์๋๋ค.
#include <stdio.h>
int main() {
int number = 1234;
int div = 10, result = 0;
while (number "( 1 )" 0) {
result = result * div;
result = result + number "( 2 )" div;
number = number "( 3 )" div;
}
printf("%d", result);
}
์ฒซ ์ฌ์ดํด
0
4
123
๋ ๋ฒ์จฐ
40
43
12
์ธ ๋ฒ์งธ
430
432
1
๋ค ๋ฒ์งธ
4320
4321
0
public class Test {
public static void main(String[] args) {
int w = 3, x = 4, y = 3, z = 5;
if((w == 2 | w == y) & !(y > z) & (1 == x ^ y != z)) {
w = x + y;
if(7 == x ^ y != w)
System.out.println(w);
else
System.out.println(x);
}
else {
w = y + z;
if(7 == y ^ z != w)
System.out.println(w);
else
System.out.println(z);
}
}
}
& , | -> ๋นํธ ์ฐ์ฐ์ผ๋ก์จ, 2์ง๋ฒ์ ๋นํธ์ฐ์ฐํ๋ฉด ๋๋ค.
x ^ y ์ฐ์ฐ์ ๋นํธ XOR
์ฐ์ฐ์ผ๋ก์จ,
001 -> y != z
000 -> 1 == x
--- (๊ฐ์ผ๋ฉด 0 / ๋ค๋ฅด๋ฉด 1)
001 -> 4 ๊ฐ ๋๋ค.
a = 100
result = 0
for i in range(1,3):
result = a >> i
result = result + 1
print(result)
100์ ๋จผ์ 2์ง์๋ก ๋ณํ 0000 ... 0001100100 -> 100 (4 + 32 + 64)
์ํํธ ์ฐ์ฐ์ ํ๋ฒ ์ํํ๋ฉด 0000 ... 0000110010 -> 50 (2 + 16 + 32) +1 ์ ํ๋ฉด 51 -> 110011
ํ๋ฒ ๋ ์ํํ๋ฉด 100์ ๋์นธ ์ํํธ ํ๋ค 0000 ... 0000011001 -> 25 (1 + 8 + 16) 25+1 = 26
print
๊ธฐ๋ณธ์ ์ผ๋ก ํ์ด์ฌ์ print
๋ print(end='\n')
์ด ํฌํจ๋ ์์ด๋ค. (์ค๋ฐ๊ฟ ๋ํํธ)
์ฆ print("123", end=' '
)๋ก ํํํ๋ฉด ์ค๋ฐ๊ฟ ๋์ ๊ณต๋ฐฑ์ด ์ฝ์
๋๋ค.
public class Test {
public static void main(String[]args) {
int a[] = new int[8];
int i = 0;
int n = 10;
while( "( 1 )" ) {
a[i++] = "( 2 )";
n /= 2;
}
for(i = 7; i >= 0; i--)
System.out.print(a[i]);
}
}
n /= 2;
๋ n = n / 2
๋ ๋์ผํ ๋ฌธ๋ฒ์ด๋ค.๋จผ์ , 10์ง์๋ฅผ 2์ง์๋ก ๋ณํํ๊ธฐ ์ํด์๋, 2๋ก ๊ณ์ํด์ ๋๋์ด, ๋๋จธ์ง๋ฅผ ์ฝ์ ํ๋ฉด๋๋ค.
10 / 2 = 5(0)
5 / 2 = 2(1)
2 / 2 = 1(0)
(1)
10 -> 1010 (8+2)
19 / 2 = 9(1)
9 / 2 = 4(1)
4 / 2 = 2(0)
2 / 2 = 1(0)
(1)
19 -> 10011 (16+2+1)
JAVA๋ ๋ฐฐ์ด ์ ์ธ์ ์ด๊ธฐํ๋ฅผ ํ์ง ์์ผ๋ฉด 0
์ด ์ฝ์
๋๋ค
a[i++] = x
๋ฅผ ์ํํ๋ฉด i๋ฒ์งธ ์๋ฆฌ์ ๊ฐ ์ฝ์
ํ, i๊ฐ์ ์ฆ๊ฐ์ํจ๋ค.
00001010
์ด ๋๋ค.public class Main {
static void func(String[] m, int n) {
for (int i = 1; i < n; i++) {
if (m[i-1].equals(m[i])) {
System.out.print("O");
} else {
System.out.print("N");
}
}
for (String mo : m) {
System.out.print(mo);
}
}
public static void main(String[] args) {
String[] m = new String[3];
m[0] = "A";
m[1] = "A";
m[2] = new String("A");
func(m, 3);
}
}
.equals
ํจ์๋ ๊ฐ์ฒด๊ฐ ์๋ ๊ฐ ์์ฒด๋ฅผ ๋น๊ตํ๊ธฐ ๋๋ฌธ์ ๊ฐ์ ๊ฐ์ผ๋ก ์ทจ๊ธํ๋ค.def test(lst):
for i in range(len(lst) // 2):
lst[i], lst[-i-1] = lst[-i-1], lst[i]
ls = [1,2,3,4,5,6]
test(ls)
print(sum(ls[::2]) - sum(ls[1::2]))
list[a::b]
ํํ๋ a๋ถํฐ ์์ํด์(์๋ค๋ฉด 0) b์นธ์ฉ ๊ฑด๋ ๋ฐ์ด ๊ฐ์ ธ์ค๋ ๊ฒ.