酷!學園
其他討論區 => 肉腳版 => 主題作者是: 夢無痕 於 2009-10-03 11:10
-
這是老師出的題目
這題在讀取一個三角形的三邊長後,算出三角形面積。
輸入:每個問題輸入分三行,一行一個大於零的整數,當第一個數字為 0 時,表示問題結束。
輸出:面積值,精確到小數點後三位。
範例:
Input:
1
1
1
3
4
5
0
Output:
0.433
6.000
程式要怎樣寫可以讓他三個一組 然後出入0他就停止
小弟功力不才只能寫到
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string str1, str2, str3;
str1 = Console.ReadLine();;
str2 = Console.ReadLine();
str3 = Console.ReadLine();
double x, y, z, ans1, total, answer;
x = Convert.ToInt16(str1);
y = Convert.ToInt16(str2);
z = Convert.ToInt16(str3);
ans1 = ((x + y + z) / 2);
total = ans1 * (ans1 - x) * (ans1 - y) * (ans1 - z);
answer = Math.Sqrt(total);
Console.WriteLine(ans1);
Console.WriteLine(total);
Console.WriteLine("{0:F3}", answer);
Console.ReadLine();
}
}
}
希望各位程式神人可以指教指教
-
這是老師出的題目
既然是作業, 那就自己加油吧~
該作的功課還是要自己作!!
-
參考看看:
using System;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
ArrayList sides = new ArrayList();
ArrayList answers = new ArrayList();
string input;
do
{
input = Console.ReadLine();
if (sides.Count >= 3)
{
double p = 0;
foreach (int s in sides)
{
p += s;
}
p = p / 2;
answers.Add(Math.Sqrt(p * (p - int.Parse(sides[0].ToString())) * (p - int.Parse(sides[1].ToString())) * (p - int.Parse(sides[2].ToString()))));
sides.Clear();
}
int i;
if (int.TryParse(input, out i))
{
sides.Add(i);
}
else
{
Console.WriteLine("無效輸入");
}
} while (input != "0");
Console.WriteLine("Output:");
if (answers.Count > 0)
{
foreach (double answer in answers)
{
Console.WriteLine("{0:F3}", answer);
}
}
Console.ReadLine();
}
}
}
-
感謝大大
可是老師他目前也才交 if else 和 while
還沒有敎到陣列的部份
不知道有沒有簡單的方法
-
自己想辦法
-
不用陣列? 那用 string to char 嗎? 好像不會比較簡單耶!!
因為user keyin 的數值是不確定數不太可能用單純幾個變數!
老師有規定不能用 array 嗎?
-
他沒有強制規定不能用
可是因為才上第二堂課
不太可能就直接用陣列
對於一個完全沒接觸過程式的新手
會不會太強為所難
因此我擔心會不會我學的太多想的太複雜
把題目複雜化了
因此想說來網路PO看看能不能找到C#神人
-
那就要看你的老師怎麼想囉
誰說沒教過就不能用陣列,
以前我在讀書時, 很多指令及演算法老師都搞不清楚哩,
當然你要自己有把握真的會這些東西, 這樣才有意義,
以前我遇到一個C語言的老師, 評分不是只看結果畫面,
他還要要看程式的精鍊度跟執行時的效能, 而且沒有註解一定不及格.
有些東西用內建的函數來處理很方便, 但是效能不一定好
-
不用陣列自然也做得到,只是這種寫法寫完就算了沒有很大的意義。
比如說如果輸入的數值換成十個以上,難道宣告十個變數寫十個 if 嗎?
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
double x = 0, y = 0, z = 0;
string input= "";
string result = "";
while ((input = Console.ReadLine()) != "0")
{
int i;
if (int.TryParse(input, out i))
{
if (x <= 0) { x = i; continue; }
if (y <= 0) { y = i; continue; }
if (z <= 0)
{
z = i;
double p = (x + y + z) / 2;
result += Math.Round(Math.Sqrt(p * (p - x) * (p - y) * (p - z)), 3).ToString("0.000") + "\r\n";
x = 0;
y = 0;
z = 0;
}
}
}
Console.WriteLine("Output:");
Console.Write(result);
Console.ReadLine();
}
}
}