补全程序
测试输入: 红楼梦 西游记 三国演义 水浒传
43 29 39 27
红楼梦
预期输出: 43
using System;
namespace IndexSample
{
class Book
{
string id;
int price;
public string ID
{
get
{ return id; }
set
{ id = value; }
}
public int Price
{
get
{ return price; }
set
{ price = value; }
}
public Book()
{
id = ""; price = 0;
}
}
class MyClass
{
private Book[] data;
public int Length
{
get
{ return data.Length; }
}
// 索引器定义,根据下标访问data
public Book this[int index]
{
get
{
if (index >= 0 && index <= Length - 1)
return data[index];
else
return null;
}
set
{
if (index >= 0 && index <= Length - 1)
data[index] = value;
}
}
// 重载下标为字符串类型的索引器
public Book this[string index]
{
get
{
//************BEGIN****************
//*************END*****************
}
}
public MyClass(Book[] st1)
{
data = st1;
}
public MyClass(string[] id1, int[] price1)
{
data = new Book[id1.Length];
for (int i = 0; i < id1.Length; i++)
{
data[i] = new Book();
(data[i]).ID = id1[i]; ;
(data[i]).Price = price1[i];
}
}
}
class MyClient
{
static void Main(string[] args)
{
//将测试集输入的第一行中的每个单词组成字符串数组
string[] str = (Console.ReadLine()).Split(' ');
//将测试集输入的第一行中的每个字符串组成价格数组
string[] prices = (Console.ReadLine()).Split(' ');
int[] price = new int[prices.Length];
for (int i = 0; i < price.Length; i++)
{
price[i] = Convert.ToInt32(prices[i]);
}
MyClass mc = new MyClass(str, price);
//提取第三行-要查看价格的书名
string book = Console.ReadLine();
// 输出此书的价格
Console.WriteLine("The price of {0} is {1}", mc[book].ID, mc[book].Price);
}
}
}