SRM ELAB SOLUTIONS OOPS
I/O Operations 2
- Play with XOR
- Professor Omkar
- Swim
- Sachin and his Pills
- Print Floyd's
Play with XOR
#include <iostream>
using namespace std;
int main()
{
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
int a[n], c1 = 0, c0 = 0;
for (int i = 0; i < n; i++)
{
cin >> a[i];
if (a[i] == 1)
c1++;
}
c0 = n - c1;
if (c1 % 2 == 0)
{
cout << c0 << endl;
}
if (c1 % 2 != 0)
{
cout << c1 << endl;
}
}
return 0;
}
Professor Omkar
#include <iostream>
using namespace std;
int main()
{
int a, b, c;
float d;
cin >> a >> b >> c;
d = (a * (c - b)) * 4184;
cout << "The energy needed is " << d;
return 0;
}
swim
#include <iostream>
using namespace std;
int main()
{
int a, b, c, s;
cin >> a >> b;
c = 3.14 * a * a;
s = b * b;
if (c > s)
cout << "I prefer centre 1";
else
cout << "I prefer centre 2";
return 0;
}
Sachin and his Pills
#include <iostream>
using namespace std;
class Month
{
int days;
public:
Month(int, int);
bool leapYear(int);
int getDays();
};
Month::Month(int number, int year)
{
if (number < 8)
(number % 2 == 1) ? (days = 31) : (days = 30);
else
(number % 2 == 0) ? (days = 31) : (days = 30);
if (number == 2)
{
if (leapYear(year) == 1)
days = 29;
else
days = 28;
}
}
bool Month::leapYear(int year)
{
if (year % 400 == 0)
return 1;
if (year % 100 == 0)
return 0;
if (year % 4 == 0)
return 1;
return 0;
}
int Month::getDays()
{
return days;
}
int pillDays(int day, int month, int year, int count = 0)
{
Month M(month, year);
int monthDays = M.getDays(), i;
for (i = day; i <= monthDays; i++)
if (i % 2 == 1)
count++;
if (monthDays % 2 == 0)
{
pillDays(1, month + 1, (month == 12) ? (year + 1) : (year), count);
}
else
return count;
}
int main()
{
int i, j, n, day, month, year, date[10];
char temp[10];
//char date[10];
cin >> n;
for (i = 0; i < n; i++)
{
cin >> temp;
for (j = 0; j < 10; j++)
date[j] = temp[j] - 48;
day = date[8] * 10 + date[9];
month = date[5] * 10 + date[6];
year = date[0] * 1000 + date[1] * 100 + date[2] * 10 + date[3];
cout << pillDays(day, month, year) << endl;
}
return 0;
}
Print Floyd's
#include <iostream>
using namespace std;
int main()
{
int n, k, i, j;
cin >> n;
k = 1;
for (i = 1; i <= n; i++)
{
for (j = 1; j <= i; j++, k++)
{
cout << k;
}
cout << endl;
}
return 0;
0 Comments