iT邦幫忙

2025 iThome 鐵人賽

DAY 11
0
自我挑戰組

cpe30天練習系列 第 11

cpe練習day11

  • 分享至 

  • xImage
  •  

今天是練習cpe的Rotating Sentences題目
##程式碼

#include <iostream>
#include<string>
using namespace std;
string str[100];
 
int main()
{
	int c=0;
   	int r=0;
   	while(getline(cin , str[c]))
   	{
   		r=max(r, (int)str[c].length());
   		c++;
	}
	for(int i=0 ; i < r ; i++)
	{
		for(int j=c-1 ; j >= 0 ; j--)
		{
			if(i >= str[j].length())
			{
				cout <<" ";
			}
			else
			{
				cout << str[j][i];
			}
		}
		cout << endl;
	} 
	return 0;
}

##解題方向

  • c:計算目前輸入了多少行
  • r:紀錄輸入行中最長的字串長度
  • r=max(r, (int)str[c].length()); 用來更新「目前遇到的最長行」
  • 外層 for (int i = 0; i < r; i++)表示目前要輸出第幾個「直欄」
  • 內層 for (int j = c-1; j >= 0; j--)表示從最後一行往上輸出

上一篇
cpe練習day10
下一篇
cpe練習day12
系列文
cpe30天練習13
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言