Getting algorithm contest (a) language papers arrays and strings

learning target

  1. Declare and use one-dimensional array
  2. Declaring and using two-dimensional array
  3. Statement string assignment, compare and connect
  4. ASCII character codes and function in ctype.h
  5. Acknowledge "++", "+ =" other variables can be modified operator
  6. and to use fgetc getchar
  7. Learn about the different operating systems represent line breaks
  8. Master fgets to use and understand gets a "buffer overflow" vulnerability
  9. Often learn to scale to simplify the code

 

First, the array

Procedure 1 : 5 the number of inputs, outputs reverse

#include<stdio.h>
#define maxn 8
int a[maxn];
int main()
{
    int x, n = 0;
    while(scanf("%d", &x) == 1){
        a[n++] = x;
    }
    for(int j = n-1 ; j >= 0; j--)
        printf("%d ",a[j]);
    return 0;
}

 

 

 The final ^ D input to the end [ie Ctrl + D]

# In the algorithm contest, it is often difficult to accurately calculate the required size of the array, the array will generally have slightly larger statement. Under the premise of enough space, wasted little will not have much impact, so even enter the number 5, the safe side can define an array of size 8

Statement: a [n ++] = x - does two things: first assignment a [n] = x, and then execute n = n + 1

# For the time variable n, n ++ and ++ n will add 1 to n, but when they are used in an expression, behavior be different: n ++ will be used to calculate the value before plus 1 expression [namely] the original value of n, and ++ n [i.e. calculates a value n + 1] expression using a value obtained by adding 1 ---- who is at the front, on the first who use [n ++, n mian front of it with the original value of n, n ++, ++ in front, will be the first n + 1]

 

Why should a definition on the outside of the main function?

Only when on the outside, the size of the array before it can open a very big; when placed in the main function, the array will be slightly larger than the abnormal exit

# Therefore, relatively large arrays should be declared outside the main function, or the program may not run

 

 

However, the array can not do an assignment:

3-1 in the program, if the declaration "int a [maxn], b [maxn]", can not be assigned to b = a

If the array from a copy k elements to the array b, can do so - memcpy (b, a, sizeof (int) * k)

Of course, if a and b are an array of floating-point , the written copy to - memcpy (b, a, sizeof (double) * k). Also note that the use of memcpy function to include the header file string.h

If desired the array a whole b copied to the array can be written in simpler: memcpy (b, a, sizeof (a))

 

 

 

Program 2 : turn on the lights problem

N-lamp, numbered 1 ~ n. Person 1 all the lights on, the second number is a multiple of all the individual pressing switches 2 (the lamps are turned off), the third press all individuals numbered multiples of three switches (which will turn off the lights is opened, open lamp will be turned off), and so on. A total of k individual, and finally asked what the lights are on? N inputs and k, the number of output driving a lamp. k≤n≤1000

Sample input:
73
sample output:
1567

 

My thoughts: n k individual lamps, if the number of status lights% human number == 0, the lamp will be negated ----> + arrays may be utilized for the double loop state value achieved +

#include<stdio.h>
#include<string.h>
#define maxn 1010
int a[maxn];
int main()
{
    int n,k,first =1;
    memset(a,0,sizeof(a));
    scanf("%d %d",&n,&k);
    for(int i = 1;i <= k;i++){
        for(int j = 1;j <= n;j++){
            if(j % i == 0)
                a[j] = !A [J]; 
        }
    }
    for(inti =. 1; i <= n-; i ++) {
        IF(A [i]) {// If the i-th light is on, i.e., A [i] =. 1
            IF( First)
                First=0;
            the else
                the printf (" ");
            the printf ("% D", I);
        }
    }
    the printf ("\ n-");
    return 0;
}

skill:

  1. memset (a, 0, sizeof (a)) - effect of the array is a clear, defined in string.h
  2. In order to avoid the output at the beginning of the final output and extra spaces, it sets a flag variable first, can indicate whether a variable current to be output as the first one. There should be no space before the first argument, but there are other variables

 

When using sample data that is the first test [1567] The lamps are lit, the program can be added last printf ( "% d", a [1]); the output is 1, i.e., 

