Daffodil International University

IT Help Desk => Programming Language => Programming Competition => Topic started by: Nazmul on March 03, 2012, 12:52:31 PM

Title: Introduction to Programming Contest (Input Format)
Post by: Nazmul on March 03, 2012, 12:52:31 PM
For the beginner,
They will find very unusual sometime regarding taking inputs. Taking input is very strict in programming contest. No extra message is allowed to instruct the user. For example:
Code: [Select]
printf("Please Input the value of n:");
Some problems will say, "The Program will continue till the END OF FILE"
some will say "Stop taking input if N=0" etc etc.
Some important ways to take input will be described here:

1. If you are asked to take an integer number (n) as input till the End Of File/ (EOF):
 
 
Code: [Select]
int main(){
    int n;
    while(scanf("%d", &n)==1){
         ...........
         ...........
    }
    return 0;
}

scanf() returns number of data read. If no data is given( EOF is reached) it will get 0 and terminates the loop. For giving EOF signal to program you have to press ctrl+z

or, If there is nothing said in the problem description about how long you will take the inputs, in that case you also have to follow this way.

2. If you are said to take an integer number n and it is said that the end of input will be indicated by a case with n=0:

Code: [Select]
int n;
while(1){
    scanf("%d", &n);
    if(n==0)
       break;
    ........
    ........
}

3. If the problem statement says something like "The input can contain multiple test cases. The first line of the input indicates the number of test cases. For each test case take an integer number input" :

Code: [Select]
int n, test;

scanf("%d", &test);
while(test--){
     scanf("%d", &n);
     ................
     ................
}

4. In the previous input technique, after taking the input of test, if it was said to take a string (char str[100]) as input then we would had done that as below:

Code: [Select]
int test;
char str[100], dummy;

scanf("%d", &test);
dummy= getchar();
while(test--){
     gets(str);
     ................
     ................
}
As "str" is a string, after giving the input of test when we press the enter button, str takes that newline character as the input, which creates big problem while processing. That is why another character variable dummy is used right after taking the input to the test variable.

5.If the problem statement says to take a set of characters as input till the end of file:

Code: [Select]
char str[100];

int main(){
    while(gets(str)!=NULL){
          .......
          .......
    }
    return 0;
}

I hope that this will help beginners to start solving problems in Programming Contest.
Title: Re: Introduction to Programming Contest (Input Format)
Post by: jas_fluidm on March 08, 2012, 11:33:12 AM
yes,  Nazmul sir it will help the beginners.