如何从循环函数中获取这些值?

huangapple go评论61阅读模式
英文:

How can I get this values from a loop function?

问题

The output should be:

1 2 4 8 16
2 6 18 54 162
3 12 48 192 768
4 20 100 500 2500
5 30 180 1080 6480
6 42 294 2058 14406

英文:

The output should be:

1 2 4 8 16

2 6 18 54 162

3 12 48 192 768

4 20 100 500 2500

5 30 180 1080 6480

6 42 294 2058 14406

I am currently stuck with my code:

using System;

class Program
{
    static void Main(string[] args)
    {
        int size = 6; 
    
        for (int i = 1; i <= size; i++)
        {
            int num = i; 
        
            if(num == 1) {
              for (int j = 1; j < size; j++)
              {
                   Console.Write(num + " "); 

                   num *= 2; 
               }
          
               Console.WriteLine();
               }else{
               int num2 = 2;
        
               for (int j = 1; j < size; j++)
               {

               Console.Write(num2 + " "); 
              num2 *= 3;
          }       
      	   Console.WriteLine();
          }

       }
   }
}

答案1

得分: 2

基于代码和预期结果,公式应该类似于num *= i+1,所以你可以尝试只使用内部循环而没有任何if

int size = 6;

for (int i = 1; i <= size; i++)
{
    int num = i;
    for (int j = 1; j < size; j++)
    {
        Console.Write(num + " ");
        num *= i + 1;
    }
    Console.WriteLine();
}
英文:

Based on the code and the expected results formula should be something like num *= i+1, so you can try just inner loop without any if:

int size = 6;

for (int i = 1; i &lt;= size; i++)
{
    int num = i;
    for (int j = 1; j &lt; size; j++)
    {
        Console.Write(num + &quot; &quot;);
        num *= i + 1;
    }
    Console.WriteLine();
}

答案2

得分: 1

这个也可以,使用 Math.Pow

int size = 6;
for (int i = 1; i <= size; i++)
{
    for (int j = 0; j < 5; j++)
    {
        Console.Write(i * Math.Pow(i + 1, j));
        Console.Write(" ");
    }

    Console.WriteLine();
}
英文:

This one would work too, using Math.Pow

int size = 6;
for (int i = 1; i &lt;= size; i++)
{
	for (int j = 0; j &lt; 5; j++)
	{
		Console.Write(i * Math.Pow(i + 1, j));
		Console.Write(&quot; &quot;);
	}

	Console.WriteLine();
}

答案3

得分: 0

for循环修改为以下形式:

for (int i = 1; i <= size; i++)
{
    int num = i; 
    
    if (num == 1) {
        for (int j = 1; j < size; j++)
        {
            Console.Write(num + " "); 

            num *= 2; 
        }
        
        Console.WriteLine();
    } else {
        int num2 = i;
        
        for (int j = 1; j < size; j++)
        {
            Console.Write(num2 + " "); 
            num2 *= i + 1;
        }       
        Console.WriteLine();
    }
}
英文:

Change the for loop into this:

for (int i = 1; i &lt;= size; i++)
    {
        int num = i; 
    
        if(num == 1) {
          for (int j = 1; j &lt; size; j++)
          {
               Console.Write(num + &quot; &quot;); 

               num *= 2; 
           }
      
           Console.WriteLine();
           }else{
           int num2 = i;
    
           for (int j = 1; j &lt; size; j++)
           {

           Console.Write(num2 + &quot; &quot;); 
          num2 *= i+1;
      }       
       Console.WriteLine();
      }

   }

答案4

得分: 0

尝试这个。

英文:

Try this one.

public static void Main(string[] args)
    {
        int size = 6; 
    
        for (int i = 1; i &lt;= size; i++)
        {
            int num = i; 
        
            
            if(num == 1) {
              Console.WriteLine();
              for (int j = 1; j &lt; size; j++)
              {
                   Console.Write(num + &quot; &quot;); 

                   num *= 2; 
               }
          
               Console.WriteLine();
            }else{
               int num2 = i;
               for (int j = 1; j &lt; size; j++)
               {

                Console.Write(num2 + &quot; &quot;); 
                num2 *= i+1;
               }       
               Console.WriteLine();
          }

       }
    }

huangapple
  • 本文由 发表于 2023年5月22日 17:21:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76304691.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定