洛谷C#顺序结构反思2

您当前在篇2

篇1链接:洛谷C#顺序结构反思-CSDN博客

P5703 【深基2.例5】苹果采购

基本就是A+B的变式,只是变成了乘号,做之前留意了一下本题的数据范围,没坑,int够用

// See https://aka.ms/new-console-template for more information
using System;
namespace ConsoleApp1
{
    class Program
    {
        static void Main(String[] args)
        {
            {
                string[] input = Console.ReadLine().Split(' ');
                int a = int.Parse(input[0]), b = int.Parse(input[1]);
                int c = a * b;
                Console.WriteLine(c);
            }
        }
    }
}

P5704 【深基2.例6】字母转换

很显然,这道题需要使用到有关字母大小写转换的函数

我把AI生成的示例代码搬过来(AI的版权极具争议,但愿它不会找我麻烦)

string original = "Hello World!";
string upperCase = original.ToUpper(); // 转换为大写
string lowerCase = original.ToLower(); // 转换为小写
 
Console.WriteLine(upperCase); // 输出: HELLO WORLD!
Console.WriteLine(lowerCase); // 输出: hello world!

很显然,这里的ToUpper()和ToLower()函数都是用于string的,这点值得注意

那难道char类型真的没有这个函数吗?

有的,本题代码如下:

using System;
namespace ConsoleApp1
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string input = Console.ReadLine();
            char ch = input[0];
            Console.WriteLine(char.ToUpper(ch));
        }
    }
}

不过这两个函数的格式有点不同,写代码时注意一下即可

P5705 【深基2.例7】数字反转

C#的字符串似乎不能直接反转,这时需要将字符串转换成字符数组,反转,再转回字符串

转回字符串的操作有点指针那味了,值得留意一下

using System;
namespace ConsoleApp1
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string input = Console.ReadLine();
            char [] chars = input.ToCharArray();
            Array.Reverse(chars);
            string output = new string(chars);
            Console.WriteLine(output);
        }
    }
}

P5706 【深基2.例8】再分肥宅水

题目本身不难,考察的是舍入和位域的操作

题目要求保留三位小数,具体看代码操作

有关舍入:

在网络获取资料的过程中,不知为何有人说Round()函数是五舍六入,也有人说是银行家舍入(四舍六入五凑偶),与题目要求的四舍五入不同,但是具体上机实践发现依然是四舍五入。其他的两种舍入方式应该可以通过调整参数规定。

有关位域:

为什么一定要写位域?理由如下

如果不使用位域,

1.0033在保留三位后是1.00,

输出为1

但我们期望的输出是1.00。因此,我们需要用到位域。

using System;
namespace ConsoleApp1
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string [] input = Console.ReadLine().Split(" ");
            float v = float.Parse(input[0]);
            int n = int.Parse(input[1]);
            Console.WriteLine($"{Math.Round(v / n, 3):F3}");
            Console.WriteLine(n * 2);
        }
    }
}
using System;
namespace ConsoleApp1
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string [] input = Console.ReadLine().Split(" ");
            double a = double.Parse(input[0]), b = double.Parse(input[1]), c = double.Parse(input[2]);
            double p = (a + b + c) / 2;
            double S = Math.Sqrt(p * (p - a) * (p - b) * (p - c));
            Console.WriteLine($"{Math.Round(S, 1):F1}");
        }
    }
}

如果觉得有用,请点赞支持一下吧!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值