📌  相关文章
📜  使用给定的总和和平均值来查找缺失天的温度

📅  最后修改于: 2021-05-04 15:13:34             🧑  作者: Mango

鉴于整数xy表示本周除了分别天第一天第2天的平均温度,和第一天第2天S的温度的总和,任务是找到天第一天第2天的温度。
例子:

方法:我们知道平均值=所有观察值的总和/观察总数。因此,观测值的总和=平均值*观测值的数量,即S = A * n

解决上述两个方程式,Day1和Day2的值由下式给出:

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function for finding the temperature
void findTemperature(int x, int y, int s)
{
    double Day1, Day2;
 
    // Store Day1 - Day2 in diff
    double diff = (x - y) * 6;
 
    Day2 = (diff + s) / 2;
 
    // Remaining from s will be Day1
    Day1 = s - Day2;
 
    // Print Day1 and Day2
    cout << "Day1 : " << Day1 << endl;
    cout << "Day2 : " << Day2 << endl;
}
 
// Driver Code
int main()
{
    int x = 5, y = 10, s = 40;
 
    // Functions
    findTemperature(x, y, s);
 
    return 0;
}


Java
// Java program for the above approach
class GFG{
  
// Function for finding the temperature
static void findTemperature(int x, int y, int s)
{
    double Day1, Day2;
  
    // Store Day1 - Day2 in diff
    double diff = (x - y) * 6;
  
    Day2 = (diff + s) / 2;
  
    // Remaining from s will be Day1
    Day1 = s - Day2;
  
    // Print Day1 and Day2
    System.out.println( "Day1 : " + Day1);
    System.out.println( "Day2 : " + Day2);
}
  
// Driver Code
public static void main(String[] args)
{
    int x = 5, y = 10, s = 40;
  
    // Functions
    findTemperature(x, y, s);
}
}
 
// This code is contributed by rock_cool


Python
# Python3 program for the above approach
 
# Function for finding the temperature
def findTemperature(x, y, s):
 
    # Store Day1 - Day2 in diff
    diff = (x - y) * 6
    Day2 = (diff + s) // 2
 
    # Remaining from s will be Day1
    Day1 = s - Day2
 
    # Print Day1 and Day2
    print("Day1 : ", Day1)
    print("Day2 : ", Day2)
 
# Driver Code
if __name__ == '__main__':
    x = 5
    y = 10
    s = 40
 
    # Functions
    findTemperature(x, y, s)
 
# This code is contributed by Mohit Kumar


C#
// C# program for the above approach
using System;
class GFG{
   
// Function for finding the temperature
static void findTemperature(int x, int y, int s)
{
    double Day1, Day2;
   
    // Store Day1 - Day2 in diff
    double diff = (x - y) * 6;
   
    Day2 = (diff + s) / 2;
   
    // Remaining from s will be Day1
    Day1 = s - Day2;
   
    // Print Day1 and Day2
    Console.Write( "Day1 : " + Day1 + '\n');
    Console.WriteLine( "Day2 : " + Day2 + '\n');
}
   
// Driver Code
public static void Main(string[] args)
{
    int x = 5, y = 10, s = 40;
   
    // Functions
    findTemperature(x, y, s);
}
}
 
// This code is contributed by Ritik Bansal


Javascript


输出:
Day1 : 35
Day2 : 5

时间复杂度: O(1)
辅助空间: O(1)