20170330今日頭條筆試題
阿新 • • 發佈:2019-01-05
只有四個程式設計題
1
2
3
#include <iostream>
using namespace std;
int main()
{
string s;
cin>>s;
int len = s.length();
int a[100];
int count = len;
int level = 1;
for(int i = 0; i < len; i++)
{
if(s[i] == '[')
{
a[i] = level;
level++;
}
else
{
level--;
a[i] = level;
}
}
int max = a[0];
for(int i = 0; i < count; i++)
{
if(a[i] > max)
{
max = a[i];
}
}
for(int i = 0; i < count; i++)
{
a[i] = max - a[i] + 1;
}
for (int i = 0; i < count; i++)
{
cout<<a[i]<<endl;
}
for(int i = 0; i < count; i++)
{
if(a[i] == max)
{
cout<<"+";
for(int j = 0; j < max * 2 - 1; j++)
{
cout<<"-";
}
cout <<"+";
cout<<endl;
if(s[i] == '[' && s[i+1] == ']')
{
for(int j = 0; j < max-a[i]; j++)
{
cout<<" ";
}
cout<<"|";
for(int j = 0; j < a[i]*2 - 1; j++)
{
cout<<" ";
}
cout<<"|"<<endl;
for(int j = 0; j < max-a[i]; j++)
{
cout<<" ";
}
cout<<"|";
for(int j = 0; j < a[i]*2 - 1; j++)
{
cout<<" ";
}
cout<<"|"<<endl;
}
}
if(a[i] != max)
{
for(int j = 0; j < max-a[i]-1; j++)
{
cout<<" ";
}
cout<<"|+";
for(int j = 0; j < a[i] * 2 - 1; j++)
{
cout<<"-";
}
cout<<"+|";
cout<<endl;
if(s[i] == '[' && s[i+1] == ']')
{
for(int j = 0; j < max-a[i]; j++)
{
cout<<" ";
}
cout<<"|";
for(int j = 0; j < a[i]*2 - 1; j++)
{
cout<<" ";
}
cout<<"|"<<endl;
for(int j = 0; j < max-a[i]; j++)
{
cout<<" ";
}
cout<<"|";
for(int j = 0; j < a[i]*2 - 1; j++)
{
cout<<" ";
}
cout<<"|"<<endl;
}
}
}
return 0;
}
4