拼图:不使用+ OR添加一个 - [英] Puzzle: Add One Without Using + OR -

查看:51
本文介绍了拼图:不使用+ OR添加一个 - 的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问:

编写一个C / C ++程序,该程序采用无符号整数并加1,而不使用加号或减号。
。不要担心溢出问题。它使用汇编命令作弊,或写一个使用

ASCII码插入加号或减号的预处理器。有很多解决方案,有些比其他更优雅。




这是我的解决方案想出了。我想知道是否有解决方案

更优雅或更有效。

void addOne(unsigned int& i){

int j = 1;


while(i& 1){

i>> = 1;

j << = 1;

}

i | = 1;


while!(j& 1){

i<< = 1;

j>> = 1;

}

}

Q:
Write a C/C++ program that takes an unsigned integer and adds 1 to it
without using the plus or minus signs. Don''t worry about overflow issues. It
is cheating to use assembly commands, or to write a preprocessor that uses
ASCII codes to insert the plus or minus sign. There are many solutions, some
more elegant than others.




Here''s the solution I came up with. I''m wondering if there''s a solution that
is more elegant or efficient.
void addOne(unsigned int &i) {
int j = 1;

while (i & 1) {
i >>= 1;
j <<= 1;
}
i |= 1;

while !(j & 1) {
i <<= 1;
j >>= 1;
}
}

推荐答案



" Method Man" < a@b.c>在消息新闻中写道:Bt ************ @ read1.cgocable.net ...

"Method Man" <a@b.c> wrote in message news:Bt************@read1.cgocable.net...
问:
编写一个C / C ++程序无符号整数并向其添加1
而不使用加号或减号。不要担心溢出问题。它使用汇编命令作弊,或编写使用
ASCII码插入加号或减号的预处理器。有很多解决方案,有些比其他更优雅。
Q:
Write a C/C++ program that takes an unsigned integer and adds 1 to it
without using the plus or minus signs. Don''t worry about overflow issues. It
is cheating to use assembly commands, or to write a preprocessor that uses
ASCII codes to insert the plus or minus sign. There are many solutions, some
more elegant than others.




这是一个有趣的解决方案。


#include< vector>

void addOne(unsigned int& i){

std :: vector< char> v(i);

v.push_back(''q'');

i =(unsigned int)v.size();

}


Jonathan



Here''s a funny one.

#include <vector>

void addOne(unsigned int &i) {
std::vector<char> v(i);
v.push_back(''q'');
i = (unsigned int) v.size();
}

Jonathan


* Method Man:
* Method Man:
问:
写一个采用无符号整数并在其中加1的C / C ++程序
,不使用加号或减号。不要担心溢出问题。它使用汇编命令作弊,或编写使用
ASCII码插入加号或减号的预处理器。有很多解决方案,有些比其他解决方案更优雅。

这是我想出的解决方案。我想知道是否有更优雅或更有效的解决方案。

void addOne(unsigned int& i){
int j = 1;

while(i& 1){
i>> = 1;
j<< = 1;
}
= 1;

while!(j& 1){
i<< = 1;
j>> = 1;
}
}
Q:
Write a C/C++ program that takes an unsigned integer and adds 1 to it
without using the plus or minus signs. Don''t worry about overflow issues. It
is cheating to use assembly commands, or to write a preprocessor that uses
ASCII codes to insert the plus or minus sign. There are many solutions, some
more elegant than others.

Here''s the solution I came up with. I''m wondering if there''s a solution that
is more elegant or efficient.
void addOne(unsigned int &i) {
int j = 1;

while (i & 1) {
i >>= 1;
j <<= 1;
}
i |= 1;

while !(j & 1) {
i <<= 1;
j >>= 1;
}
}




很好的尝试,但是关于风格,优先使用函数而不是

一个输入/输出参数,以及关于C ++ ,上面不应该编译:总是

编写的代码(除非重点是它没有编译)。


