Answers to Ladder questions-1205, 1075, 1083, 1160

Source: Internet
Author: User

1205 word flip:

DescriptionDescription

Give an English sentence. I hope you can flip the word order in the sentence.

Input descriptionInput description

The input includes an English sentence.

Output descriptionOutput description

Output words in inverted order

Sample InputSample Input

I love you

Sample outputSample output

You love I

#include <iostream>#include <string>using namespace std;void resever_word(string str1){    int len = str1.size();    int i=0;    while(i<len/2){        str1[i] = str1[len-i-1]^str1[i];        str1[len-i-1] = str1[i]^str1[len-i-1];        str1[i] = str1[i]^str1[len-i-1];        i++;    }    int fr=0,er=0;    for(int j=0;j<len;j++)    {        er = j;        if(str1[er] == ‘ ‘ || er==len-1)        {            if(er==len-1)                er++;            int k=0;            while(k<(er-fr)/2){                str1[fr+k] = str1[er-k-1]^str1[fr+k];                str1[er-k-1] = str1[fr+k]^str1[er-k-1];                str1[fr+k] = str1[fr+k]^str1[er-k-1];                k++;            }            fr = er+1;        }    }    cout << str1 << endl;}int main(){    string str;    getline(cin,str);    resever_word(str);    return 0;}

Read the input statement, first flip the entire sentence, and then flip each word.

 

1075 clearly defined Random Number:

DescriptionDescription

I want to ask some students to do a questionnaire survey in school. In order to make the experiment objective, he first generates n random integers (n ≤ 1000) from 1 to 100 on the computer ), only one number is retained, and the remaining identical numbers are removed. Different numbers correspond to different student IDs. Then sort these numbers from small to large and sort them to the students for investigation. Please help clearly complete "de-duplication" and "sorting.

Input descriptionInput description

There are two rows, and 1st act as one positive integer, indicating the number of N of the generated Random Number:

The first row has n positive integers separated by spaces, which are the random numbers generated.

Output descriptionOutput description

1st act 1 positive integer m, indicating the number of different random numbers. 2nd act M positive integers separated by spaces.

Different random numbers in a large sorting order.

Sample InputSample Input

10

20 40 32 67 40 20 89 300 400 15

Sample outputSample output

8

15 20 32 40 67 89 300 400

#include <iostream>#include <algorithm>using namespace std;int main(){    int n;    cin >> n;    int num[100]={0},onum[100]={0};    for(int i=0;i<n;i++)    {        cin >> num[i];    }    cout << endl;    sort(num,num+n);    int j=1,m=1;    onum[0] = num[0];    while(j<n)    {        while(num[j]==num[j-1]){            j++;        }        onum[m++] = num[j];        j++;    }    cout << m << endl;;    for(int i=0;i<m;i++)    {        cout << onum[i] << " ";;    }    return 0;}

Removing duplicates is to put non-duplicated data into the new arrary.

 

1083 Cantor table

DescriptionDescription

One of the famous proofs of modern mathematics is that Georg Cantor proves that rational numbers can be enumerated. He used the following table to prove this proposition: 1/1 1/2 1/3 1/4... 2/1 2/2 2/3 2/4... 3/1 3/2 3/3... 4/1 4/2... 5/1... ... Each item in the preceding table is numbered in Z. The first item is 1/1, then 1/2, 2/1, 3/1, 2/2 ,...

Input descriptionInput description

Integer N (1≤n ≤10000000)

Output descriptionOutput description

Nth entry in the table

Sample InputSample Input

7

Sample outputSample output

1/4

#include <iostream>using namespace std;int main(){    int n,i=1;    cin >> n;    int sum = 0;    for(;sum<n;i++)    {        sum = sum+i;    }    i = i-1;    //i斜行    int a=1,b=1,num;    if(i%2 == 0){        num = sum-n+1;        b = num;        a = i+1-b;        cout << a << "/" << b;    }    else{        num = sum-n+1;        a = num;        b = i+1-a;        cout << a << "/" << b;    }    return 0;}

First, judge the number of the th oblique line, and then determine the number of the th oblique line. The I is the parity to determine whether the number is going from top to bottom, that is, the calculation method of A and B.

 

1160 snake Matrix

DescriptionDescription

James plays a digital game and obtains an N-row and N-column Number Matrix (where N is an odd number not greater than 100). The number filling method is as follows: in the center of the matrix, start from 1 and circle it in a counter-clockwise direction, expanding circle by circle until n rows and n columns are filled with numbers. Please output the square matrix of N rows and the sum of its diagonal numbers.

Input descriptionInput description

N (n rows and n columns)

Output descriptionOutput description

A matrix composed of N + 1 rows and N actions. The last row is the sum of diagonal numbers.

Sample InputSample Input

3

Sample outputSample output

5 4 3
6 1 2
7 8 9
25

 

#include <iostream>using namespace std;#define MIN 100#define MAX 10000int main(){    int n, m, num[MAX]={0},snake[MIN][MIN]={0};    cin >> n;    if(n%2==0)        return 0;    m = n*n;    int i=0,j=0;    //存下n的矩阵的所有数,顺序排放在num中    while(i<(m))    {        num[i] = i+1;        i++;    }    //分配对角线的值    int core,up,down;    core = n/2;    up = core-1;    down = core+1;    snake[core][core] = 1;    for(i=2;i<=n;i=i+2)    {        snake[up][up] = i*i+1;        snake[down][down] = (i+1)*(i+1);        up--;down++;    }    //分配上半矩阵    up = core-1;    for(i=2;i<n;i=i+2)    {        j=1;        while(j<=i)        {            snake[up][up+j] = snake[up][up]-j;            snake[up+j][up] = snake[up][up]+j;            j++;        }        up--;    }    //分配下半矩阵    down = core+1;    for(i=1;i<n;i+=2)    {        j=1;        while(j<=i)        {            snake[down][down-j] = snake[down][down]-j;            snake[down-j][down] = snake[down-i-1][down]-(i-j+1);            j++;        }        down++;    }    int sum=0;    for(i=0;i<n;i++)    {        for(j=0;j<n;j++)        {            cout << snake[i][j] << " ";            if(j == (n-i-1)||j==i){                sum = sum+snake[i][j];            }        }        cout << endl;    }    cout << sum;    return 0;}

 

It may be difficult to assign values by yourself, but it is quite understandable.

First, assign a diagonal value (core, core), and forward and down along the diagonal lines to the upper and lower directions. You can write several matrix examples. n is the upper right corner of the maximum even number, and N is the lower left corner of the maximum odd number. N is required to be an odd number.

The upper triangle matrix is based on the upper right corner (Up, Up) value. It is easy to write each column-1 to the right of each row + 1. The lower Triangle Matrix, in the lower left corner of the diagonal line (Down, Down) as the standard, to the left of each column-1, but up to the (down-i-1, down) value as the standard, in turn online-(i-j-1 ), this may be troublesome. The standard points can be changed, and the natural computing methods can also be changed.

At last, the value on the diagonal line should include two diagonal lines X.

 

 

Answers to Ladder questions-1205, 1075, 1083, 1160

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.