-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasicprogramming1.cs
More file actions
136 lines (116 loc) · 3.08 KB
/
basicprogramming1.cs
File metadata and controls
136 lines (116 loc) · 3.08 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
// Kattis : Basic Programming 1
using System;
using System.Linq;
using System.Collections.Generic;
namespace basicprogramming1
{
class Program
{
public static void Main(string[] args)
{
string line = Console.ReadLine();
string[] arr = line.Split(' ');
int[] nt = Array.ConvertAll(arr, int.Parse);
line = Console.ReadLine();
arr = line.Split(' ');
int[] A = Array.ConvertAll(arr, int.Parse);
string res = "";
switch(nt[1])
{
case 1:
res = "7";
break;
case 2:
res = Compare(A[0],A[1]);
break;
case 3:
res = Median(A[0],A[1],A[2]).ToString();
break;
case 4:
res = Sum(A).ToString();
break;
case 5:
res = SumNumMod(A).ToString();
break;
case 6:
res = MapIntMod26ToAlphabetLower(A);
break;
case 7:
res = SpecialProcedure(A);
break;
}
System.Console.WriteLine(res);
}
static string Compare(int a, int b)
{
string res;
if(a > b)
{
res = "Bigger";
}
else if(a == b)
{
res = "Equal";
}
else
{
res = "Smaller";
}
return res;
}
static int Median(params int[] arr)
{
Array.Sort(arr);
return arr[1];
}
static long Sum(int[] arr)
{
long sum = 0;
foreach (var item in arr)
{
sum += item;
}
return sum;
}
static long SumNumMod(int[] arr, int num = 2, int mod = 0)
{
long sum = Sum(arr.Where( i => i % num == mod).ToArray());
return sum;
}
static string MapIntMod26ToAlphabetLower(int[] arr)
{
char[] res = new char[arr.Length];
int j = 0;
foreach (var item in arr)
{
int i = item % 26;
char c = (char) (i + 'a');
res[j] = c;
j++;
}
return new string(res);
}
static string SpecialProcedure(int[] arr)
{
int i = 0;
HashSet<int> set = new HashSet<int>();
while(true)
{
i = arr[i];
if(i > arr.Length - 1)
{
return "Out";
}
else if(i == arr.Length - 1)
{
return "Done";
}
else if(set.Contains(i))
{
return "Cyclic";
}
set.Add(i);
}
}
}
}