如何仅使用添加和赋值执行乘法?

时间:2022-04-24 11:49:55

How do I change the following program, so that it performs the same Task, but using only additions and assignments?

如何更改以下程序,以便它执行相同的任务,但仅使用添加和分配?

I can only do max 27 additions, and the Output has to be generated in a single Input. Loops and other control flow operations are not allowed

我只能添加最多27个,并且必须在单个输入中生成输出。不允许循环和其他控制流操作

#include <iostream>

int main()
{
    int a;
    std::cout << "Enter number: ";
    std::cin >> a;

    std::cout << a*29 << std::endl;

    return 0;
}

9 个解决方案

#1


7  

Another approach that requires 7 +:

另一种需要7 +的方法:

int M1  = a;        // 1a
int M2  = M1 + M1;  // 2a
int M4  = M2 + M2;  // 4a
int M8  = M4 + M4;  // 8a
int M16 = M8 + M8;  // 16a
int res = M16 + M8 + M4 + M1; // 29a

The result is constructed from the binary pattern of the multiplier, i.e. 29 decimal is 0001.1101 binary. So we need to add M16, M8, M4 and M1 (and exclude M2).

结果由乘法器的二进制模式构成,即29十进制是0001.1101二进制。所以我们需要添加M16,M8,M4和M1(并排除M2)。

#2


6  

This is not general, and it cannot be, but to multiply just by 29 you can do this:

这不是一般的,它不可能,但只需乘以29就可以做到这一点:

// x is input
int t = x;  // x*1
x = x + x;
x = x + x;
t = t + x;  // x*5
x = x + x;
t = t + x;  // x*13
x = x + x;
t = t + x;  // x*29

This is just unrolled binary multiplication, like the similar answers, but without naming the temporary results. The additions to t correspond to the set bits in 29.

这只是展开的二进制乘法,就像类似的答案一样,但没有命名临时结果。对t的加法对应于29中的设置位。

#3


2  

This way you can do it with 21 additions.

这样你就可以增加21个。

#include <iostream>

int main()
{
    int a;
    std::cout << "Enter number: ";
    std::cin >> a;
    int b = (a+a+...+a) // 10 times
    b = b + b;
    b = b + (a + a + a ... + a ); // 9 times
    std::cout << b << std::endl;

    return 0;
}

#4


2  

Can you use an additional var?

你可以使用额外的var吗?

b = a + a + a//3 times a 
b = b + b + b//9 times a
b = b + b + b//27 times a
b = b + a + a//29 times a

#5


1  

Multiplication is just a repeated addition. Use a loop for this. Do you know how many times your loop should execute? Yes! So use a for loop.

乘法只是重复添加。为此使用循环。你知道你的循环应该执行多少次?是!所以使用for循环。

So here is the plan:

所以这是计划:

  1. Get input
  2. Use a for loop to perform the addition
  3. 使用for循环执行添加

  4. Output sum

Example:

// get input
int input;
std::cout << "Enter number: ";
std::cin >> input;

// use a for loop to perform the addition
int sum = 0;
for(int i = 0; i< 29; ++i)
    sum += a;

// output result
std::cout << sum << std::endl;

#6


0  

,resRepetitive addition is multiplication -- mathematically

,resRepetitive加法是乘法 - 数学上

So repeat addition of a for n number of times. A simple for/while loop can do this

因此,重复添加一次n次。一个简单的for / while循环可以做到这一点

#include <iostream>

int main()
{
    int a,i,temp=0;
    std::cout << "Enter number: ";
    std::cin >> a;
    for(i=1;i<=29;i++)
        temp+=a;//add a repeatedly for 'n' number of times. Here 29 as per your requirement. a*n  
    std::cout << temp <<std::endl; 
    return 0;
}

Without loop

int b=0,result=0;
b=a+a+a+a+a+a+a;\\adding for 7 times
result=b+b+b+b+a;\\4 times 7 (of a)= 28+a= 29 times (of a).

This involves 10 addition.

这涉及10次加法。

#7


0  

You can try this -

你可以尝试这个 -

b = a + a + a + a + a + a + a + a + a + a + a + a + a + a + a; // equivalent to a*15

b += b -a;

#8


0  

If you are only allowed to use + and =, and are not allowed to cheat by introducing other variables, there is no way of doing this unless you indulge in a bit of undefined behaviour and allow yourself something of the form

如果你只允许使用+和=,并且不允许通过引入其他变量来作弊,除非你沉迷于一些未定义的行为并允许自己形成某种形式,否则无法做到这一点

... + (a += ...) + ...

where ... is any number of a +. The trick here is that (a += ...) changes the value of a part way through evaluation of the expression.

其中......是任何数量的+。这里的诀窍是(a + = ...)通过评估表达式来改变部分方式的值。

Tempting as it may be, alas this is not portable C++ since += and + are not sequencing points. On your classroom compiler, you might be able to get this to work.

虽然很可能很诱人,但这不是可移植的C ++,因为+ =和+不是排序点。在您的课堂编译器上,您可能能够使其工作。

See https://ideone.com/fC3fai, you can see that

请参阅https://ideone.com/fC3fai,您可以看到

(a + a + a + a + a + (a += a + a + a) + a + a + a + a + a)

