zoukankan      html  css  js  c++  java
  • A. Special Permutation(水题)

    题意:有t个样例,输入一个数n,求输出一个n个数的数组p,并满足每个数的数值和对应的角标不相同。(p1!=1)

    题解:只需要想出最简单的满足条件的表示方法即可。如果是偶数,直接倒着输出;如果是奇数,还是倒着输出,再用一个数把中间的数换掉就行了。(当然也可以统一处理,倒着输出,把中间数换成固定的一个数,比如1,能省代码)

    ACcode:

    int main()

    {

    int t;

    cin >> t;

    while (t--)

    {

    int n;

    cin >> n;

    if (n % 2 == 0)

    {

    for (int i = n; i > 1; i--)

    cout << i << " ";

    cout << "1" << endl;

    }

    if (n % 2 == 1)

    {

    for (int i = n; i >= n / 2+2; i--)

    cout << i << " ";

    cout << "1" << " ";

    for (int i = n / 2 + 1; i > 2; i--)

    cout << i << " ";

    cout << "2" << endl;

    }

    }

    return 0;

    }

  • 相关阅读:
    如何写Makefile?
    C语言变量的存储类别详解
    Longest Palindrome Substring
    Count Primes
    Closest Binary Search
    Search Insert Position
    Set Matrix Zeros ****
    Search for a Range
    Two Sum II
    Jump Game
  • 原文地址:https://www.cnblogs.com/Uiney117/p/14346430.html
Copyright © 2011-2022 走看看