At为你做家庭作业的风险:


未签名的下一个(未签名的x)

{

if(x ==〜 0){return 0; }

unsigned flipmask =〜0;

while((x | flipmask)== ~0)

{

flipmask<< = 1;

}

返回x ^ ~flipmask;

}


陈述此解决方案所依赖的假设。


-

答:因为它弄乱了人们通常阅读文本的顺序。

问:为什么这么糟糕?

A:热门发布。

问:usenet上最烦人的事情是什么并在电子邮件中?



Good attempt, but regarding style, preferentially use a function rather than
an in/out argument, and regarding C++, the above shouldn''t compile: always
post code that compiles (except when the point is that it doesn''t compile).

At the risk of doing your HOMEWORK for you:

unsigned next( unsigned x )
{
if( x == ~0 ) { return 0; }
unsigned flipmask = ~0;
while( (x | flipmask) == ~0 )
{
flipmask <<= 1;
}
return x ^ ~flipmask;
}

State the assumptions that this solution relies on.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


在文章< Bt ************ @ read1.cgocable.net>中,Method Man< a@b.c>写道:
In article <Bt************@read1.cgocable.net>, Method Man <a@b.c> wrote:
问:
编写一个C / C ++程序,该程序采用无符号整数并加1,而不使用加号或减号。不要担心溢出问题。


(这实际上更容易处理无符号溢出,就像++

运算符一样,通过环绕而不是将其视为a不要

担心这个特殊情况。


这是我提出的解决方案。我想知道是否有更优雅或更有效的解决方案。

void addOne(unsigned int& i){
int j = 1;

while(i& 1){
i>> = 1;
j<< = 1;
}
= 1;

while!(j& 1){
i<< = 1;
j>> = 1;
}
}



我有一个只使用一个循环的解决方案:

--------

/ *读者练习:

-找出并纠正故意引入的bug

(也就是说,不要把它作为你的作业而不用想一想

首先,否则你会失败)

-解释这个bug有什么影响以及为什么

-重构我的评论解释代码在做什么以及为什么

* /

unsigned long inc(unsigned long i)

{

unsigned long j = 1;

while(j)

{

i ^ = j;

if(! (i& j))

返回i;

j<< = 1;

}

返回i;

}

--------

dave


-

Dave Vandervies dj ****** @ csclub.uwaterloo.ca 就像C一样,vi并不是无能为力。
Q:
Write a C/C++ program that takes an unsigned integer and adds 1 to it
without using the plus or minus signs. Don''t worry about overflow issues.
(It''s actually easier to treat unsigned overflow the same way the ++
operator does it, by wrapping around, than by treating it as a "Don''t
worry about this" special case.)

Here''s the solution I came up with. I''m wondering if there''s a solution that
is more elegant or efficient.
void addOne(unsigned int &i) {
int j = 1;

while (i & 1) {
i >>= 1;
j <<= 1;
}
i |= 1;

while !(j & 1) {
i <<= 1;
j >>= 1;
}
}

I have a solution that only uses one loop:
--------
/*Exercises for the reader:
-Find and correct the deliberately introduced bug
(that is, don''t hand this in as your homework without thinking about it
first, or you''ll fail)
-Explain what effect the bug has and why
-Reconstruct my comments explaining what the code is doing and why
*/
unsigned long inc(unsigned long i)
{
unsigned long j=1;
while(j)
{
i^=j;
if(!(i&j))
return i;
j<<=1;
}
return i;
}
--------
dave

--
Dave Vandervies dj******@csclub.uwaterloo.ca Just like C, vi is not incompetent-friendly.



我不同意。我和C和vi都很好。

--Dan Pop和Richard Heathfield在comp.lang.c


I disagree. I get on just fine with both C and vi.
--Dan Pop and Richard Heathfield in comp.lang.c


这篇关于拼图:不使用+ OR添加一个 - 的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