-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgramers_Level_0.cs
More file actions
452 lines (406 loc) · 10.6 KB
/
Programers_Level_0.cs
File metadata and controls
452 lines (406 loc) · 10.6 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Algorithm
{
public class A_1_1_OddEven
{
public string solution(int num)
{
if (num % 2 == 0) return "Even";
else return "Odd";
}
}
public class A_1_2_Average
{
public double solution(int[] arr)
{
if (arr.Length == 0) { return 0; }
double average = 0;
foreach (int i in arr)
{
average += (double)i;
}
if (average == 0) return average;
else return average / arr.Length;
}
}
public class A_1_3_ciphers
{
public int solution(int n)
{
int answer = 0;
while (n > 0)
{
answer += n % 10;
n /= 10;
}
return answer;
}
}
public class A_1_4_aliquot
{
public int solution(int n)
{
if (n > 0)
{
int sum = 0;
for (int i = 1; i <= n; i++)
{
if (n % i == 0) sum += i;
}
return sum;
}
return 0;
}
}
public class A_1_5_remainder1
{
public int solution(int n)
{
for (int i = 2; i < n; i++)
{
if (n % i == 1) return i;
}
return -1;
}
}
public class A_1_6_termx
{
public long[] solution(int x, int n)
{
long[] answer = new long[n];
for (int i = 0; i < n; i++)
{
answer[i] = (long)x * (i + 1);
}
return answer;
}
}
public class A_1_7_naturalnumberarray
{
public int[] solution(long n)
{
string number = n.ToString();
int[] answer = new int[number.Length];
for (int i = 0; i < number.Length; i++)
{
answer[i] = (int)(n % 10);
n /= 10;
}
return answer;
}
}
public class A_1_8_parseint
{
public int solution(string s)
{
return int.Parse(s);
}
}
public class A_1_9_squareroot
{
public static long solution(long n)
{
for (long answer = 1; answer * answer <= n; answer++)
{
if (answer * answer == n) return (long)((answer + 1) * (answer + 1));
}
return (long)-1;
}
}
public class A_1_10_descendingorder
{
public static long solution(long n)
{
int[] ints = new int[n.ToString().Length];
for (int i = 0; i < ints.Length; i++) { ints[i] = (int)(n % 10); n /= 10; }
Array.Sort(ints);
Array.Reverse(ints);
string s = "";
for (int i = 0; i < ints.Length; i++) { s += ints[i].ToString(); Console.Write(ints[i]); }
return long.Parse(s);
}
}
public class A_1_11_harshadnumber
{
public static bool solution(int x)
{
int _x = x;
int sum = 0;
for (int i = 0; i < _x.ToString().Length; i++)
{
sum += x % 10;
x /= 10;
}
if (_x % sum == 0) return true;
return false;
}
}
public class A_1_12_intsum
{
public static long solution(int a, int b)
{
long answer = a;
while (a != b)
{
a = a > b ? a - 1 : a + 1;
answer += a;
}
return answer;
}
}
public class A_1_13_Collatz
{
public static int solution(int num)
{
for (int i = 0; i < 500; i++)
{
if (num == 1) return i;
num = (num % 2 == 0) ? num / 2 : num * 3 + 1;
}
return -1;
}
}
public class A_1_14_Kim
{
public string solution(string[] seoul)
{
for (int i = 0; i < seoul.Length; i++)
{
if (seoul[i] == "Kim") return "김서방은 " + i.ToString() + "에 있다";
}
return "";
}
}
public class A_1_15_divisor
{
public static int[] solution(int[] arr, int divisor)
{
Array.Sort(arr);
List<int> list = new List<int>();
for (int i = 0; i < arr.Length; i++)
{
if (arr[i] % divisor == 0)
{
list.Add(arr[i]);
}
}
if (list.Count == 0) return new int[] { -1 };
return list.ToArray();
}
}
public class A_1_16_YinYang
{
public static int solution(int[] absolutes, bool[] signs)
{
int answer = 0;
for (int i = 0; i < absolutes.Length; i++)
{
answer = signs[i] ? answer + absolutes[i] : answer - absolutes[i];
}
return answer;
}
}
public class A_1_17_phone_number
{
public static string solution(string phone_number)
{
char[] answer = phone_number.ToCharArray();
for (int i = 0; i < answer.Length - 4; i++)
{
answer[i] = '*';
}
return new string(answer);
}
}
public class A_1_18_numbers
{
public int solution(int[] numbers)
{
int answer = 45;
for (int i = 0; i < numbers.Length; i++)
{
answer -= numbers[i];
}
return answer;
}
public int solution2(int[] numbers)
{
var numberArray = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
return numberArray.Except(numbers).Sum();
}
}
public class A_1_19_Remove_smallest_number
{
public static int[] solution(int[] arr)
{
if (arr.Length < 2) return new int[] { -1 };
return arr.Except(new int[] { arr.Min() }).ToArray();
}
}
public class A_1_20_center
{
public string solution(string s)
{
string answer = "";
answer = (s.Length % 2 == 0) ? s.Substring(s.Length / 2 - 1, 2) : s.Substring(s.Length / 2, 1);
return answer;
}
}
public class A_1_21_subak
{
public string solution(int n)
{
string answer = "";
for (int i = 0; i < n; i++)
{
answer += (i % 2 == 0) ? "수" : "박";
}
return answer;
}
}
public class A_1_22_inner_product
{
public int solution(int[] a, int[] b)
{
int answer = 0;
for (int i = 0; i < a.Length; i++)
{
answer += a[i] * b[i];
}
return answer;
}
}
public class A_1_23_aliquot
{
public int solution(int left, int right)
{
int answer = 0;
for (int i = left; i <= right; i++)
{
answer = (findaliquotnumber(i) % 2 == 0) ? answer + i : answer - i;
}
return answer;
}
public int findaliquotnumber(int n)
{
int count = 0;
for (int i = 1; i <= n; i++)
{
if (n % i == 0) { count++; }
}
return count;
}
}
public class A_1_24_string_descending_order
{
public string solution(string s)
{
char[] answer = s.ToCharArray();
Array.Sort(answer);
Array.Reverse(answer);
return new string(answer);
}
}
public class A_1_25_less_price
{
public long solution(int price, int money, int count)
{
long total = 0;
for (int j = 1; j <= count; j++)
{
total += (long)(price * j);
}
if (total <= money) return 0;
return (long)(total - money);
}
}
public class A_1_26_basic_string
{
public static bool solution(string s)
{
int tmp;
if (s.Length == 4 || s.Length == 6)
{
return int.TryParse(s, out tmp);
}
return false;
}
}
public class A_1_27_array_addiction
{
public int[,] solution(int[,] arr1, int[,] arr2)
{
int[,] answer = new int[arr1.GetLength(0), arr1.GetLength(1)];
for (int i = 0; i < arr2.GetLength(0); i++)
{
for (int j = 0; j < arr2.GetLength(1); j++)
{
answer[i, j] = arr1[i, j] + arr2[i, j];
}
}
return answer;
}
}
public class A_1_28_rectangle_star
{
// String[] s;
// Console.Clear();
// s = Console.ReadLine().Split(' ');
// int a = Int32.Parse(s[0]);
// int b = Int32.Parse(s[1]);
// for(int i = 0; i<b; i++){
// for(int j=0;j<a;j++){
// Console.Write("*");
// }
//Console.WriteLine();
}
public class A_1_29_greatestDivisor_LeastMultiple
{
public static int[] solution(int n, int m)
{
int greatestDivisor = 1;
int ntmp = n;
int mtmp = m;
List<int> Divisors = new List<int>();
int divisor = 2;
while ((divisor < n / 2 || divisor < m / 2))
{
if (ntmp % divisor == 0 && mtmp % divisor == 0)
{
Divisors.Add(divisor);
ntmp /= divisor;
mtmp /= divisor;
divisor = 1;
}
divisor++;
}
if (Divisors.Count == 0)
{
return new int[] { 1, n * m };
}
foreach (int i in Divisors)
{
greatestDivisor *= i;
}
return new int[] { greatestDivisor, greatestDivisor * ntmp * mtmp };
}
}
public class A_1_30_ternary_scale
{
public static int solution(int n)
{
int answer = 0;
while (n > 0)
{
answer *= 3;
answer += n % 3;
n /= 3;
}
return answer;
}
}
}