简体   繁体   English

如何检查数字是否更接近特定数字?

[英]How to check if a number is closer to a specific number?

write a program that repeatedly asks a user to enter numbers between 0 to 100, until the user enters a value outside that range. 编写一个程序,反复要求用户输入0到100之间的数字,直到用户输入超出该范围的值为止。 Then output the number closest to 50. 然后输出最接近50的数字。

For example: 例如:

Enter a number: 91.3 输入数字:91.3
Enter a number: 64.4 输入数字:64.4
Enter a number: 38.12 输入数字:38.12
Enter a number: 46.9 输入数字:46.9
Enter a number: 99.45 输入数字:99.45
Enter a number: 103. 输入数字:103。

The number closest to 50 is 46.900000 最接近50的数字是46.900000

I have only learnt do,while,for loops, if and else if selection statements. 我只学过for,while,for循环,if和else if选择语句。

I understand I would need an if statement that checks the difference between each number with 50, the smaller the difference, the closer the number to 50. I included stdlib.h because I figured I would need to take the absolute value of the difference to avoid negative number. 我知道我需要一个if语句来检查每个数字与50之间的差异,差异越小,数字越接近50。我包含了stdlib.h,因为我认为我需要将差异的绝对值取为避免负数。 Yet I do not know how to incorporate this thought into the codes. 但是我不知道如何将这种思想纳入代码中。 How should the code be written? 代码应如何编写?

#include<stdio.h>
#include<stdlib.h>

int main (void)
{
  float num;
  printf("Enter a number:");
  scanf("%f",&num);

  while (num>=0 && num<=100)
  {
    printf("Enter a number:");
    scanf("%f",&num);
    //I think I would need an if statement here to check the difference
  }
  printf("\nThe closest is %.2f",/*I think I would need a new variable here.*/ );
  return 0;
}

It is a math problem hidden in a programming problem. 这是隐藏在编程问题中的数学问题。

To find the closest number, you would have the smallest absolute value of the difference between two numbers, so you need to take the difference of all the numbers and keep in mind which difference (and the number it corresponds to) has the smallest absolute value. 要找到最接近的数字,您将获得两个数字之间的差异的最小绝对值,因此您需要考虑所有数字的差异,并牢记哪个差异(及其对应的数字)具有最小的绝对值。

After reading each number, find the difference between that number and 50. Keep track of the minimum difference found till now and compare it with current difference. 读取每个数字后,找到该数字与50之间的差异。跟踪到目前为止找到的最小差异,并将其与当前差异进行比较。 Update the minimum difference 更新最小差异

float result;
float mindiff = 100; // minimum difference, initialize with any value greater than 50
scanf("%f", &num);
while(num>=0 && num<=100)
{
    //check with minimum till now and update accordingly
    if( fabs(num-50) < mindiff)
    {
        mindiff = fabs(num-50);
        result = num;
    }

    //read next number
    printf("Enter a number:");
    scanf("%f",&num);
}

Now result holds the value you need 现在result拥有您所需的价值

You would need an additional variable float ClosestToFifty which would be updated with a statemet like the following. 您将需要一个附加变量float ClosestToFifty ,该变量将使用如下所示的statemet更新。

if (fabs(num - 50.0f) < fabs(ClosestToFifty - 50.0f) )
{
    ClosestToFifty = num:
}

ClosestToFifty could be initialized with some value outside the valid range, say 200.0f . ClosestToFifty可以用有效范围之外的一些价值被初始化,说200.0f

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM