# IIUM Code Jam Practice

Assalamualaikum everyone. IIUM Code Jam is just around the corner and we think that it is best if we give out some practice set. For this practice, we are going to use an online platform that we commonly use to train for an actual ACM ICPC. Its basically an online contest. However, the question that we set are very easy to cater for those who are really new competitive programming. Or in another word, those who are joining the IIUM Code Jam just for the fun of it. In fact, in this post, I’ll be discussing on the answers for the question. However, you should try it yourself first. The contest is available through the link below. The password is “redjam”.

IIUM Code Jam Practice Contest

The winner for the practice contest will get nothing. Because it is just a practice. More importantly, if you are really new to competitive programming, this can give you an edge in IIUM Code Jam against other student who are also new, but did not do the practice. Again, try to solve the problem first, then read the spoiler below.

## Problem A

In this problem, what you need to do is determine if the case of the word need to be changed. If it seems that it need to be changed, then output the changed word. If not, then output the original one. It need to be changed, if:

- It only contains uppercase letters;
- if all letters except for the first one are uppercase.

Actually, if you think about it, you just need to check for all letter except for the first one for the uppercase. That should cater for both the condition. This is my solution:

#include<iostream> using namespace std; int main(int argv, char** argc){ // s is the string that we are testing string s; cin >> s; // Flag if need to swap bool need_to_swap = true; // If at least one of the string letter // except the first letter is a lowercase letter, // then we don't need to swap for(int i=1;i<s.size();i++){ // islower is a builtin function if(islower(s[i])){ need_to_swap = false; } } // If we need to swap, if(need_to_swap){ // Loop each character for(int i=0;i<s.size();i++){ // Get the character char c = s[i]; if(islower(c)){ // If the character is lowercase // Uppercase it, buy subtracting it with the distance between the smallcase // 'a' and large case 'A'. In ASCII 'a' has a higher value than 'A' // and all other letter occur after 'a', in normal sequence like we are used too. c = c-('a'-'A'); }else{ // Else, add it by the distance between the smallcase 'a' and largecase 'A' c = c+('a'-'A'); } // Put it back s[i] = c; } // Output it cout << s; }else{ // Else, just output the string unchanged cout << s; } return 0; }

In Java