a[j] = !a[j];

After the inversion, from 0 to 1, 1 to 0, or a

 

 

Program 3 : Snake number fill

Fill in the matrix in the n × n 1,2, ..., n × n, serpentine fill requirements. For example, n = 4 is square when:

Square in the above, an extra space only to facilitate observation of laws, not strictly output. n≤8.

 

My thoughts: . . I will not = _ = ...... = __ = ___ = = ..... ....

Solving a two-dimensional array - [n] [n] int a -> and from a [0] [n] -> a [n] [n] to initialize ---> a [n] [n] - -> a [n] [0] ----> a [0] [0] ---> a [0] [n-1]. . . . The for loop [but contain multiple variable length steering, a short time can not do it]

 

Book ideas:

Is a two-dimensional array, fill in the order starting at 1. Set "pen" of coordinates (x, y), the start x = 0, y = n- 1, i.e., row 0, range (the ranks of the n-1 column is 0 ~ n-1, without the first n column). "Pen" trajectories is: down, down, down, left, left, left, up, up, up, right, right, down, down, left on. In short, under the first, not to fill up, then left, then on, and finally the right. "Not filled" refers to walk out of bounds (e.g. 4 → 5), or walk coming into the lattice (e.g., 12 → 13) had previously filled. If all the grid is initialized to 0, it can be judged easily .

#include<stdio.h>
#include<string.h>
#define maxn 20
int a[maxn][maxn];
int main()
{
    int n, x, y, tot = 0;
    scanf("%d", &n);
    memset(a, 0, sizeof(a));
    tot = a[x=0][y=n-1] = 1;//从1开始写
    while(tot < n*n)
    {
            while(x+1<n && !a[x+1][y])
                a[++x][y] = ++tot;    //【1】
            while(y-1>=0 && !a[x][y-1])
                a[x][--y] = ++tot;    //【2】
            while(x-1>=0 && !a[x-1][y])
                a[--x][y] = ++tot;    //【3】
            while(y+1<n && !a[x][y+1])
                a[x][++y] = ++tot;    //【4】
    }
    for(x = 0; x < n; x++)
        {
            for(y = 0; y < n; y++)
                printf("%3d", a[x][y]);
            printf("\n");
        }
    return 0;
}

 

[1]: this line, remain unchanged y, x plus one each move, down to "fill in" number from the same time in the pen when moving down to meet no more than x n, n * n because of digital array, and in order to fill out the circulation loop filled outer layers of the stratum gradually, to ensure that the next position will be a "fill in the number" is 0, i.e., not yet enter the number [the number of fill! a [x] [ y] = 0}

 

 

 At this time, x = 0, y = 2 to print a [x] [++ y] 16, i.e., y = 3, the determination at this time to start the cycle, y + 1 <n true, but a [x] [y + 1] has been filled with "1", we can not continue to fill the right and began to write down (next cycle)

If you remove all the anticipation over whether the industry: [remove && a [x] [y]!]

 

 Similarly rest of the process

 

# In many cases, it is best not to do is to check before you can do one thing, and do not regret it done again. Because the "undo" is often more trouble

If # x + 1 <n is false, will not count "! A [x + 1] [y]", it is short-circuited without crossing operator && ---- [here] mentioned

 

Why is not the ++ tot tot ++?

#include<stdio.h>

int main(){
    int i = 1;
    printf("%d\n",i);
    i++;
    printf("%d\n",i);
    ++i;
    printf("%d\n",i);
    return 0;
}

 

 Recalling the just mentioned:

# For the time variable n, n ++ and ++ n will add 1 to n, but when they are used in an expression, behavior be different: n ++ will be used to calculate the value before plus 1 expression [namely] the original value of n, and ++ n [i.e. calculates a value n + 1] expression using a value obtained by adding 1 ---- who is at the front, on the first who use [n ++, n mian front of it with the original value of n, n ++, ++ in front, will be the first n + 1]

 

 

 

Second, the array of characters

 

 

 

 

 

 

a

Guess you like

Origin www.cnblogs.com/expedition/p/11482813.html