负数的除法和取模运算规则
2019-04-14 16:24发布
生成海报
负数的除法和取模运算规则
1/*
2 *测试负数除法和取模运算
3 */
4#include
5
6 int main( void )
7 {
8 printf ("-5
/ 2 = %d ", -5/2);
9 printf ("-5
%% 2 = %d
", -5%2);
10 printf ("5
/ -2 = %d ",5/
-2);
11 printf ("5
%% -2 = %d
",5%
-2);
12 printf ("-5
/ -2 = %d ", -5/
-2);
13 printf ("-5
%% -2 = %d
", -5%
-2);
14
15 return 0;
16 }
17
18
19
20/*
21 *
-5 / 2 = -2 -5 % 2 = -1 ( -5 = 2 * ( -2 ) + ( -1 ) )
22 *
5 / -2 = -2 5 % -2 = 1 ( 5 = ( -2 ) * ( -2 ) + 1 )
23 *
-5 / -2 = 2 -5 % -2 = -1 ( -5 = ( -2 ) * 2 + ( -1 ) )
24 *
25 * 被除数、除数、商和余数满足以下关系:
26 * 被除数
=除数 *商
+余数
27 * 余数和被除数的符号一致,而且其绝对值小于除数
28 * 如果被除数和除数的符号一致,那么商为非负数,否则,为非正数
29 *
30 */
/*
* 测试负数除法和取模运算
*/
#include
int main ( void )
{
printf ( "-5 / 2 = %d ", -5 / 2 );
printf ( "-5 %% 2 = %d
", -5 % 2 );
printf ( "5 / -2 = %d ", 5 / -2 );
printf ( "5 %% -2 = %d
", 5 % -2 );
printf ( "-5 / -2 = %d ", -5 / -2 );
printf ( "-5 %% -2 = %d
", -5 % -2 );
return 0;
}
/*
* -5 / 2 = -2 -5 % 2 = -1 ( -5 = 2 * ( -2 ) + ( -1 ) )
* 5 / -2 = -2 5 % -2 = 1 ( 5 = ( -2 ) * ( -2 ) + 1 )
* -5 / -2 = 2 -5 % -2 = -1 ( -5 = ( -2 ) * 2 + ( -1 ) )
*
* 被除数、除数、商和余数满足以下关系:
* 被除数 = 除数 * 商 + 余数
* 余数和被除数的符号一致,而且其绝对值小于除数
* 被除数和除数的符号一致,那么商为正数,否则为负数
*
*/
打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