does the job on that particular compiler (at the time of writing). (For what it's worth, it also does the correct thing in Java where the expression is well-defined).

完成特定编译器的工作(在撰写本文时)。 (对于它的价值,它也可以在Java中使用表达式定义正确的东西)。

#9


-1  

You can add value multiple times:

您可以多次添加值:

int sum=0;
for(int i=0; i<multipler; i++){
    sum+=a; //adds value of a to itself
}
return sum;

If you want to multiple only by 2 you can just shift left:

如果你想只乘2乘以你可以向左移:

a = a << 1;

#1


7  

Another approach that requires 7 +:

另一种需要7 +的方法:

int M1  = a;        // 1a
int M2  = M1 + M1;  // 2a
int M4  = M2 + M2;  // 4a
int M8  = M4 + M4;  // 8a
int M16 = M8 + M8;  // 16a
int res = M16 + M8 + M4 + M1; // 29a

The result is constructed from the binary pattern of the multiplier, i.e. 29 decimal is 0001.1101 binary. So we need to add M16, M8, M4 and M1 (and exclude M2).

结果由乘法器的二进制模式构成,即29十进制是0001.1101二进制。所以我们需要添加M16,M8,M4和M1(并排除M2)。

#2


6  

This is not general, and it cannot be, but to multiply just by 29 you can do this:

这不是一般的,它不可能,但只需乘以29就可以做到这一点:

// x is input
int t = x;  // x*1
x = x + x;
x = x + x;
t = t + x;  // x*5
x = x + x;
t = t + x;  // x*13
x = x + x;
t = t + x;  // x*29

This is just unrolled binary multiplication, like the similar answers, but without naming the temporary results. The additions to t correspond to the set bits in 29.

这只是展开的二进制乘法,就像类似的答案一样,但没有命名临时结果。对t的加法对应于29中的设置位。

#3


2  

This way you can do it with 21 additions.

这样你就可以增加21个。

#include <iostream>

int main()
{
    int a;
    std::cout << "Enter number: ";
    std::cin >> a;
    int b = (a+a+...+a) // 10 times
    b = b + b;
    b = b + (a + a + a ... + a ); // 9 times
    std::cout << b << std::endl;

    return 0;
}

#4


2  

Can you use an additional var?

你可以使用额外的var吗?

b = a + a + a//3 times a 
b = b + b + b//9 times a
b = b + b + b//27 times a
b = b + a + a//29 times a

#5


1  

Multiplication is just a repeated addition. Use a loop for this. Do you know how many times your loop should execute? Yes! So use a for loop.

乘法只是重复添加。为此使用循环。你知道你的循环应该执行多少次?是!所以使用for循环。

So here is the plan:

所以这是计划:

  1. Get input
  2. Use a for loop to perform the addition
  3. 使用for循环执行添加

  4. Output sum

Example:

// get input
int input;
std::cout << "Enter number: ";
std::cin >> input;

// use a for loop to perform the addition
int sum = 0;
for(int i = 0; i< 29; ++i)
    sum += a;

// output result
std::cout << sum << std::endl;

#6


0  

,resRepetitive addition is multiplication -- mathematically

,resRepetitive加法是乘法 - 数学上

So repeat addition of a for n number of times. A simple for/while loop can do this

因此,重复添加一次n次。一个简单的for / while循环可以做到这一点

#include <iostream>

int main()
{
    int a,i,temp=0;
    std::cout << "Enter number: ";
    std::cin >> a;
    for(i=1;i<=29;i++)
        temp+=a;//add a repeatedly for 'n' number of times. Here 29 as per your requirement. a*n  
    std::cout << temp <<std::endl; 
    return 0;
}

Without loop

int b=0,result=0;
b=a+a+a+a+a+a+a;\\adding for 7 times
result=b+b+b+b+a;\\4 times 7 (of a)= 28+a= 29 times (of a).

This involves 10 addition.

这涉及10次加法。

#7


0  

You can try this -

你可以尝试这个 -

b = a + a + a + a + a + a + a + a + a + a + a + a + a + a + a; // equivalent to a*15

b += b -a;

#8


0  

If you are only allowed to use + and =, and are not allowed to cheat by introducing other variables, there is no way of doing this unless you indulge in a bit of undefined behaviour and allow yourself something of the form

如果你只允许使用+和=,并且不允许通过引入其他变量来作弊,除非你沉迷于一些未定义的行为并允许自己形成某种形式,否则无法做到这一点

... + (a += ...) + ...

where ... is any number of a +. The trick here is that (a += ...) changes the value of a part way through evaluation of the expression.

其中......是任何数量的+。这里的诀窍是(a + = ...)通过评估表达式来改变部分方式的值。

Tempting as it may be, alas this is not portable C++ since += and + are not sequencing points. On your classroom compiler, you might be able to get this to work.

虽然很可能很诱人,但这不是可移植的C ++,因为+ =和+不是排序点。在您的课堂编译器上,您可能能够使其工作。

See https://ideone.com/fC3fai, you can see that

请参阅https://ideone.com/fC3fai,您可以看到

(a + a + a + a + a + (a += a + a + a) + a + a + a + a + a)

does the job on that particular compiler (at the time of writing). (For what it's worth, it also does the correct thing in Java where the expression is well-defined).

完成特定编译器的工作(在撰写本文时)。 (对于它的价值,它也可以在Java中使用表达式定义正确的东西)。

#9


-1  

You can add value multiple times:

您可以多次添加值:

int sum=0;
for(int i=0; i<multipler; i++){
    sum+=a; //adds value of a to itself
}
return sum;

If you want to multiple only by 2 you can just shift left:

如果你想只乘2乘以你可以向左移:

a = a << 1;