24663人加入学习
(72人评价)
C#编程-第一季-编程基础-宇宙最简单2021最新版

制作完成于2021年10月10日,使用Visual Studio 2019

价格 免费
using System;

namespace _019_while循环_编程题
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = Convert.ToInt32(Console.ReadLine());
            int j = 0;
            while (n != 1)
            {
                if (n % 2 == 1)
                {
                    n = 3 * n + 1;
                }
                else
                {
                    n /= 2;
                }
                j++;
                Console.WriteLine("变换后的值为:" + n);
            }
            Console.WriteLine(" 变换的次数为:" + j);
        }
    }
}

 

[展开全文]

using System;

public class HelloWorld
{
    public static void Main(string[] args)

    {
        int n = Convert.ToInt32(Console.ReadLine());
        //int n = int.Parse(Console.ReadLine());
        int i = 0;
        int sum = 0;
        while (n > 1)
        {
            if (n % 2== 1)
            {
                n = 3 * n + 1;
                i++;
            }
            else 
                {
                    n = n / 2;
                i++;
            }
            sum = i + 1;
            
        }
        Console.WriteLine(i);
        Console.ReadKey();
    }
}

[展开全文]

编程题

3n+1

例:int n = Convert.ToInt32(Console.ReadLine());

int j = 0

while(n≠1)

{

    if (n % 2 == 1)

    {

          n = 3 * n + 1;

    }

    else

    {

          n  /= 2;

    }

    j++;

    //执行次数必须放在if语句外,否则每次进行运算时都会将j值归0重新计算

    Console.WriteLine("变换后的值为:" + n)

}

Console.WriteLine("变换后的次数为:" + j)

[展开全文]

int n = Convert.ToInt32(Console.ReadLine());
            int i = 0;
            while (n != 1) 
            {
                if (n % 2 == 1)
                    n = 3 * n + 1;
                else
                    n = n / 2;

                i++;
               
            }
            Console.WriteLine(i);

[展开全文]
static void test01()
        {
            Console.WriteLine("请输入整数n:");
            int n = Convert.ToInt32(Console.ReadLine());
            for (int i = 0; i >=0; i++)
            {
                if (n>1)
                {
                    break;
                }
                else
                {
                    Console.WriteLine("输入有误,请重新输入:");
                    n = Convert.ToInt32(Console.ReadLine());
                }
            }
            int j = 0;
            while (n>1)
            {
                if (n % 2 != 0)
                {
                    n = 3 * n + 1;
                }
                else if (n % 2 == 0)
                {
                    n /= 2;
                }
                
                j++;
                Console.WriteLine("每次变换后n=" + n);               
            }
            Console.WriteLine("变换次数j是:" + j);
        }

 

[展开全文]

统计次数,需要巩固学习

[展开全文]

第五十三课  编程题3n加1问题

1. 3n+1问题:

对于任意大于1的自然数n,若n为奇数,将n变成3n+1,否则变成n的一半,经过若干次这样的变化,n最终一定会变成1,。比如:7→22→11→34→17→52→26→13→40→20→10→5→16→8→4→2→1

输入n,输出变换的次数。

比如输入3,输出7;输入10,输出6。

int n =Convert.ToInt32(Console.ReadLine());
            int a = 0;
            while (n > 1)
            {
                if(n % 2 == 1)
                {
                    n = 3 * n + 1;
                    a++;
                }
                else
                {
                    n = n / 2;
                    a++;
                }                
            }
            Console.Write("最后值为{0}" + " " + "共用{1}步", n, a);

这道题还蛮好玩的,睡觉去了,睡晚了更掉头发,明天再听老师揭秘。

注:

计循环次数额a++写在while里if外啊,我还每个if里面写一个,真笨。

[展开全文]

授课教师

SiKi学院老师

课程特色

下载资料(1)
视频(118)
图文(1)

学员动态