C#编程题分享(1)

求四个整数中的最大值和最小值问题

编写⼀个程序,对输⼊的4个整数,求出其中的最⼤值和最⼩值,并显⽰出来。

cs 复制代码
Console.WriteLine("请分别输入四个整数:");
int a = Convert.ToInt32(Console.ReadLine());
int b = Convert.ToInt32(Console.ReadLine());
int c = Convert.ToInt32(Console.ReadLine());
int d = Convert.ToInt32(Console.ReadLine());
int max = a, min = a;
if (max < b) max = b;
if (max < c) max = c;
if (max < d) max = d;
if (min > b) min = b;
if (min > c) min = c;
if (min > d) min = d;
Console.WriteLine("这四个整数的最大值是:{0},最小值是:{1}", max, min);

3n + 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

cs 复制代码
Console.WriteLine("请输入一个自然数:");
int n = Convert.ToInt32(Console.ReadLine());
int count = 0;
while (n != 1)
{
    if (n % 2 == 1) n = 3 * n + 1;
    else n = n / 2;
    count += 1;
    Console.WriteLine(n);
}
Console.WriteLine("一共经历了{0}次变化", count);

学员增长问题

2023年培养学员80000⼈,每年增⻓25 %,请问按此增⻓速度,到哪⼀年培训学员⼈数将达到20万⼈?

cs 复制代码
int number = 80000;
int year = 2023;
while (number < 200000)
{
    number += (int)((double)number * 0.25);
    year++;
}
Console.WriteLine("到{0}年人数达到20万", year);
相关推荐
中国胖子风清扬32 分钟前
Rust 序列化技术全解析:从基础到实战
开发语言·c++·spring boot·vscode·后端·中间件·rust
_oP_i44 分钟前
WinForms 项目里生成时选择“首选目标平台 32 位导致有些电脑在获取office word对象时获取不到
c#·office
bobz9651 小时前
分析 docker.service 和 docker.socket 这两个服务各自的作用
后端
要记得喝水1 小时前
C#某公司面试题(含题目和解析)--1
开发语言·windows·面试·c#·.net
野犬寒鸦1 小时前
力扣hot100:旋转图像(48)(详细图解以及核心思路剖析)
java·数据结构·后端·算法·leetcode
phiilo2 小时前
golang 设置进程退出时kill所有子进程
后端
花花无缺2 小时前
python自动化-pytest-用例发现规则和要求
后端·python
程序员小假2 小时前
我们来说一说 Cglib 与 JDK 动态代理
后端
冷冷的菜哥2 小时前
ASP.NET Core文件分片上传
c#·asp.net·asp.net core·文件分片上传