仅使用添加将数字增加到幂

时间:2017-09-23 10:19:02

标签: c# addition

我试图仅使用添加功能将数字提升为功率,但它不起作用,它只增加了比原始数字更大的数字。这是我的代码:

private void ExpOperation()
    {
        result = 0;
        num01 = Int32.Parse(inpu01.Text);
        num02 = Int32.Parse(inpu02.Text);
        int a = num02;
        num02 = num01;
        int i = 1;


        while (i <= a)
        {
            result = SimpleMulti(num01,num02);
            num01 = result;
            i++;
        }
        result_Text.Text = result.ToString();
    }
    private int SimpleMulti (int num1, int num2)
    {
        int c = 0;
        int i = 1;
        while (i <= num2)
        {
            c += num1;
            i++;
        }
        return c;
    }

3 个答案:

答案 0 :(得分:0)

# A tibble: 18 x 7
# Groups:   Loc [2]
      Loc   term comparison    estimate   conf.low conf.high adj.p.value
   <fctr> <fctr>      <chr>       <dbl>      <dbl>     <dbl>       <dbl>
 1   Loc1    Rep        2-1  1.06654704 -0.5666584 2.6997525   0.1920531
 2   Loc1    Rep        3-1  0.07349636 -1.5597091 1.7067018   0.9895627
 3   Loc1    Rep        3-2 -0.99305068 -2.6262561 0.6401548   0.2283849
 4   Loc1    Trt      T2-T1  0.66688371 -1.4607989 2.7945663   0.7105928
 5   Loc1    Trt      T3-T1 -0.34873829 -2.4764209 1.7789443   0.9382673
 6   Loc1    Trt      T4-T1  0.76089899 -1.3667836 2.8885816   0.6281933
 7   Loc1    Trt      T3-T2 -1.01562201 -3.1433046 1.1120606   0.4201776
 8   Loc1    Trt      T4-T2  0.09401528 -2.0336673 2.2216979   0.9985800
 9   Loc1    Trt      T4-T3  1.10963728 -1.0180453 3.2373199   0.3556331
10   Loc2    Rep        2-1 -0.59970808 -2.4360070 1.2365908   0.6023328
11   Loc2    Rep        3-1 -0.29558179 -2.1318807 1.5407171   0.8768041
12   Loc2    Rep        3-2  0.30412629 -1.5321726 2.1404252   0.8702266
13   Loc2    Trt      T2-T1 -1.06715766 -3.4594233 1.3251080   0.4703902
14   Loc2    Trt      T3-T1 -1.38659230 -3.7788579 1.0056733   0.2828393
15   Loc2    Trt      T4-T1 -1.23727832 -3.6295439 1.1549873   0.3616019
16   Loc2    Trt      T3-T2 -0.31943464 -2.7117003 2.0728310   0.9646736
17   Loc2    Trt      T4-T2 -0.17012066 -2.5623863 2.2221450   0.9942021
18   Loc2    Trt      T4-T3  0.14931398 -2.2429516 2.5415796   0.9960495

请记住,此方法不支持负指数,后者处理除法,但您可以为SimpleDivide创建一个使用减法的方法,而不是使用SimpleMulti。原理是一样的

答案 1 :(得分:0)

我不认为这个问题与这个网站的主要原因非常相关,但是我得到了一个解决方案:

public long ExpOperation(int a, int b)
{
    long result = 0;
    long temp = 0;
    for (int i = 1; i <= b; i++) // Executes a full loop when we have successfully multiplied the base number "a" by itself
    {
        for (int j = 1; j <= a; j++) // Increase the result by itself for a times to multiply the result by itself
            result += temp;
        temp = result:
    }
    return result;
}

答案 2 :(得分:0)

因为 x ^ y = x * x ^(y-1),所以可以递归地求解。由于有问题的SimpleMulti返回整数,我假设base和exponent都是非负整数。

private static int PowerWithAddition(int x, int y)
{
    if(y == 0){
        return 1;
    }

    var y1 = PowerWithAddition(x, y - 1);
    var sum = 0;
    for (int i = 0; i < y1; i++)
    {
        sum += x;
    }

    return sum;
}