【2022年蓝桥杯】蓝桥杯第一次海选考试题(5题考试大二)(C#题解)

2021/11/29 20:37:29

本文主要是介绍【2022年蓝桥杯】蓝桥杯第一次海选考试题(5题考试大二)(C#题解),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

请根据答题情况自己给出分数。

目录

1.字符串值交换(10分)【变量操作】

2.会员打折(20分)【分支结构】

3.输出九九乘法表(20分)【循环结构】

4.计算从1开始累加到2^64,测试数据最低10000000(一亿)的值,时间不得超过1s。(25分)【规律总结】

5.生兔子问题(25分)【逻辑基础】


1.字符串值交换(10分)【变量操作】

string x = Console.ReadLine();
string y = Console.ReadLine();
string z = x;
x = y;
y = z;
Console.WriteLine(x);
Console.WriteLine(y);

2.会员打折(20分)【分支结构】

int x = int.Parse(Console.ReadLine());
double y = double.Parse(Console.ReadLine());

if (x == 1 & y >= 0)
{
    if (y >= 500)
    {
        Console.WriteLine(y * 0.6);
    }
    else if (y >= 300)
    {
        Console.WriteLine(y * 0.65);
    }
    else if (y >= 200)
    {
        Console.WriteLine(y * 0.7);
    }
    else if (y >= 100)
    {
        Console.WriteLine(y * 0.8);
    }
    else
    {
        Console.WriteLine(y * 0.9);
    }
}
else if (x == 0 & y > 0)
{
    if (y >= 100)
    {
        Console.WriteLine(y * 0.9);
    }
    else
    {
        Console.WriteLine(y);
    }
}
else
{
    Console.WriteLine("x只允许输入1与0,y必须大于0");
}

3.输出九九乘法表(20分)【循环结构】

for (int i = 1; i < 10; i++)
{
    for (int j = 1; j <= i; j++)
    {
        Console.Write(j + "*" + i + "=" + (i * j) + "\t");
    }
    Console.WriteLine();
}

4.计算从1开始累加到2^64,测试数据最低10000000(一亿)的值,时间不得超过1s。(25分)【规律总结】

decimal x = decimal.Parse(Console.ReadLine());
Console.WriteLine((x + 1) * x / 2);

5.生兔子问题(25分)【逻辑基础】

有一对兔子,从出生后第四个月起每个月都生一对兔子,小兔子长到第四个月后每个月又生一对兔子。假如兔子都不死,计算第十个月兔子的总数?

解法1:

int month = int.Parse(Console.ReadLine());
int one = 1;
int two = 1;
int three = 0;
for (int i = 1; i < month; i++)
{
    three = two;
    two = one + two;
    one = three;
}
Console.WriteLine(three * 2);

解法2:

static int dfs(int i) {
    if (i==0) {
        return 0;
    }
    if (i == 1) {
        return 1;
    }
    return dfs(i - 1) + dfs(i - 2);
}
static void Main(string[] args)
{
    int i = dfs(10);
    Console.WriteLine(i*2);
}



这篇关于【2022年蓝桥杯】蓝桥杯第一次海选考试题(5题考试大二)(C#题解)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程