Zoj 1201 Reverse Order and its original order

Source: Internet
Author: User

//////////////////////////////////////// ///////////////////////////////
// Inversion
// Reverse Order and its original order
# Include <iostream>
# Include <string>
Using namespace STD;
Char nor [100];
Long shuru [10000];
Long chu [10000];
Long I, J, K, jishu;

 

 

Void perm (int n)
{
Jishu = 0;
For (I = 0; I <n; I ++)
Cin> shuru [I];
For (I = 0; I <n; I ++) // output of each number
{
For (j = 0; j <n; j ++) // there are several large numbers before a number.
{
If (shuru [J]> I + 1)
Jishu ++;
Else if (shuru [J] = I + 1)
Break;
}
Cout <jishu;
If (I! = N-1)
Cout <"";
Jishu = 0;
}
}

 

Void inv (int n)
{
Int temp;
For (I = 0; I <n; I ++)
Cin> shuru [I];
For (j = 0; j <n; j ++)
Chu [J] = N-J;
For (I = 0; I <n; I ++) // This loop allows n numbers to be exchanged once.
{
Jishu = 0;
For (k = n-1; k> = 0; k --)
{
If (chu [k] = I + 1)
Break;
}
J = 0;
Do {// This round robin swaps the I-th number and its proper position
If (jishu = shuru [I] & chu [J]> chu [k] & J <K)
{
Temp = chu [J];
Chu [J] = chu [k];
Chu [k] = temp;
Break;
}
If (chu [J]> chu [k] & jishu <= shuru [I])
{
Jishu ++;
}
J ++;
If (chu [J]> chu [k] & jishu = shuru [I]-1 & K <j)
{
Temp = chu [J];
Chu [J] = chu [k];
Chu [k] = temp;
Break;
}
} While (j <n );
}
For (I = 0; I <n; I ++)
{
Cout <chu [I];
If (I! = N-1)
Cout <"";
}
}

 


Int main ()
{
Int N;
While (CIN> N)
{
If (n = 0)
Return 0;
Cin> nor;
If (strcmp (nor, "P") = 0)
Perm (N );
Else
Inv (N );
Cout <Endl;
}
Return 0;
}

 

 

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.