1.Vito's family
世界聞名的黑社會老大Vito Deadstone要搬到紐約來了。在那裡他有一個大家族,並且他們都住在Lamafia大道上。因為Vito時常要拜訪所有的親戚,他想要找一間離他們最近的房子,也就是說他希望從他的家到所有的親戚的家的距離的和為最小。
他恐嚇你寫一個程式來幫助幫助他解決這個問題。
Input
輸入的第一列有一個整數代表以下有多少組測試資料。
每組測試資料一列,第一個整數 r(0 < r < 500),代表他親戚的數目。接下來的r個整數s1,s2,......sr為這些親戚房子的門牌號碼(0 < si <30000)。注意:有些親戚的門牌號碼會相同。
Output
對每一組測試資料,輸出從他的新家到所有的親戚的家的距離的和為最小為多少。2個門牌號碼si、sj的距離為si-sj的絕對值。
Sample Input
3
2 2 4
3 2 4 6
4 2 1 999 5
Sample Output
2
4
1001
static void Main(string[] args)
{
int N = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("---------------------------");
int distance, R,
Center;
for (int i = 0; i < N;
i++)
{
List<int> Si = new List<int>();
R = Center = distance = 0;
R = Convert.ToInt32(Console.ReadLine());
for (int j = 0; j < R;
j++)
Si.Add(Convert.ToInt32(Console.ReadLine()));
Si.Sort();
Console.WriteLine();
Center = Si[R / 2];
for (int j = 0; j < R;
j++)
distance += Math.Abs(Center -
Si[j]);
Console.WriteLine(distance
+ "\n------------------------");
}
}
2.Hashmat the brave warrior
Hashmat is a
brave warrior who with his group of young soldiers moves from one place to
another to fight against his opponents. Before Fighting he just calculates one
thing, the difference between his soldier number and the opponent’s soldier
number. From this difference he decides whether to fight or not. Hashmat’s
soldier number is never greater than his opponent.
Input
The input
contains two numbers in every line. These two numbers in each line denotes the
number soldiers in Hashmat’s army and his opponent’s army or vice versa. The
input numbers are not greater than 2 32. Input is terminated by ‘End of File’.
Output
For each line of
input, print the difference of number of soldiers between Hashmat’s army and
his opponent’s army. Each output should be in seperate line.
Sample Input
10 12
10 14
100 200
Sample Output
2
4
100
static
void
Main(string[] args)
{
while
(true)
{
int
a = Convert.ToInt32(Console.ReadLine());
int
b = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("相差: {0}",b-a);
}
}
3.Primary Arithmetic
內容 :
在小學時我們都做過加法的運算,就是把2個整數靠右對齊然後,由右至左一位一位相加。如果相加的結果大於等於10就有進位(carry)的情況出現。你的任務就是要判斷2個整數相加時產生了幾次進位的情況。這將幫助小學老師分析加法題目的難度。
輸入說明 :
每一列測試資料有2個正整數,長度均小於10位。最後一列有2個0代表輸入結束。
輸出說明 :
每列測試資料輸出該2數相加時產生多少次進位,請參考Sample
Output。注意進位超過1次時operation有加s
範例輸入 :
123 456
555 555
123 594
0 0
範例輸出:
No carry operation.
3 carry operations.
1 carry operation.
static void Main(string[] args)
{
while (true)
{
string[] answer = new string[2];
int carry =
0;
answer = Console.ReadLine().Split(' ');
if
(answer[0] == "0" && answer[1] == "0") return;
else
{
for (int i = 0;
i < 3; i++)
{
char[] c1 =
answer[0].ToCharArray();
char[] c2 =
answer[1].ToCharArray();
if (Convert.ToInt32(c1[i].ToString())
+ Convert.ToInt32(c2[i].ToString())
>= 10)
carry++;
}
}
if(carry
== 0) Console.WriteLine("No
carry operation.");
else Console.WriteLine("{0}
carry operation",carry);
}
}
}
沒有留言:
張貼留言