import java.io.PrintStream; import java.util.*; public class Main{ static Scanner in; static PrintStream out; public static void main(String[] args){ in = new Scanner(System.in); out = System.out; // s is the string that we are testing String s = in.next(); // Flag if need to swap Boolean need_to_swap = true; // If at least one of the string letter // except the first letter is a lowercase letter, // then we don't need to swap for(int i=1;i<s.length();i++){ // Character.isLowerCase is a builtin function if(Character.isLowerCase(s.charAt(i))){ need_to_swap = false; } } // If we need to swap, if(need_to_swap){ // To modify a string easily, we use a StringBuilder StringBuilder builder = new StringBuilder(s); // Loop each character for(int i=0;i<s.length();i++){ // Get the character char c = s.charAt(i); if(Character.isLowerCase(c)){ // If the character is lowercase // Uppercase it, by calling a built in function. c = Character.toUpperCase(c); }else{ // Else, lowercase it c = Character.toLowerCase(c); } // Set the character builder.setCharAt(i,c); } // Output it out.println(builder.toString()); }else{ // Else, just output the string unchanged out.println(s); } } }

## Problem B

In problem B, essentially, what you need to do is to get the difference of the area between a square and a circle. You are given the radius of the circle. And the width of the square is 2*radius. So you just need to find (r+r)*(r+r) – (r*r*PI). The PI is given. It is not a number, but a function call which is 2*acos(0.0). Remember, cos(90 degree) is 0. So acos(0) is 90 degree. But computer function works in radian. So 90 degree is equal to PI/2 radian. That is why 2*acos(0) == PI. acos is the inverse of cos by the way. And its available with the ‘cmath’ header.

The other problem is that, the output need to be in two decimal place. In C++ you can do so, using *precision* function. In Java, you can use the DecimalFormat class. Checkout my solution below to see how I do it:

#include<iostream> #include<cmath> using namespace std; // This macro, define PI as in the question. // Whenever 'PI' occur, it will be replaced with 2*acos(0.0) #define PI 2*acos(0.0) double solve(){ // r is the radius double r; cin >> r; // Calculate the rectangle area double rectangle_area = (r*2)*(r*2); // Calculate the circle area. // For those who don't remember, the formula for area of a circle is // (r^2)*PI. Pronounced as "radius squared times pi" double circle_area = (r*r*PI); // The rectangle area minus the circle area return rectangle_area - circle_area; } int main(int argv, char** argc){ // The output must be in two decimal place // So the first line make sure the decimal place shows // Even if it has no value after the decimal place. For example, "2123.00" // The precision make sure it have two decimal place. cout << fixed; cout.precision(2); // t is number of test case int t; cin >> t; // We run this t times for(int i=0; i<t; i++){ // Each time, we output the result of 'solve' function cout << "Case " << i+1 << ": " << solve() << endl; } }

In Java

import java.io.PrintStream; import java.util.*; import java.text.*; public class Main{ static Scanner in; static PrintStream out; // This function, define PI as in the question. // To use it, we call the function static double PI(){ return 2*Math.acos(0.0); } static double solve(){ // r is the radius double r = in.nextDouble(); // Calculate the rectangle area double rectangle_area = (r*2)*(r*2); // Calculate the circle area. // For those who don't remember, the formula for area of a circle is // (r^2)*PI. Pronounced as "radius squared times pi" double circle_area = (r*r*PI()); // The rectangle area minus the circle area return rectangle_area - circle_area; } public static void main(String[] args){ in = new Scanner(System.in); out = System.out; // The output must be in two decimal place // This DecimalFormat will be used to do so DecimalFormat numberformat = new DecimalFormat("#.00"); // t is number of test case int t = in.nextInt(); // We run this t times for(int i=0; i<t; i++){ double answer = solve(); // Each time, we output the result of 'solve' function out.println("Case " + (i+1) + ": " + numberformat.format(answer)); } } }

## Problem C

Problem C may be a bit intimidating for beginner. You see a grid and say “Oh God! How do I input this?”. Its actually quite simple, just input it one by one. The problem involve, getting the distance for the ‘1’ from the center of the grid. Or to put it in another word, how many movement do ‘1’ need to make so that it goes to the middle of the grid.

My approach is simple, just get the coordinate for the ‘1’ and then determine the distance of that coordinate with the coordinate (‘2′,’2’) which is the middle. I am using a 0 indexed array, so the coordinate start with 0. To get the coordinate of ‘1’, just check for ‘1’ while inputting and at the same time, keep track of the coordinate of the current input. Checkout my solution:

#include<iostream> #include<cmath> using namespace std; int main(int argv, char** argc){ // We just need to x and y position for the 1 // So we define the variable first, that we will use. int posx; int posy; // Then we loop through the y for(int y=0;y<5;y++){ // And the x for(int x=0;x<5;x++){ // And at the same time, we input the number int num; cin >> num; // If the number is 1, then update posx and posy if(num == 1){ posx = x; posy = y; } } } // Then, we need to get the absolute different between // the position of the '1' and the center. Which is coordinate // is (2,2). // // abs is a function in cmath header. // It basically change negative number to positive, getting its absolute value. int diffx = abs(posx-2); int diffy = abs(posy-2); // Then we output the sum of the different in x and y cout << diffx + diffy << endl; }

In Java

import java.io.PrintStream; import java.util.*; public class Main{ static Scanner in; static PrintStream out; public static void main(String[] args){ in = new Scanner(System.in); out = System.out; // We just need to x and y position for the 1 // So we define the variable first, that we will use. int posx = 0; int posy = 0; // Then we loop through the y for(int y=0;y<5;y++){ // And the x for(int x=0;x<5;x++){ // And at the same time, we input the number int num = in.nextInt(); // If the number is 1, then update posx and posy if(num == 1){ posx = x; posy = y; } } } // Then, we need to get the absolute different between // the position of the '1' and the center. Which is coordinate // is (2,2). // // abs is a function in Math class // It basically change negative number to positive, getting its absolute value. int diffx = Math.abs(posx-2); int diffy = Math.abs(posy-2); // Then we output the sum of the different in x and y out.println(diffx+diffy); } }

**Problem D**

Problem D is a bit tricky. It involve counting how many ‘1’ is there in a number’s binary representation. So do we need to convert it to binary first? Actually, computer already store it in binary. What we need to do is to read the digit one by one. There are various approach. My approach involve reading the least significant bit by AND-ing it with 1, and then shifting the number to the right, so that the second least significant bit is now the most significant bit. We repeat this 32 time, meaning, we shift this 32 time, effectively iterating on all 32 bit.

#include<iostream> using namespace std; void solve(){ // num is the number int num; cin >> num; // This number is used to store the number of '1' digit in binary int one_count = 0; // Basically, what we are going to do is, we will check // each binary digit representation using a bitwise AND operator // An integer in a normal computer would have 32 bit, so we loop 32 time for(int i=0;i<32;i++){ // Each time, we check the least significant digit, by AND-ing the number // with one. Which in binary only have one digit, the least significant one. // The result would be 0 if the least significant digit is not turned on, and 1 // if the least significant digit is turned on. if(num & 1){ // If the least significant digit is turned on, we increment the one_count one_count++; } // Then we shift the number to the right, // effectively, checking the next digit on the next iteration. num = num >> 1; } // If the one_count is even, print "even" if not, print "odd" if(one_count%2 == 0){ cout << "even"; }else{ cout << "odd"; } } int main(){ // t is the number of test case int t; cin >> t; // Loop t times for(int i=0;i<t;i++){ // Output the Case number first cout << "Case " << i+1 << ": "; // Run the algorithm // This function will run the algorithm and output the result too. solve(); // Output a newly cout << endl; } }

In Java

import java.io.PrintStream; import java.util.*; public class Main{ static Scanner in; static PrintStream out; static void solve(){ // num is the number int num = in.nextInt(); // This number is used to store the number of '1' digit in binary int one_count = 0; // Basically, what we are going to do is, we will check // each binary digit representation using a bitwise AND operator // An integer in a normal computer would have 32 bit, so we loop 32 time for(int i=0;i<32;i++){ // Each time, we check the least significant digit, by AND-ing the number // with one. Which in binary only have one digit, the least significant one. // The result would be 0 if the least significant digit is not turned on, and 1 // if the least significant digit is turned on. if((num & 1) == 1){ // If the least significant digit is turned on, we increment the one_count one_count++; } // Then we shift the number to the right, // effectively, checking the next digit on the next iteration. num = num >> 1; } // If the one_count is even, print "even" if not, print "odd" if(one_count%2 == 0){ out.print("even"); }else{ out.print("odd"); } } public static void main(String[] args){ in = new Scanner(System.in); out = System.out; // t is the number of test case int t = in.nextInt(); // Loop t times for(int i=0;i<t;i++){ // Output the Case number first out.print("Case "+(i+1)+": "); // Run the algorithm // This function will run the algorithm and output the result too. solve(); // Output a newly out.println(); } } }

## Problem E

In problem E, some magnet are added in specific order and orientation and you need to determine how many partition is created when some of the magnet combine with each other. Some of you may think something like “Oh no! Now for each partition, I need to keep track of the first and the last pole! It will be hard, but I can do it! Think positive!”. Well, that will work, eventually. But an easier approach is to see that when two consecutive magnet has the same orientation it will combine so no new partition is created. In another word, a new partition is created when two consecutive magnet have different orientation. So, my solution is that, I input the magnet one at a time, if the current magnet is different to the last magnet, then I increment a counter.

#include<iostream> using namespace std; int main(){ // Input n which is how many magnet int n; cin >> n; // howmany is how many partition. Initialized to 0 int howmany = 0; // Last will store the last magnet we checked. // For now, nowthing. string last; for(int i=0;i<n;i++){ // Input the magnet string current; cin >> current; // If this is the first time we input it if(i == 0){ // Just increment the partition counter howmany++; }else{ // If the last magent is not in the same partition of the // current magnet, it must make a new partition if(last != current){ howmany++; } } // Update the last magnet last = current; } // Output how many partition we found. cout << howmany; }

In Java

import java.io.PrintStream; import java.util.*; public class Main{ static Scanner in; static PrintStream out; public static void main(String[] args){ in = new Scanner(System.in); out = System.out; // Input n which is how many magnet int n = in.nextInt(); // howmany is how many partition. Initialized to 0 int howmany = 0; // Last will store the last magnet we checked. // For now, nothing. String last = ""; for(int i=0;i<n;i++){ // Input the magnet String current = in.next(); // If this is the first time we input it if(i == 0){ // Just increment the partition counter howmany++; }else{ // If the last magent is not in the same partition of the // current magnet, it must make a new partition if(!last.equals(current)){ howmany++; } } // Update the last magnet last = current; } // Output how many partition we found. out.println(howmany); } }

## That’s all folks!

We hope this practice serve you well for IIUM Code Jam. We target there would be 3 question like this in the upcoming IIUM Code Jam. Anyway, Come! Experience an ACM ICPC competition at IIUM Code Jam. Next week we will open some registration booth in both KICT and KOE. For any question, contact us, or meet us there. Thank you for reading and Assalamualaikum.