简体   繁体   English

将正数四舍五入到下一个最接近的 5 的倍数

[英]Rounding off a positive number to the next nearest multiple of 5

I need to round of a number (the input is guaranteed to be an integer & is positive) to the next multiple of 5.我需要将一个数字(输入保证为 integer 且为正)舍入到下一个 5 的倍数。

I tried this:我试过这个:

int round = ((grades[j] + 2)/5) * 5;

However, this rounds off the number to the nearest multiple of 5.但是,这会将数字四舍五入为最接近的 5 的倍数。

Eg: 67 is rounded off to 65, not 70.例如:67 四舍五入为 65,而不是 70。

To round up the general form should be:四舍五入的一般形式应该是:

((n + denominator -1) / denominator )* denominator 

so in your case:所以在你的情况下:

int round = ((grades[j] + 4)/5) * 5;

The reason we deduct 1 from the denominator is to handle exact multiples of the rounding value for instance:我们从分母中减去 1 的原因是为了处理舍入值的精确倍数,例如:

((70 + 4) / 5) * 5

would yield 70将产生70

You can take the difference between grades[j] and the next number, and just add it.您可以将grades[j]与下一个数字之间的差值相加,然后将其相加。 For example, if grades[j] == 12 then 12 mod 5 == 2 , so add 5 - 2 .例如,如果grades[j] == 1212 mod 5 == 2 ,所以添加5 - 2

Here is a sample program to test it out:这是一个示例程序来测试它:

#include <iostream>

int main() {
    int x[] = {2,7,123,32}; // some random numbers to show how this works
    for (int i = 0; i < 4; {
        std::cout << x[i] << "\t" << x[i] + ((5-(x[i] % 5)) % 5) << std::endl;
    }
    return 0;
}

Output:输出:

2   5
7   10
123 125
32  35
int mod = grades[j] % 5;
int round = grades[j] - mod;
if (mod > 0) {
    round += 5;
}

This is my solution using cmath::abs这是我使用cmath::abs解决方案

int rounded = n + abs((n % denom) - denom);

You can change the denom with any other denomination you want您可以更改denom你想要的任何其他面额

Try this:尝试这个:

int num = grades[j] % 5 < 3 ? grades[j] - (grades[j] % 5) : grades[j] - (grades[j] % 5) + 5;

Here demo code:这里演示代码:

#include <stdio.h>

int main() {
    //code
    int grades[5] = {10, 68, 12, 67, 41};
    int j;
    for (j = 0; j < 5; j++)
    {
        int num = grades[j] % 5 < 3? grades[j] - (grades[j] % 5) : grades[j] - (grades[j] % 5) + 5;
        printf("%d\n",num);
    }

    return 0;
}

Output:输出:

10
70
10
65
40

I hope help you.我希望能帮到你。

One algorithm that works is:一种有效的算法是:

  1. half the round value圆形值的一半
  2. get the remainder of number by dividing it with roundValue通过将其除以 roundValue 得到数字的余数
  3. get quotient of division (so that we can multiply with it in end)得到除法的商(这样我们就可以最后乘以它)
  4. comparing it with the half of round Value将其与轮值的一半进行比较
  5. if it's greater than half it rounds to the nearest greater value如果它大于一半,则四舍五入到最接近的较大值
  6. if it's less than half it rounds to the nearest smaller value如果它小于一半,则四舍五入到最接近的较小值

In code:在代码中:

     int round(int nearest , int number){
            int half = nearest / 2;
            int answer = 0;  
            int remainder = number% nearest ;
            int quotient = number/ nearest ;
            if(remainder > half ){
                answer =(quotient+1) * nearest ;
            }else{
                answer =quotient * nearest ;
            }
        return answer;
        }

I have a function to round up:我有一个功能要四舍五入:

def next(number, base):
    temp = round(number / base) * base
    return temp

you can use it for your case like this:您可以像这样将它用于您的案例:

next(grade[j], 5)

and in function it will be:在功能上它将是:

temp = round(67 / 5) * 5
return temp #temp = 75

some case not using round but using ceil某些情况下不使用圆形而是使用 ceil

Why so much of complication.为什么这么复杂。 Here is the code.这是代码。 Have a look.看一看。

vector<int> gradingStudents(vector<int> grades) {
    int size = grades.size();
    for(int i=0;i<size;i++)
    {
        if(grades[i]>40)
        {
            if((grades[i]+2)%5==0)
            grades[i]=grades[i]+2;
            else if((grades[i]+1)%5==0)
            grades[i]=grades[i]+1;
        }
        else if(grades[i]>37&&grades[i]<40)
        grades[i]=40;
    }
return grades;
}
def next_multiple_of(number, value):
    return (value // number + 1) * 5

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

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