THE PREPARATION FOR QUALIFICATIONS FOR DISTRICT COMPETITIONS IN INFORMATICS
This page is a guide for preparing the solutions to the tasks that appear in the district competitions in informatics, as well as the qualifications for the same competitions, for elementary schools. Also shown are simple examples that are an integral part of larger tasks. This page is intended for primary and secondary school students who are preparing for exams in informatics. Since they are more difficult tasks, they can also be used by students for practice.
Complex input and formatted output
- The following example shows loading from multiple rows, where a number of rows are loaded first and each row contains one piece of data.
Task text (Part of the "Master" tasks from the first round of qualifications(see the 4th task on the qualification page for grade 6): In the first line of the standard input, enter the total number of boxes n ( 2 ≤ n ≤ 30). Then, in the next n lines, the numbers of the screws in the boxes k and (1≤k and ≤100) are entered, respectively.
#include < iostream >
using namespace std;
int main(){
using namespace std;
int main(){
int n;
cin >> n;/*Enter number of rowsn(number of boxes)*/
int srafoviUKutijama[n];/*an array representing the numbers of slots per box*/
for(int i = 0; i < n; i++)
{
}cin >> n;/*Enter number of rowsn(number of boxes)*/
int srafoviUKutijama[n];/*an array representing the numbers of slots per box*/
for(int i = 0; i < n; i++)
{
cin >> numOfSlotsPerBox[i];
}Do the Masters (6th example on Qualifications for district competitions)example completely, for practice. It is assumed that you have previous knowledge of loops (for) and strings.
Formatted output:
Example text: Two numbers are given
x=2.345677696787
a=23
a=23
Round the first number to 4 decimal places. Write the second number with a decimal point and show it with 3 decimal places. The output should be:
x=2.3457
a=23.000
a=23.000
Solution:
To determine the number of decimal places when rounding, use the built-in function: precision(), and to determine the number of decimal places to display the whole number as a real number and thus the number of trailing zeroes, use the showpoint function, which shows the following code:
To determine the number of decimal places when rounding, use the built-in function: precision(), and to determine the number of decimal places to display the whole number as a real number and thus the number of trailing zeroes, use the showpoint function, which shows the following code:
#include < iostream >
using namespace std;
int main()
{
using namespace std;
int main()
{
double x=2.345677696787;
double a=23;
cout.precision(5);
cout << "x=" << x << endl;
cout << "a=" << showpoint << a << endl;
return 0;
}double a=23;
cout.precision(5);
cout << "x=" << x << endl;
cout << "a=" << showpoint << a << endl;
return 0;
Task 3:
Enter the first and last name separated by commas and display the message "Hello first name last name!".
Enter the first and last name separated by commas and display the message "Hello first name last name!".
string name, surname;
getline(cin, name, ',');
getline(cin, surname); /* For example John, Malkovic*/
cout << "Hello " << name << " " << surname << "!" << endl;
getline(cin, name, ',');
getline(cin, surname); /* For example John, Malkovic*/
cout << "Hello " << name << " " << surname << "!" << endl;
Task 4:
Enter today's day, month and year in dd/mm/yyyy format and display them in yyyy-mm-dd format.
Solution:
First you need to include the <string> header
Enter today's day, month and year in dd/mm/yyyy format and display them in yyyy-mm-dd format.
Solution:
First you need to include the <string> header
#include< string >
and then inside the main method:
int day, month, year;
string dayStr,monthStr;
getline(cin,dayStr '/');
dan = stoi( dayStr);
getline(cin,monthStr, '/');
month = stoi( monthStr );
cin >> year;
cout << year << "-" << month << "-" << day;
string dayStr,monthStr;
getline(cin,dayStr '/');
dan = stoi( dayStr);
getline(cin,monthStr, '/');
month = stoi( monthStr );
cin >> year;
cout << year << "-" << month << "-" << day;
Task 5:
Enter the current time (hours and minutes) in hh:mm format and display them.
Enter the current time (hours and minutes) in hh:mm format and display them.
Extracting digits from a number. operator %
Task 1:
Determine the ones, tens and hundreds digits from a number.
Solution:
Determine the ones, tens and hundreds digits from a number.
Solution:
int number,units,tens,hundreds;
cin >> number;/*eg. 753*/
units = number % 10; /* 753 % 10=[3] */
tens = number / 10 % 10; /* 753 / 10=[75] -> 75 %10 -> 7[5]
hundreds = number / 100 % 10; /* 753 / 100 -> [7] */
cout << hundreds << ' ' << tens << ' ' << units;
cin >> number;/*eg. 753*/
units = number % 10; /* 753 % 10=[3] */
tens = number / 10 % 10; /* 753 / 10=[75] -> 75 %10 -> 7[5]
hundreds = number / 100 % 10; /* 753 / 100 -> [7] */
cout << hundreds << ' ' << tens << ' ' << units;
Task 2:
Write a program that outputs the hundreds digit from the decimal notation of a number. For example. If the number 9678 is entered, the output should be number 978.
Write a program that outputs the hundreds digit from the decimal notation of a number. For example. If the number 9678 is entered, the output should be number 978.
Task 3:
In a four-digit number (e.g. 9753), extract the first and last two digits (e.g. 97 and 53)
In a four-digit number (e.g. 9753), extract the first and last two digits (e.g. 97 and 53)
Extracting digits from a number with an unknown number of digits
Instructions: Use a while loop or a do-while loop and inside the loop use %10 to extract the last digit, then divide by 10 to reduce the number by one digit.
If necessary, do a few tasks with a while loop on the side: Branching in the program - examples
If necessary, do a few tasks with a while loop on the side: Branching in the program - examples
Task 4:
Load an integer with number of digits <15, then swap places with digits by swapping first and last, second and penultimate, etc.
For example. for loaded number 18734 should be converted to number 43781
Load an integer with number of digits <15, then swap places with digits by swapping first and last, second and penultimate, etc.
For example. for loaded number 18734 should be converted to number 43781
Qualification tasks
Task 1: Do the Padlock task from the first round of qualifications for the 7th grade (Task No. 14)
Task 2: Do the task Stubići od kockice, from the first round of qualifications for the 8th grade (Task no. 16)
The following tasks, from 3 - 6, are from the second round of qualifications for the 6th grade, given under the corresponding ordinal number of the task
Quest 3: Complete the Plum quest (Task no. 17)
Task 4: Complete the Bottle Quest (Task no. 18)
Task 5: Do the task Football group - table (Task no. 19)
Task 6: Complete the Most Excellent task (Task no. 20)
Infinite loops in programming
An infinite loop can be obtained when we write the for or while statement as follows:
for( ; ; ) {
//commands
} |
while(true) {
//commands
} |
Using break and continue
If the cycle needs to be interrupted before the end of the execution, the command break is used.
Example 1: Enter numbers until -1 is entered. Print the number of completed cycles. (See code below left)
Example 1: Enter numbers until -1 is entered. Print the number of completed cycles. (See code below left)
int broj=0,X; for( ; ; ) {
cin >> X; //Enter number X
}if(X == -1)break; broj++; |
int broj=0,X; for( ; ; ) {
cin >> X; //Enter number X
}if(X == -1)break; if(X < 0)continue; broj++; |
Example 2:
Extend example 1 so that cycles are not counted if a negative number other than -1 is entered;
The continue command within a cycle switches the execution of the next cycle. If there are commands in the body of the cycle after continue, they will not be executed for the current cycle. In the example on the right, if negative numbers are loaded the cycles are not counted because then number++ is not executed
Extend example 1 so that cycles are not counted if a negative number other than -1 is entered;
The continue command within a cycle switches the execution of the next cycle. If there are commands in the body of the cycle after continue, they will not be executed for the current cycle. In the example on the right, if negative numbers are loaded the cycles are not counted because then number++ is not executed
Task 3:
Enter an integer, then determine how many digits from left to right are sorted in ascending order:
Enter an integer, then determine how many digits from left to right are sorted in ascending order:
Enter a number, e.g. 13524. Determine how many digits there are. use a do-while loop, in which you should:
#include < iostream >
#include < cmath >
using namespace std;
int main()
{
#include < cmath >
using namespace std;
int main()
{
int br,d,x;
cin >> br;//13524
if(br==0) // If zero is loaded, the result is 1. Further execution is terminated
{
d=(int)log10(br)+1;//5, number of digits
if(d==1) //If it is a single digit, it returns 1 as the solution
{
int i=0;
int pret,c,j;
j=d; //the power of the divisor to remove the digit and extract the digit from the left
x=br;
do{
while(i < d);
cout << i << endl;
return 0;
}cin >> br;//13524
if(br==0) // If zero is loaded, the result is 1. Further execution is terminated
{
cout << "1" << endl;
return 0;
}return 0;
d=(int)log10(br)+1;//5, number of digits
if(d==1) //If it is a single digit, it returns 1 as the solution
{
cout << "1" << endl;
return 0;
}return 0;
int i=0;
int pret,c,j;
j=d; //the power of the divisor to remove the digit and extract the digit from the left
x=br;
do{
c=(int)(x/pow(10,j-1));//1, pull out the number on the left
x=x%(int)pow(10,j-1); //removes the digit from the right, 3524=13524 % 10000,3524=13524 % 10000
j--;
if(i==0){
if(c >pret){
else{
}x=x%(int)pow(10,j-1); //removes the digit from the right, 3524=13524 % 10000,3524=13524 % 10000
j--;
if(i==0){
pret=c;
i++;
continue;
}i++;
continue;
if(c >pret){
pret=c;
i++;
continue;
}i++;
continue;
else{
break;
}while(i < d);
cout << i << endl;
return 0;
Task 3: Do the task of the Valley from the second round of qualifications for the 6th grade (Task no. 21)
Strings in c++
string represents a string of characters (text data). To use string methods, you need to include the string header
#include < string >
Read more about strings on the page: Strings in the C/C++ language
Loading character from string
Example 1:
Enter a series of numbers in one line separated by ",", eg 2,5,7,4,3. Separate the digits from the text and convert them into numbers that are 10 larger than the entered digits. that would be the numbers 12 15 17 14 13 in the mentioned example.
Enter a series of numbers in one line separated by ",", eg 2,5,7,4,3. Separate the digits from the text and convert them into numbers that are 10 larger than the entered digits. that would be the numbers 12 15 17 14 13 in the mentioned example.
#include < iostream >
#include < cmath >
using namespace std;
int main()
{
#include < cmath >
using namespace std;
int main()
{
string numbers;
getline(cin,numbers);
cout << numbers << endl;
int ch;
int b = 0; /*length of entered numbers*/
for(char c: numbers)/* extract characters from a string */
{
cout << b << endl;
return 0;
}getline(cin,numbers);
cout << numbers << endl;
int ch;
int b = 0; /*length of entered numbers*/
for(char c: numbers)/* extract characters from a string */
{
if(c != ',')
{
}{
ch=c - '0';/*converts a digit from char to int*/
cout << (ch+10) << endl;
b++;/* increases the length of the sequence of numbers by 1 */
}cout << (ch+10) << endl;
b++;/* increases the length of the sequence of numbers by 1 */
cout << b << endl;
return 0;
Task 1:
Enter a series of numbers in one line separated by ",", eg 22,35,71,24,33. Separate the numbers from the text and convert them into numbers that are 10 larger than the entered digits. that would be the numbers 32 45 81 34 43 in the mentioned example.
Enter a series of numbers in one line separated by ",", eg 22,35,71,24,33. Separate the numbers from the text and convert them into numbers that are 10 larger than the entered digits. that would be the numbers 32 45 81 34 43 in the mentioned example.
The following tasks, from 2 - 5, are from the first round of qualifications for the n-th grade, given under the corresponding ordinal number of the task
Task 2: Do the task List of Products from the 1st round of qualifications for the 6th grade
Task 3: Do the Vocabulary task from the 1st round of qualifications for the 7th grade
Task 4: Do the task Notes from the 1st round of qualifications for the 8th grade
Task 5: Do the task "Date with the highest earnings" from the 1st round of the 8th grade qualification
Extracting a number and date from a string. Using the function substr
In some tasks, some information is loaded in the form of a string, so that more information can be found in one line, so after loading one line of text (string), data must be extracted from it. For example, if the line of text contains information about daily earnings and the date when that earnings were earned (eg 21_11_2021 32.5). This is demonstrated in the following example:
Example 1:
All fiscal invoices issued by one store are known. From each invoice, you can read the date when the invoice was issued and the amount charged. Extract the following data from the input data as separate strings of dates and earnings and print them on the screen. Store dates as string data, and earnings as double data. Dates are entered in the format dd-mm-yyyy, and on the output they should be displayed in the reverse order of the day of the month and the year, i.e. in the form: yyyy-mm-dd
Example:
Input
5
03-07-2021 340.00
05-04-2021 285.50
03-07-2021 100.50
04-07-2021 270.00
04-05-2021 155.00
Output
Date: Earnings:
2021-07-03 340.00
2021-04-05 285.00
2021-07-03 100.00
2021-07-04 270.00
2021-05-04 155.00
All fiscal invoices issued by one store are known. From each invoice, you can read the date when the invoice was issued and the amount charged. Extract the following data from the input data as separate strings of dates and earnings and print them on the screen. Store dates as string data, and earnings as double data. Dates are entered in the format dd-mm-yyyy, and on the output they should be displayed in the reverse order of the day of the month and the year, i.e. in the form: yyyy-mm-dd
Example:
Input
5
03-07-2021 340.00
05-04-2021 285.50
03-07-2021 100.50
04-07-2021 270.00
04-05-2021 155.00
Output
Date: Earnings:
2021-07-03 340.00
2021-04-05 285.00
2021-07-03 100.00
2021-07-04 270.00
2021-05-04 155.00
#include < iostream >
#include < string >
using namespace std;
int main()
{
#include < string >
using namespace std;
int main()
{
int n;
cin >> n;
string textRow;
cin.ignore();
string dates[n];
double wages[n];
int positions[2]= {2,5};/*positions where there are "-" separators to separate the year, month and day in the date*/
int ind=0;
while(getline(cin,textRow))
/*Entering rows*/ {
//Printing
cout<<"Date: Earnings:" << endl;
for(int i=0;i < n;i++){
return 0;
}cin >> n;
string textRow;
cin.ignore();
string dates[n];
double wages[n];
int positions[2]= {2,5};/*positions where there are "-" separators to separate the year, month and day in the date*/
int ind=0;
while(getline(cin,textRow))
/*Entering rows*/ {
int pos=line.find(" ");
/*search for the position " " that separates the date and earnings*/
string date=line.substr(0,pos);
/*extracts part of the text from position 0, while pos is actually the length of the substring*/
string dd=date.substr(0,positions[0]);
string mm=date.substr(positions[0]+1,2);
string yyyy=date.substr(6);
string dateReversed=yyyy+"-"+mm+"-"+dd;
double earning = stod(line.substr(pos+1));
dates[ind] = dateReversed;
wages[ind] = earning;
ind++;
if(ind == n)break;
}/*search for the position " " that separates the date and earnings*/
string date=line.substr(0,pos);
/*extracts part of the text from position 0, while pos is actually the length of the substring*/
string dd=date.substr(0,positions[0]);
string mm=date.substr(positions[0]+1,2);
string yyyy=date.substr(6);
string dateReversed=yyyy+"-"+mm+"-"+dd;
double earning = stod(line.substr(pos+1));
dates[ind] = dateReversed;
wages[ind] = earning;
ind++;
if(ind == n)break;
//Printing
cout<<"Date: Earnings:" << endl;
for(int i=0;i < n;i++){
cout << dates[i] << " " << wages[i] << endl;
}return 0;
Note: To extract the date from a line of text could be done using the getline() function with the 3rd parameter representing the separator, as shown in the previous examples on this page.
Eg: getline(cin,date,' ');
This is left to the reader to try out.
Also, you can find more about the substr() function and strings on the page: Strings in the C/C++ language
Eg: getline(cin,date,' ');
This is left to the reader to try out.
Also, you can find more about the substr() function and strings on the page: Strings in the C/C++ language
|
Next
Qualifications for district competitions >| |