哪些运算符可用于整数除法?
What operators can be used to ceil an integer division?
我在下面有这段代码,它假设生成 11 个矮人的输出,矮人身高输入 10,输入值 101、102、103、104,105、106、107、108、109 以及 110当矮人的高度再次为 10 时,巨大的高度然后变为 12 个矮人的输出,巨人高度的值为 111-120 等等。'n' 和 's' 具有基本相同的解决方案:正确的输出值 101 - 109 的小矮人,但对于值 110 的输出不正确(应该是 11,但给出 10)。可以使用哪些运算符来帮助解决这个问题。我正在研究我的运算符用法,强烈不希望使用任何条件语句或 if-then。
#include <stdio.h>
int main()
{
int g, d, n, s;
while(1)
{
printf("enter the heights of giant and drwafs: ");
scanf("%d%d", &g, &d);
n = g % d + (g/d) + (-g % d + 1) - (g%d + 1)/10;
s = g / d + 1;
printf("%d",s);
printf("it takes %d dwarfs to be "
"greater than or equal to giant.\n\n", s );
}
return 0;
}
整数除法上限:
unsigned int dividend, divisor, c;
/* dividend != 0 && divisor != 0 */
c = 1 + ((dividend - 1) / divisor);
您的程序将如下所示:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
unsigned int giant, dwarf, c;
printf("Height of giant: ");
scanf("%u", &giant);
printf("Height of dwarf: ");
scanf("%u", &dwarf);
/* the heights can't be 0 */
if (giant == 0 || dwarf == 0) {
printf("The heights need to be greater than 0.\n");
exit(EXIT_FAILURE);
}
/* ceil giant divided by dwarf */
c = 1 + ((giant - 1) / dwarf);
printf("It takes %u dwarfs to be as high "
"or higher than a giant.\n", c);
return 0;
}
您可以使用 NOT 运算符,如果您有余数则加 1,如果您没有余数则只是除法的结果:
include <stdio.h>
int main()
{
int g, d, s;
while(1)
{
printf("enter the heights of giant and drwafs: ");
scanf("%d %d", &g, &d);
if (d <= 0 || g <= 0) // what if your user use 0 for the size of dwarf? ouch...
continue
s = g / d + !!(g % d);
printf("%d",s);
printf("it takes %d dwarfs to be "
"greater than or equal to giant.\n\n", s );
}
return 0;
}
注意:此解决方案适用于 giant = INT_MAX
我在下面有这段代码,它假设生成 11 个矮人的输出,矮人身高输入 10,输入值 101、102、103、104,105、106、107、108、109 以及 110当矮人的高度再次为 10 时,巨大的高度然后变为 12 个矮人的输出,巨人高度的值为 111-120 等等。'n' 和 's' 具有基本相同的解决方案:正确的输出值 101 - 109 的小矮人,但对于值 110 的输出不正确(应该是 11,但给出 10)。可以使用哪些运算符来帮助解决这个问题。我正在研究我的运算符用法,强烈不希望使用任何条件语句或 if-then。
#include <stdio.h>
int main()
{
int g, d, n, s;
while(1)
{
printf("enter the heights of giant and drwafs: ");
scanf("%d%d", &g, &d);
n = g % d + (g/d) + (-g % d + 1) - (g%d + 1)/10;
s = g / d + 1;
printf("%d",s);
printf("it takes %d dwarfs to be "
"greater than or equal to giant.\n\n", s );
}
return 0;
}
整数除法上限:
unsigned int dividend, divisor, c;
/* dividend != 0 && divisor != 0 */
c = 1 + ((dividend - 1) / divisor);
您的程序将如下所示:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
unsigned int giant, dwarf, c;
printf("Height of giant: ");
scanf("%u", &giant);
printf("Height of dwarf: ");
scanf("%u", &dwarf);
/* the heights can't be 0 */
if (giant == 0 || dwarf == 0) {
printf("The heights need to be greater than 0.\n");
exit(EXIT_FAILURE);
}
/* ceil giant divided by dwarf */
c = 1 + ((giant - 1) / dwarf);
printf("It takes %u dwarfs to be as high "
"or higher than a giant.\n", c);
return 0;
}
您可以使用 NOT 运算符,如果您有余数则加 1,如果您没有余数则只是除法的结果:
include <stdio.h>
int main()
{
int g, d, s;
while(1)
{
printf("enter the heights of giant and drwafs: ");
scanf("%d %d", &g, &d);
if (d <= 0 || g <= 0) // what if your user use 0 for the size of dwarf? ouch...
continue
s = g / d + !!(g % d);
printf("%d",s);
printf("it takes %d dwarfs to be "
"greater than or equal to giant.\n\n", s );
}
return 0;
}
注意:此解决方案适用于 giant = INT_MAX