1. 程式人生 > >1031 Hello World for U (20 分)

1031 Hello World for U (20 分)

1031 Hello World for U (20 分)

題目意思就是給你一串字串,給出U型圖。
方法:求出字串長度 n , 然後得 m = n+2
          然後再得到 n1=n3=m/3,同時n2 = m - n1*2;
我寫的程式碼中求出的n2是比上面的n2少2.

#include <bits/stdc++.h> 
using namespace std;
int main() {
	//freopen("datain.txt","r",stdin); 
	string s;
cin>>s; int n = s.length(); //cout<<n<<endl; int n1 = (n+2)/3; int n2 = n-n1*2; //cout<<n1<<" "<<n2<<endl; string s1 = s.substr(0,n1);//n1字串 string s2 = s.substr(n1,n2);//n2字串 string s3 = s.substr(n1+n2);//n3字串 //cout<<s1<<" "<<s2<<" "<<s3<<endl;
for (int i = 0; i < n1; i++) { cout<<s1[i]; for (int j = 0; j < n2; j++) { if (i != n1-1) //中間空格 cout<<" "; else //最後一行 cout<<s2[j]; } cout<<s3[n1-i-1]<<endl; } return 0; }