题目大意:将给定串输出“U”形状,使得三边尽量接近。有三种情况:
- (N+2)%3 == 0 三边相等;
- (N+2)%3 == 1 多出来的一个给底边;
- (N+2)%3 == 2 多出来的两边给底边
解题思路:仔细点,控制输出即可。
题目链接:https://www.patest.cn/contests/pat-a-practise/1031
#include <iostream>
#include <cstring>
using namespace std;
int main(int argc, char** argv) {
char a[81];
scanf("%s",a);
int len = strlen(a);
int x = (len+2)/3;
int y = (len+2)%3+x;
for(int i=0;i<x-1;++i)
{
cout << a[i];
for(int j=0;j<y-2;++j)
cout << " ";
cout << a[len-1-i] << endl;
}
for(int i=x-1;i<x+y-1;++i)
cout << a[i];
cout << endl;
return 0;
}