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