算法基础与在线实践 递归4.6&4.7 源代码

4.6文件结构图

#include <bits/stdc++.h>
using namespace std;
int fileStructure(int level, string dir, int nCase)
{
    vector<string> v;
    for (int i = 1; i <= level; i++)
        cout << "|     ";
    if (level != 0)
        cout << dir << endl;
    int t = 0;
    while (true)
    {
        string s;
        cin >> s;
        if (s[0] == '#')
            return 0;
        if (t == 0 && level == 0)
        {
            cout << "DATA SET" << nCase << ":" << endl;
            cout << dir << endl;
            t = 1;
        }
        if (s[0] == '*')
            break;
        if (s[0] == 'f')
            v.push_back(s);
        if (s[0] == 'd')
            fileStructure(level + 1, s, nCase);
        if (s[0] == ']')
            break;
    }
    sort(v.begin(), v.end());
    vector<string>::iterator it = v.begin();
    for (; it != v.end();it++){
        for (int i = 1; i <= level;i++){
            cout << "|     ";
        }
        cout << *it << endl;
    }
    return 1;
}
int main()
{
    int nCase = 0;
    while(true){
        nCase++;
        int end = fileStructure(0, "ROOT", nCase);
        if(end==0)
            break;
        cout << endl;
    }
    system("pause");
}

输入输出    

file1
DATA SET1:
ROOT
file2
dir3
|     dir3
dir2
|     |     dir2
file1
file2
]
|     |     file1
|     |     file2
]
file4
dir1
|     dir1
]
file3
*
file1
file2
file3
file4

file2
DATA SET2:
ROOT
file1
*
file1
file2

#

4.7 算24

#include <bits/stdc++.h>
using namespace std;
int ans = 0;
void counting24(double a[], int n)
{
    int i, j, k, m;
    double b[4];
    if (n == 1 && a[0] > 0 && fabs(a[0] - 24) < 0.0001)
        ans = 1;
    else if (n > 1)
    {
        for (i = 0; i < n; i++)
        {
            for (j = 0; j < n; j++)
            {
                if (i == j)
                    continue;
                for (m = k = 0; k < n; k++)
                    if (k != i && k != j)
                    {
                        b[m] = a[k];
                        m++;
                    }
                b[m] = a[i] + a[j];
                counting24(b, m + 1);
                b[m] = a[i] - a[j];
                counting24(b, m + 1);
                b[m] = a[i] * a[j];
                counting24(b, m + 1);
                if (a[j] != 0)
                    b[m] = a[i] / a[j];
                counting24(b, m + 1);
            }
        }
    }
}
int main()
{
    double a[4];
    while (cin >> a[0] >> a[1] >> a[2] >> a[3] && ((a[0] + a[1] + a[2] + a[3]) != 0))
    {
        ans = 0;
        counting24(a, 4);
        if (ans)
            cout << "YES" << endl;
        else
            cout << "NO" << endl;
    }
    system("pause");
}

输入输出

5 5 5 1
YES
1 1 4 2
NO
0 0 0 0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值