简体   繁体   English

如何使用 C Sharp 上的指数返回 1、1+1、1+1+1?

[英]How do you return 1, 1+1, 1+1+1, using exponents on C Sharp?

I am a rookie using C# trying to figure out how to run a for loop to output 1 on the first iteration, then output 1+1 on the second iteration, 1+1+1 on the third, etc. To solve this, I first put an if statement for the value i = 1 so that it outputs 1 using the equation (i-1) 10^i-1 * 1 then for i>1 I am writing i = 10^(i-1) + 1. However, while I am running the program, it is telling me I have unassigned local variables and that there is no parameter that corresponds to y of Math Pow (double, double).我是一个使用 C# 的菜鸟,试图弄清楚如何在第一次迭代中运行 for 循环到 output 1,然后在第二次迭代中运行 output 1+1,在第三次迭代中运行 1+1+1,等等。为了解决这个问题,我首先为值 i = 1 放置一个 if 语句,以便它使用等式 (i-1) 10^i-1 * 1 输出 1 然后对于 i>1 我正在写 i = 10^(i-1) + 1 . 但是,当我运行程序时,它告诉我我有未分配的局部变量,并且没有与 Math Pow (double, double) 的 y 对应的参数。 So far I am using Math.Pow to create exponent in C sharp unless there is another easier way of doing so.到目前为止,我正在使用 Math.Pow 在 C sharp 中创建指数,除非有另一种更简单的方法。 Not sure what that means?不确定那是什么意思? Do you have any suggestions as to how to fix this please?您对如何解决这个问题有什么建议吗?

Thank you so much!太感谢了!

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

/* Equation that helps 
 * i = (n-1)*(10^i-1) + 1 -> works only for i = 1
 * for i>1 i = (10^i-1) + 1 
 */
namespace PL1Week6Part1SumofSeries1_11_111_1111CSharp
{
    internal class Program
    {
        static void Main(string[] args)
        {
        int i, sum;
        double d = Math.Pow(i - 1); 
        sum = 0;
        for (i=1; i >= 1; i++)
            {
                if (i == 1)
                {
                    i = ((i - 1) * 10^(i-1) + 1);
                    Console.Write("{0} +", i);
                    Console.ReadLine();
                }
                else
                {
                    sum = i + sum;
                    i = 10 ^ (i - 1) + 1;
                    Console.Write("{0} +", i);
                    Console.ReadLine();
                }
            }
        }
    }
}

I assume that you want to create a series of 1 s like 1, 11, 111, ... and not add the 1 s.我假设您想创建一系列1 s,例如 1、11、111,...而不是添加1 s。 Then you can do this by adding up powers of ten.然后你可以通过将 10 的幂相加来做到这一点。 You don't need a power function for this.为此,您不需要电源 function。 Just start with 1 and then multiply this value by 10 at every loop iteration.只需从 1 开始,然后在每次循环迭代时将该值乘以 10。 I will use long , as it has it allows up to 19 digits.我将使用long ,因为它最多允许 19 位数字。

int numberOfDigits = 4; // As an example. Works up to 19.
long sum = 0;
long digit = 1;
for (int i = 0; i < numberOfDigits; i++) {
    sum += digit;
    digit *= 10;
}
Console.WriteLine(sum);

A completely different solution creates the 1's series by first calculating a power of 10, eg 10 4 = 10000, then subtracts 1 to get 9999 and finally divides by 9 to get 1111.一个完全不同的解决方案通过首先计算 10 的幂来创建 1 的级数,例如 10 4 = 10000,然后减去 1 得到 9999,最后除以 9 得到 1111。

int numberOfDigits = 4; // Works up to 18.
Console.WriteLine(((long)Math.Pow(10, numberOfDigits) - 1) / 9);

Both examples print 1111 .这两个示例都打印1111

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM