Algorithmic race get started classic _3.1_ Array _ Reverse Output _ Turn on the light problem

Source: Internet
Author: User

It's a new day, keep updating.

Today into the new chapters, arrays and strings

1. Reverse Output problem:

Look at the code first.

#include <stdio.h>//Reverse Output 2017-8-16#defineMAXN 105intA[MAXN];intMainintargcChar*argv[]) {    //int A[MAXN];    intX, n =0;  while(SCANF ("%d", &x) = =1) A[n++] =x;  for(inti = n-1; I >=1; i--) printf ("%d", A[i]); printf ("%d\n", a[0]);//output beginning and end of line is generally no space, so with two statements to achieve outputreturn 0;}

Note: Arrays are generally declared slightly larger

This is a reverse output problem, but it should be noted that in the definition of the array, to be defined outside the main function, if you encounter a large maxn, such as 1000000, the program will not run, the following is the certificate

It's over, so we should just put it out there and look at it.

Put it on the outside and it's working, and then look at the effect.

2. Turn on the light problem:

Look at the code

//turn on the light problem 2017-8-16#include<stdio.h>#include<string.h>#defineMAXN 1010intA[MAXN];intMainintargcChar*argv[]) {    intN, k, first =1; Memset (A,0,sizeof(a)); scanf ("%d%d", &n, &k);  for(inti =1; I <= K; i++)         for(intj =1; J <= N; J + +)            if(j% i = =0) A[j] =!A[j];  for(inti =1; I <= N; i++)        if(A[i]) {if(first) First =0; Elseprintf (" "); printf ("%d", i); } printf ("\ n"); return 0;}

Here are a lot of new knowledge points, first of all, string.h in memcpy (b,a,sizeof (int) *k) is to copy a array to B, size K,

If you want to copy all, you can use memcpy (b,a,sizeof (a)), there is a function, that is, the program appears memset (A, 0, sizeof (a));

is to set the data in array a to 0.

It is important to note that we define the first variable, what is the variable? It is clear that it is a variable that is a flag,

Because in the output, the first number is not required before the space.

See Running results

3. Snake Filling Number:

The problem blogger has not solved, do not know why and the book code, running but there is a problem

Funny, bloggers just want to paste the code when checking the code, found that the 22while loop condition is wrong, should be moving up x-1 >= 0 (I wrote the < before, this tells you that if you have some very strange running results, try to check your code, Especially cycle conditions )

//The problem of serpentine filling 2017-8-16#include <stdio.h>#include<string.h>#defineMAXN 20intA[MAXN][MAXN];intMain () {intn, x, y, tot =0; scanf ("%d", &N); Memset (A,0,sizeof(a)); X=0; Y= N-1; A[x][y]=1; Tot=1; //tot = a[x = 0][y = n-1] = 1;//This statement is very concise, you can also use this sentence instead of the above four statements     while(Tot < nN) { while(x +1< n &&!a[x +1][y]) a[++x][y] = + +tot;  while(Y-1>=0&&!a[x][y-1]) a[x][--Y] = + +tot;  while(X-1>=0&&!a[x-1][y]) a[--x][y] = + +tot;  while(Y +1< n &&!a[x][y +1]) a[x][++y] = + +tot; }     for(x =0; x < n; X + +)    {         for(y =0; Y < n; y++) printf ("%3d", A[x][y]); printf ("\ n"); }    return 0;}

Analysis: Starting from 1 to fill in, set the starting coordinates of the pen (x, y), that is (0,n-1), X for the row, Y for the column, so the movement of the pen is down, down, down, left, left, left, Upper, upper, upper, right, right, bottom, bottom, left, on

Here we only need to judge two conditions

    • One is whether to go beyond the boundary, in the moving trajectory, i.e. x+1<n
    • The other is whether to fill in the previously filled lattice, so is a[x+1][y] = = 0 conditions, abbreviated to!A[X+1][Y]

With this idea, it's good to see how the results work.

In this exercise, we can learn:

1) can take advantage of the simple C language syntax, but only if you keep the code readable

2) In many cases, it is better to check before one thing is not to do, and not to regret after the finish, because "undo" is often more troublesome.

Found to do algorithms, which still can learn a lot, and very interesting

Life is also, when we do something, we should consider whether should do, because the regret medicine is very difficult to buy, like bloggers stay up late to write a blog,

Now is still acceptable, if every day so the body will be too much, good today to here, you colleagues also early rest, pay attention to the body.

Goodnight World!

Algorithmic race get started classic _3.1_ Array _ Reverse Output _ Turn on the light problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.