Pages

Tuesday, June 14, 2011

An Introduction to Programming Contest (Various Input\Output Systems)

If you are a beginner, you will find it very unusual sometime regarding taking inputs. 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 said to take an integer number (n) as input till the End Of File/ (EOF):
 
 
int main(){
    int n;
    while(scanf("%d", &n)!=EOF){
         ...........
         ...........
    }
    return 0;
}

Sunday, June 5, 2011

File Input/Output

There are various ways to take input from file and show the result in another file. 

We can use fopen() for taking input from a file or to show output in a file. It is the easiest way to do so. First, we have to open the files and at the end we just need to close the file. An example is given below:

Suppose we want to take input from the "input.txt" file and want to store the results in the "output.txt" file. The code for this is given below:

#include<stdio.h>
int main(){
    int T, ans;
    int cm, y, ssu, ssa, f;
    int b, gs, gc, w;
    freopen("input.txt","r", stdin);
    freopen ("output.txt","w",stdout);
    scanf("%d", &T);
    while(T--){
               scanf("%d%d%d%d%d", &cm, &y, &ssu, &ssa, &f);
               scanf("%d%d%d%d", &b, &gs, &gc, &w);
               ans = b + (gs/30) + (gc/25) + (w/10);
               printf("%d\n", ans);
    }
    fclose(stdin);
    fclose(stdout);
    return 0;
}

Wednesday, June 1, 2011

memset (cstring (string.h))

memset:
memset is such a library function which is rarely used in practical. Suppose, in an array, for some certain values you have to make that true. And for any other values that will be false. In this case you have to change all the elements of the array to false first, and then you can make that true for those specific values. memset is the easiest way to do this. An example of it is given below:

bool row [10000 + 5];
bool col [10000 + 5];
memset (row, false, 10005);
memset (col, false, 10005);


memset:
void * memset ( void * ptr, int value, size_t num );


Fill block of memory Sets the first num bytes of the block of memory pointed by ptr to the specified value (interpreted as an unsigned char).

Parametersptr
Pointer to the block of memory to fill.
value
Value to be set. The value is passed as an int, but the function fills the block of memory using the unsigned char conversion of this value.
num
Number of bytes to be set to the value.

Return Valueptr is returned.

Example

/* memset example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str[] = "almost every programmer should know memset!";
  memset (str,'-',6);
  puts (str);
  return 0;
}

Output:
------ every programmer should know memset!

UVA Problem#11760

Question:

Solution:

1. Take 2 Boolean array (e.g bool row[10000 + 5]; bool col[10000 + 5]; )
2. Let all the elements of the above two array be false. The easiest way to do this is by using memset(...,...,)
3. Then start taking the inputs as "r" and "c"and turn the row[r] and col[c] into true from false.
4. Then check the 4 places of Arif where he can move. 
5. Print your result.
Pseudo code is given below: