特定格式化输出题目
CCCC-GPLT L1-039. 古風排版 團體程序設計天梯賽
中國的古人寫文字,是從右向左豎向排版的。本題就請你編寫程序,把一段文字按古風排版。
輸入格式:
輸入在第一行給出一個正整數N(<100),是每一列的字符數。第二行給出一個長度不超過1000的非空字符串,以回車結束。
輸出格式:
按古風格式排版給定的字符串,每列N個字符(除了最后一列可能不足N個)
輸入樣例:
4
This is a test case
輸出樣例:
分析:先根據字符串的長度s.length()和n計算出col列數,然后建立一個n行col列的字符數組,一開始都賦值為空格,然后根據先最后一列再倒數第二列依次向前,行數從0~n-1的順序依次將s[index]的值填入vector中,直到s的所有字符都填完為止,最后輸出這個字符數組~
#include <iostream> #include <vector> using namespace std; int main() {int n;string s;cin >> n;getchar();getline(cin, s);int col = (s.length() - 1) / n + 1;vector<vector<char> > v(n, vector<char>(col, ' '));int index = 0;for (int j = col - 1; j >= 0; j--) {for (int i = 0; i < n; i++) {if (index < s.length())v[i][j] = s[index++];}}for (int i = 0; i < n; i++) {for (int j = 0; j < col; j++)cout << v[i][j];cout << endl;}return 0; }2017年4月“比特科技杯”C語言大賽
蛇形填數
樣例1:
5
1 3 6 10 15
2 5 9 14
4 8 13
7 12
11
樣例2:
71 3 6 10 15 21 28
2 5 9 14 20 27
4 8 13 19 26
7 12 18 25
11 17 24
16 23
22
#include<bits/stdc++.h> using namespace std; int c[105][105]; int main() {ios::sync_with_stdio(false);int n;while(cin>>n){fill(c[0],c[0]+105*105,0); int cnt=n*(n+1)/2;//cout<<cnt<<endl;int k=cnt;int t=n,in=n;for(int i=1;i<=n;i++){cnt=k--;in=n;for(int j=t;j>=1;j--){c[i][j]=cnt;cnt-=in;in--;}t--;}t=n;for(int i=1;i<=n;i++){for(int j=1;j<t;j++){cout<<c[i][j]<<" ";}cout<<c[i][t]<<endl;t--;}}return 0;}總結
- 上一篇: poj2002 STL set
- 下一篇: 十进制整数(包括负数)和二进制的转换