Simulation [noip2016&2015] toy puzzle & Magical Magic Square

Source: Internet
Author: User
Tags first row

simulation has always been the NOIP must test a type of question is also the most simple type of questions generally speaking, this problem takes the least time and should try to take the most points, so as my "Noip real Problem" series of the first article on first say a simulation problem

Topics
① Toy Puzzle "Noip Improvement Group" 2016d1t1 [difficulty]0.8 [tag]< simulation > (Rokua search P1563 1S128MB)
② Magical Magic Square "Noip raise Group" 2015d1t1 [difficulty]0.9 [tag]< simulation >< Enumeration >< violence > (Rokua search P2615 1S128MB)

Toy Puzzles
Key points: judging
Title Description
Small South has a cute toy villain, and they have different occupations. One day, the dolls hid the little South's glasses. Small South found that the toys were surrounded by small people, some facing the inner circle, and some facing outside. The following figure:

Then singer told Little South a puzzle: "The glasses hidden in my left number 3rd toy villain right number 1th toy villain's left number 2nd toy man there." ”
Small South found that the direction of the toy villain in this puzzle is very critical, because the inside and outside of the toy villain is opposite: the side of the toy villain, its left side is clockwise direction, the right is counterclockwise direction; While facing the outside of the toy villain, its left is counterclockwise direction, the right is clockwise direction.
Small south, while struggling to identify the toy villain, counted:
Singer Inward, the 3rd of the left is archer.
Archer outward, the 1th of the right is thinker.
Thinker outward, the 2nd of the left is writer.
So the glasses are hidden in writer's place!
Although the successful return of glasses, but small south and not relieved. If the next time more toys hide his glasses, or the length of the puzzle is longer, he may not be able to find his glasses. So small South wants you to write a program to help him solve a similar puzzle. Such puzzles can be described specifically as:
There are N toy men in a circle, known for their occupations and orientations. Now the 1th toy villain tells the small south a puzzle containing the m-directive, where the z-directive is shaped like "left/right number S, a toy villain". You need to output a number of these instructions in order to reach the toy villain's occupation.
input/Output sample
Input Sample:
7 3
0 singer
0 Reader
0 Mengbier
1 thinker
1 Archer
0 writer
1 Mogician
0 3
1 1
0 2
Sample output:
Writer

First of all, we need to know that there are four things to consider, the key is to judge the direction of the villain and rotation
The code is as follows

 #include <iostream> #include <cstring> #include <string> using namespace
Std
    int main () {int n,m;cin>>n>>m;
    int a[100001];
    String b[100001];
    for (int i=1;i<=n;i++) {cin>>a[i]>>b[i];
    } int x[100001],y[100001];
    for (int i=1;i<=m;i++) {cin>>x[i]>>y[i];
    } int t=1;
    for (int l=1;l<=m;l++) {if (a[t]==0&&x[l]==0) {t-=y[l];if (t>n) t-=n;if (t<=0) t+=n;
    } else if (a[t]==0&&x[l]==1) {t+=y[l];if (t>n) t-=n;if (t<=0) t+=n;
    } else if (a[t]==1&&x[l]==0) {t+=y[l];if (t>n) t-=n;if (t<=0) t+=n;
    } else if (a[t]==1&&x[l]==1) {t-=y[l];if (t>n) t-=n;if (t<=0) t+=n;
}} cout<<b[t]<<endl; }//coyg 

Magical Magic Square
Important: Use of conditional statements
Title Description
Magic Square is a very magical n*n matrix: It is composed of digital three-way,......, n*n, and the sum of the numbers on each row, column, and two diagonal lines are the same.
When n is an odd number, we can construct a magic square by the following methods:
First write 1 in the middle of the first line.
Then, fill in each number of K (k=2,3,..., n*n) in the following way from small to large:
1. if (k−1) in the first row but not in the last column, the K is filled in the last row, (k−1) in the right column of the column;
2. if (k−1) in the last column but not in the first row, the K is filled in the first column, (k−1) on the row of the previous line;
3. if (k−1) in the last column of the first row, the K is filled directly below (k−1);
4. if (k−1) is neither in the first line nor in the last column, if the upper right of (k−1) is not filled, the k is filled in the upper right of (k−1), otherwise k is filled directly below (k−1).
Now given n please construct the magic square of N*n by the above method.
input/Output sample
Input Sample:
3
Sample output:
8 1 6
3 5 7
4 9 2

We need to understand this question first. Test instructions writes 1 in the middle of the first line and then writes out 4 cases according to the 1 2 3 four of the problem.
The code is as follows

#include <iostream>
using namespace std;
int main ()
{
    int n;cin>>n;
    int a[101][101];
    A[1][n/2+1]=1;
    int x=1,y=n/2+1;
    int m=n*n;
    for (int i=2;i<=m;i++) {
        int xx,yy;
        if (x==1&&y!=n) {
            xx=n;yy=y+1;
        }
        else if (y==n&&x!=1) {
            yy=1;xx=x-1;
        }
        else if (x==1&&y==n) {
            xx=x+1;yy=y;
        }
        else if (x!=1&&y!=n) {
            if (!a[x-1][y+1]) xx=x-1,yy=y+1;
            else xx=x+1,yy=y;
        }
        a[xx][yy]=i;
        x=xx,y=yy;
    }
    for (int i=1;i<=n;i++) {for
        (int j=1;j<n;j++) {
            cout<<a[i][j]<< "";
        } cout<<a[i][n]<<endl;
    }
}
Coyg

Because these two questions are relatively simple so do not consider too complex the first code (20 pilot) In addition to the last four 200+ms the rest are 0ms after all, this is the most water in the Noip.
Link id:ghao5311

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.