-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasicprogramming1.cpp
More file actions
114 lines (106 loc) · 2.3 KB
/
basicprogramming1.cpp
File metadata and controls
114 lines (106 loc) · 2.3 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
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
// Get inputs
int n, t;
cin >> n >> t;
int a[n];
for(int i = 0; i < n; i++)
{
cin >> a[i];
}
if(t == 1)
{
cout << "7" << endl;
}
else if(t == 2)
{
if(a[0] > a[1])
{
cout << "Bigger" << endl;
}
else if(a[0] == a[1])
{
cout << "Equal" << endl;
}
else
{
cout << "Smaller" << endl;
}
}
else if(t == 3)
{
vector<int> nums = {a[0], a[1], a[2]}; // Get the first 3 numbers
sort(nums.begin(), nums.end()); // Sort the numbers
cout << nums[1] << endl; // Get the median(middle)
}
else if(t == 4)
{
long long total = 0;
for(int i = 0; i < n; i++)
{
total += a[i];
}
cout << total << endl;
}
else if(t == 5)
{
long long total = 0;
for(int i = 0; i < n; i++)
{
if((a[i] % 2) == 0) // Only add even numbers to the total
{
total += a[i];
}
}
cout << total << endl;
}
else if(t == 6)
{
for(int i = 0; i < n; i++)
{
// Convert the number to a character index using % 26, and map this
// to the correct ASCII value by adding 97 to get to the lower case
// letter
cout << static_cast<char>((a[i] % 26) + 97);
}
cout << endl;
}
else
{
int i = 0; // Starting index
bool seen[n]; // Track seen indices
for(int i = 0; i < n; i++)
{
seen[i] = false;
}
// Until one of the conditions has been met
while(true)
{
i = a[i]; // Get the next index
if(i >= n)
{
cout << "Out" << endl;
break;
}
else if(i == (n-1))
{
cout << "Done" << endl;
break;
}
else if(seen[i])
{
cout << "Cyclic" << endl;
break;
}
else
{
seen[i] = true;
}
}
}
return 0;
}