簡體   English   中英

用 C# 語言將最接近的溫度歸零

[英]Return closest temperature to zero in C# language

我被問到以下問題來編程,下面是我創建的代碼。 有更好的方法嗎? 謝謝,

實現方法 ClosestToZero 以返回屬於數組 ts 的接近零的溫度。

• 如果 ts 為空,則返回 0(零)

• 如果兩個數字盡可能接近於零,則認為正整數更接近於零(例如,-5 和 5,返回 5) 輸入:

• 溫度總是用-273 to 5526范圍內的浮點數表示

namespace ConsoleApp5
{
    class Program
    {
        static void Main(string[] args)
        {
            double[] ts = { 7, 12, -2, 8, 1 };
            var result = closetozero(ts);
            Console.WriteLine(result);
        }
        public static double closetozero(double[] ts)
        {
            int targetNumber = 0;
            var nearest = ts.OrderBy(x => Math.Abs((long)x - targetNumber)).First();
            return nearest;
        }
    }
}

嘗試這個:

using System;
using System.IO;
using System.Text;
using System.Collections;
using System.Collections.Generic;

class Solution
{
    static void Main(string[] args)
    {
        int n = int.Parse(Console.ReadLine()); 
        string[] inputs = Console.ReadLine().Split(' ');
        int closest=0;
        if(n>0)
        {
          closest=int.Parse(inputs[0]);
          for (int i = 1; i < n; i++)
          {
            int t = int.Parse(inputs[i]); 
            if(Math.Abs(closest-0)>Math.Abs(t-0))
            {closest=t;}
            else if(Math.Abs(closest-0) == Math.Abs(t-0))
            {
                if(closest-Math.Abs(closest)==0 && t-Math.Abs(t)==0)
                {closest=closest;}
                else if(closest-Math.Abs(closest)==0)
                {closest=closest;}
                else if(t-Math.Abs(t)==0)
                {closest=t;}
            }
         }
       }
          Console.WriteLine(closest);
      }
   }
public static double ClosestToZero(double[] ts)
{

    if(ts == null || ts.LongLength ==0 )
        return 0;

    double a = ts[0];
    double b;
    for(int i = 0; i <= ts.LongLength-1; i++){
        if(Math.Abs(a) > Math.Abs(ts[i])){
            a = ts[i];
        }
        else if(Math.Abs(a) == Math.Abs(ts[i]))
        {  
            a = a > ts[i] ? a : Maths.Abs(ts[i]); 
        }
    }

    return a;

}

正如 Sweeper 所說,定義“更好”。

在運行時方面:拋棄 LINQ,寫出循環。 問題是您的方法構建了一個值列表,然后對它們進行排序(O(n log n)。一個簡單的循環不進行內存分配並在 O(n)時間內運行。

此外,在我看來,+5 在您的代碼中優於 -5,因此絕對是錯誤的。

一種“更好”的方法是使用ForEach並隨時存儲最接近零的值,同時考慮 +/-。 沒有理由對整個數組進行排序。

    public static int ComputeClosestToZero(int[] ts)
    {
        // Write your code here
        // To debug: Console.Error.WriteLine("Debug messages...");
        if (ts == null || ts.Length == 0)
            return 0;
        if (ts.Length == 1)
            return ts[0];
        if (ts.Length == 2 )
            return ts[0]>=ts[1] ? ts[0] :ts[1];

        int a = ts[0];
        bool b = false;
        for (int i = 1; i <= ts.Length - 1; i++)
        {
            if (ts[i] > 0 && a > 0)
            {
                b = true;
                if (ts[i] == 1)
                    return ts[i];
                if (a > ts[i])
                {
                    a = ts[i];
                }
               
            }
            else if(!b)
            {
                if (ts[i] == -1)
                    return ts[i];
                if (a < ts[i])
                {
                    a = ts[i];
                }
            }
        }
        return a;
    }

嘗試這個:

public static double closetozero(double[] ts)
{
    return ts.Aggregate((t1, t0) => Math.Abs(t1) > Math.Abs(t0) ? t0 : t1);
}
public static double ClosestToZero(double[] ts)
{

    if(ts == null || ts.LongLength ==0 )
        return 0;

    double a = ts[0];
    double b;
    for(int i = 0; i <= ts.LongLength-1; i++){
        if(Math.Abs(a) > Math.Abs(ts[i])){
            a = ts[i];
        }
        else if(Math.Abs(a) == Math.Abs(ts[i]))
        {  
            a = a > ts[i] ? a : ts[i];
        }
    }

    return a;

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM