CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
英特尔®游戏设计大赛100美元现金周周送 专题改版:Java Web 专题
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  专题开发/技术/项目 >  数据结构与算法

基本问题:取余运算规则

楼主bigtea(企鹅)2003-08-03 17:25:33 在 专题开发/技术/项目 / 数据结构与算法 提问

忘记了,比如对于负数的规定。 问题点数:20、回复次数:4Top

1 楼Riemann()回复于 2003-08-03 17:47:23 得分 5

我的印象中:  
  a%b=r,r=0,1...r-1  
  例如(-5)%8,结果应该为3Top

2 楼happy__888([顾问团]寻开心 www.e-jjj.com)回复于 2003-08-04 13:46:44 得分 5

a%b   =   k*b   +   d    
  其中K是整数,D是正整数  
  和楼上的一样Top

3 楼BlueSky2008(懒惰是程序员的美德)回复于 2003-08-04 18:34:58 得分 10

理论上,我也同意楼上两位的看法。  
  不过在VC++6.0中,(-k)%n   =   -(k%n)         (k>=0)    
  所以(-5)%8,结果是-5   :)Top

4 楼BlueSky2008(懒惰是程序员的美德)回复于 2003-08-04 19:02:46 得分 0

找到了,取余结果的绝对值是两个操作数的绝对值的取余结果,取余结果的符号是第一个操作数的符号。(不过这是Microsoft   自己定义的)  
   
  下面是MSDN中的原文:  
   
  %     The   result   of   the   remainder   operator   is   the   remainder   when   the   first   operand   is   divided   by   the   second.   When   the   division   is   inexact,   the   result   is   determined   by   the   following   rules:    
    If   the   right   operand   is   zero,   the   result   is   undefined.    
     
    If   both   operands   are   positive   or   unsigned,   the   result   is   positive.    
     
    If   either   operand   is   negative   and   the   result   is   inexact,   the   result   is   implementation   defined.   (See   the   Microsoft   Specific   section   below.)    
     
   
   
  Microsoft   Specific   —>  
   
  In   division   where   either   operand   is   negative,   the   direction   of   truncation   is   toward   0.  
   
  If   either   operation   is   negative   in   division   with   the   remainder   operator,   the   result   has   the   same   sign   as   the   dividend   (the   first   operand   in   the   expression).    
   
  END   Microsoft   Specific  
   
  Examples  
   
  The   declarations   shown   below   are   used   for   the   following   examples:  
   
  int   i   =   10,   j   =   3,   n;  
  double   x   =   2.0,   y;  
   
  This   statement   uses   the   multiplication   operator:  
   
  y   =   x   *   i;  
   
  In   this   case,   x   is   multiplied   by   i   to   give   the   value   20.0.   The   result   has   double   type.  
   
  n   =   i   /   j;  
   
  In   this   example,   10   is   divided   by   3.   The   result   is   truncated   toward   0,   yielding   the   integer   value   3.  
   
  n   =   i   %   j;  
   
  This   statement   assigns   n   the   integer   remainder,   1,   when   10   is   divided   by   3.  
   
  Microsoft   Specific   —>  
   
  The   sign   of   the   remainder   is   the   same   as   the   sign   of   the   dividend.   For   example:  
   
  50   %   -6   =   2  
  -50   %   6   =   -2  
   
  In   each   case,   50   and   2   have   the   same   sign.    
   
  END   Microsoft   Specific  
   
  Top

相关问题

  • 位运算的最基本的问题
  • 请教一个关于位运算的基本问题???
  • 关于取余运算符的问题?
  • 初学,请问Oracle8i中取余的运算符是什么?
  • 基本问题,在线等待:如何实现整数与浮点数的混合除法运算?
  • 设计出一个新的数据类型完成任意整数间的基本运算
  • 矩阵运算
  • 位运算
  • 运算符?
  • 大数运算

关键词

  • microsoft
  • operand
  • remainder
  • negative
  • division
  • 取余结果
  • result
  • operator

得分解答快速导航

  • 帖主:bigtea
  • Riemann
  • happy__888
  • BlueSky2008

相关链接

  • CSDN Blog
  • 技术文档
  • 代码下载
  • 第二书店
  • 读书频道

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
世纪乐知(北京)网络技术有限公司 版权所有, 京 ICP 证 020026 号
北京创新乐知广告有限公司 提供技术支持
Copyright © 2000-2007, CSDN.NET, All Rights Reserved
GongshangLogo