YTU OJ C# 1637: List the Books
题目描述
Jim is fond of reading books, and he has so many books that sometimes it’s hard for him to manage them. So he is asking for your help to solve this problem. Only interest in the name, press year and price of the book, Jim wants to get a sorted list of his books, according to the sorting criteria.
输入
The problem consists of multiple test cases. In the first line of each test case, there’s an integer n that specifies the number of books Jim has. n will be a positive integer less than 100. The next n lines give the information of the books in the format Name Year Price. Name will be a string consisting of at most 80 characters from alphabet, Year and Price will be positive integers. Then comes the sorting criteria, which could be Name, Year or Price. Your task is to give out the book list in ascending order according to the sorting criteria in non-ascendent order. Note: That Name is the first criteria, Year is the second, and Price the third. It means that if the sorting criteria is Year and you got two books with the same Year, you’d sort them according to their Name. If they equals again, according to their Price. No two books will be same in all the three parameters. Input will be terminated by a case with n = 0.
输出
For each test case, output the book list, each book in a line. In each line you should output in the format Name Year Price, the three parameters should be seperated by just ONE space. You should output a blank line between two test cases.
样例输入
3
LearningGNUEmacs 2003 68
TheC++StandardLibrary 2002 108
ArtificialIntelligence 2005 75
Year
4
GhostStory 2001 1
WuXiaStory 2000 2
SFStory 1999 10
WeekEnd 1998 5
Price
0
样例输出
TheC++StandardLibrary 2002 108
LearningGNUEmacs 2003 68
ArtificialIntelligence 2005 75
GhostStory 2001 1
WuXiaStory 2000 2
WeekEnd 1998 5
SFStory 1999 10
using System;
class Program
{
static void Main(String[] agrs)
{
var num = 0;
method method = new method();
while ((num = Convert.ToInt32(Console.ReadLine())) != 0)
{
string[] str = new string[num+1];//要储存排序方法所以加一
for (var i = 0; i < num+1; i++)
{
str[i] = Console.ReadLine();
}
method.printf(str,num);
Console.WriteLine();
}
}
}
class method
{
public void printf(string[] str,int a)
{
string[,] sort = new string[a,3];//用二维数组吧每行的名称、年份和价格分开存放
string[] tomp;
string sp;
for (var i = 0; i < a ; i++)
{
tomp = str[i].Split(' ');
sort[i, 0] = tomp[0];
sort[i, 1] = tomp[1];
sort[i, 2] = tomp[2];
}
if (str[a] == "Year")
{
for (var i = 0; i < a ; i++)
{//冒泡
for (var j = 0; j< a-1-i; j++)
{
if (Convert.ToInt32(sort[j, 1]) > Convert.ToInt32(sort[j + 1, 1]))
{//按年份排序
sp = sort[j, 0];
sort[j, 0] = sort[j + 1, 0];
sort[j + 1, 0] = sp;
sp = sort[j, 1];
sort[j, 1] = sort[j + 1, 1];
sort[j + 1, 1] = sp;
sp = sort[j, 2];
sort[j, 2] = sort[j + 1, 2];
sort[j + 1, 2] = sp;
}
else if (Convert.ToInt32(sort[j, 1]) == Convert.ToInt32(sort[j + 1, 1]))
{//年份相同,按名称长度排序
if (sort[j, 0].Length > sort[j + 1, 2].Length)
{
sp = sort[j, 0];
sort[j, 0] = sort[j + 1, 0];
sort[j + 1, 0] = sp;
sp = sort[j, 1];
sort[j, 1] = sort[j + 1, 1];
sort[j + 1, 1] = sp;
sp = sort[j, 2];
sort[j, 2] = sort[j + 1, 2];
sort[j + 1, 2] = sp;
}
else if (sort[j, 0].Length == sort[j + 1, 2].Length)
{//名称相同长度按价格排序
if (Convert.ToInt32(sort[j, 2]) > Convert.ToInt32(sort[j + 1, 2]))
{
sp = sort[j, 0];
sort[j, 0] = sort[j + 1, 0];
sort[j + 1, 0] = sp;
sp = sort[j, 1];
sort[j, 1] = sort[j + 1, 1];
sort[j + 1, 1] = sp;
sp = sort[j, 2];
sort[j, 2] = sort[j + 1, 2];
sort[j + 1, 2] = sp;
}
}
}
}
}
for (var i = 0; i < a; i++)
{
Console.WriteLine(sort[i,0]+" "+sort[i,1]+" "+sort[i,2]);
}
}
else if (str[a] == "Price")
{
for (var i = 0; i < a ; i++)
{
for (var j = 0; j < a - 1-i; j++)
{
if (Convert.ToInt32(sort[j, 2]) > Convert.ToInt32(sort[j + 1, 2]))
{//按价格排序
sp = sort[j, 0];
sort[j, 0] = sort[j + 1, 0];
sort[j + 1, 0] = sp;
sp = sort[j, 1];
sort[j, 1] = sort[j + 1, 1];
sort[j + 1, 1] = sp;
sp = sort[j, 2];
sort[j, 2] = sort[j + 1, 2];
sort[j + 1, 2] = sp;
}
else if (Convert.ToInt32(sort[j, 2]) == Convert.ToInt32(sort[j + 1, 2]))
{//价格相同,按名称排序
if (sort[j, 0].Length > sort[j + 1, 2].Length)
{
sp = sort[j, 0];
sort[j, 0] = sort[j + 1, 0];
sort[j + 1, 0] = sp;
sp = sort[j, 1];
sort[j, 1] = sort[j + 1, 1];
sort[j + 1, 1] = sp;
sp = sort[j, 2];
sort[j, 2] = sort[j + 1, 2];
sort[j + 1, 2] = sp;
}
else if (sort[j, 0].Length == sort[j + 1, 2].Length)
{//名称长度相同按年份排序
if (Convert.ToInt32(sort[j, 1]) > Convert.ToInt32(sort[j + 1, 1]))
{
sp = sort[j, 0];
sort[j, 0] = sort[j + 1, 0];
sort[j + 1, 0] = sp;
sp = sort[j, 1];
sort[j, 1] = sort[j + 1, 1];
sort[j + 1, 1] = sp;
sp = sort[j, 2];
sort[j, 2] = sort[j + 1, 2];
sort[j + 1, 2] = sp;
}
}
}
}
}
for (var i = 0; i < a; i++)
{
Console.WriteLine(sort[i, 0] + " " + sort[i, 1] + " " + sort[i, 2]);
}
}
else
{
for (var i = 0; i < a ; i++)
{
for (var j = 0; j < a - 1-i; j++)
{
if (sort[j, 0].Length > sort[j + 1, 2].Length)
{//按名称排序
sp = sort[j, 0];
sort[j, 0] = sort[j + 1, 0];
sort[j + 1, 0] = sp;
sp = sort[j, 1];
sort[j, 1] = sort[j + 1, 1];
sort[j + 1, 1] = sp;
sp = sort[j, 2];
sort[j, 2] = sort[j + 1, 2];
sort[j + 1, 2] = sp;
}
else if (sort[j, 0].Length == sort[j + 1, 2].Length)
{//名称相同按年份
if (Convert.ToInt32(sort[j, 1]) > Convert.ToInt32(sort[j + 1, 1]))
{
sp = sort[j, 0];
sort[j, 0] = sort[j + 1, 0];
sort[j + 1, 0] = sp;
sp = sort[j, 1];
sort[j, 1] = sort[j + 1, 1];
sort[j + 1, 1] = sp;
sp = sort[j, 2];
sort[j, 2] = sort[j + 1, 2];
sort[j + 1, 2] = sp;
}
else if (Convert.ToInt32(sort[j, 1]) == Convert.ToInt32(sort[j + 1, 1]))
{//年份相同按价格
if (Convert.ToInt32(sort[j, 2]) > Convert.ToInt32(sort[j + 1, 2]))
{
sp = sort[j, 0];
sort[j, 0] = sort[j + 1, 0];
sort[j + 1, 0] = sp;
sp = sort[j, 1];
sort[j, 1] = sort[j + 1, 1];
sort[j + 1, 1] = sp;
sp = sort[j, 2];
sort[j, 2] = sort[j + 1, 2];
sort[j + 1, 2] = sp;
}
}
}
}
}
for (var i = 0; i < a; i++)
{
Console.WriteLine(sort[i, 0] + " " + sort[i, 1] + " " + sort[i, 2]);
}
}
}
}