编程打卡:C语言趣味编程习题做

发布时间 2023-04-18 21:09:29作者: satou_matsuzaka

编程打卡:C语言趣味编程习题做

存钱问题

问题描述

给定不同期限档次整存整取的月利率,期限和本金,求出使利息最大的存款方案。

设计思路

遍历每种可能的存钱方案,求出利息最大的方案,然后输出。

流程图

graph A[开始] --> B[定义各种各样的变量] --> C[遍历所有存款方案,保存利率最大的情况] --> D[输出方案];

代码实现

class Program
{
static void Main(string[] args)
    {
        const double a = 2000;
        double max = 0,y1 = 0,y2 = 0,y3 = 0,y5 = 0,y8 = 0;
        for (int x8 = 0; x8 <= 2; x8++)
            for (int x5 = 0; x5 <= (20 - x8 * 8)/5; x5++)   
                for (int x3 = 0; x3 <= (20 - x8 * 8 - x5 * 5)/3; x3++)
                    for (int x2 = 0; x2 <= (20 - x8 * 8 - x5 * 5 - x3 * 3)/2; x2++)
                        for (int x1 = 0; x1 <= (20 - x8 * 8 - x5 * 5 - x3 * 3 - x2 * 2); x1++)  {
                            double interest = a * Math.Pow((1+0.0063 * 12),x1) * Math.Pow((1+0.0066 * 12),x2) * Math.Pow((1+0.0069 * 12),x3) * Math.Pow((1+0.0075 * 12),x5) * Math.Pow((1+0.0084 * 12),x8);
                            if (interest > max) {
                                y1 = x1;
                                y2 = x2;
                                y3 = x3;
                                y5 = x5;
                                y8 = x8;  
                            }
                        }
        Console.WriteLine($"8年期存 {y8} 次,5年期存 {y5} 次,3年期存 {y3} 次,2年期存 {y2} 次,一年期存 {y1} 次。");
    }
}

运行结果

8年期存 2 次,5年期存 0 次,3年期存 1 次,2年期存 0 次,一年期存 1 次。

分糖果

问题描述

给定初始每个人的糖块数,问多少次这样的操作之后每个人的糖块数相等。

设计思路

循环进行操作,满足所有人糖块数相等时结束循环,输出次数;

流程图

graph A[开始] --> B[用数组存储每个人的糖果数,初始化次数=0] --> C[进行分糖操作,次数++] --> D{每个人的糖果一样多?} --Yes--> E[输出次数]; D --No--> C;

代码实现

class Program
{
    static int[] candy = {10,2,8,22,16,4,10,6,14,20};
    static int[] tmp = new int[10];
    static bool check()    {
        int temp = candy[0];
        bool flag = true;
        foreach (int i in candy)    {
            Console.Write("{0} ",i);
            if (i != temp)  {
                flag = false;
            }
        }
        Console.WriteLine();
        return flag;
    }
    static void Main(string[] args)
    {
        int count = 0;
        while (!check())  {
            for (int i = 0; i < 10; i++)    {
                if (candy[i]%2 != 0) candy[i]++;
                candy[i]/=2;
                tmp[i] = candy[i];
            }
            for (int i = 0; i < 9; i++)     {
                candy[i+1] += tmp[i];
            }
            candy[0] += tmp[9];
            count++;
        }
        Console.WriteLine(count);
    }
}

运行结果

10 2 8 22 16 4 10 6 14 20 
15 6 5 15 19 10 7 8 10 17 
17 11 6 11 18 15 9 8 9 14 
16 15 9 9 15 17 13 9 9 12 
14 16 13 10 13 17 16 12 10 11
13 15 15 12 12 16 17 14 11 11
13 15 16 14 12 14 17 16 13 12
13 15 16 15 13 13 16 17 15 13
14 15 16 16 15 14 15 17 17 15
15 15 16 16 16 15 15 17 18 17
17 16 16 16 16 16 16 17 18 18
18 17 16 16 16 16 16 17 18 18
18 18 17 16 16 16 16 17 18 18 
18 18 18 17 16 16 16 17 18 18
18 18 18 18 17 16 16 17 18 18
18 18 18 18 18 17 16 17 18 18
18 18 18 18 18 18 17 17 18 18
18 18 18 18 18 18 18 18 18 18
17