[英]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] == 12
则12 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:一种有效的算法是:
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.