編程11月最後一週周結

編程題#1:計算矩陣邊緣元素之和
來源: POJ (Coursera聲明:在POJ上完成的習題將不會計入Coursera的最後成績。)

注意: 總時間限制: 1000ms 內存限制: 65536kB

描述
輸入一個整數矩陣,計算位於矩陣邊緣的元素之和。所謂矩陣邊緣的元素,就是第一行和最後一行的元素以及第一列和最後一列的元素。

輸入
第一行爲整數k,表示有k組數據。

每組數據有多行組成,表示一個矩陣:

第一行分別爲矩陣的行數m和列數n(m < 100,n < 100),兩者之間以空格分隔。

接下來輸入的m行數據中,每行包含n個整數,整數之間以空格作爲間隔。

輸出

輸出對應矩陣的邊緣元素和,一個一行。

樣例輸入

2
4 4
1 1 1 1
0 0 0 0
1 0 1 0
0 0 0 0
3 3
3 4 1
3 7 1
2 0 1
樣例輸出

5
15
編程題#2: 二維數組右上左下遍歷
來源: POJ (Coursera聲明:在POJ上完成的習題將不會計入Coursera的最後成績。)

注意: 總時間限制: 1000ms 內存限制: 65536kB

描述
給定一個row行col列的整數數組array,要求從array[0][0]元素開始,按從左上到右下的對角線順序遍歷整個數組。

![在這裏插入圖片描述](https://img-blog.csdnimg.cn/20181129154414409.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NjaHVmZmVs,size_16,color_FFFFFF,t_70)
輸入

輸入的第一行上有兩個整數,依次爲row和col。

餘下有row行,每行包含col個整數,構成一個二維整數數組。

(注:輸入的row和col保證0 < row < 100, 0 < col < 100)

輸出
按遍歷順序輸出每個整數。每個整數佔一行。

樣例輸入

3 4
1 2 4 7
3 5 8 10
6 9 11 12
樣例輸出

編程題#3:文字排版

來源: POJ (Coursera聲明:在POJ上完成的習題將不會計入Coursera的最後成績。)

注意: 總時間限制: 1000ms 內存限制: 65536kB

描述
給一段英文短文,單詞之間以空格分隔(每個單詞包括其前後緊鄰的標點符號)。請將短文重新排版,要求如下:

每行不超過80個字符;每個單詞居於同一行上;在同一行的單詞之間以一個空格分隔;行首和行尾都沒有空格。

輸入
第一行是一個整數n,表示英文短文中單詞的數目. 其後是n個以空格分隔的英文單詞(單詞包括其前後緊鄰的標點符號,且每個單詞長度都不大於40個字母)。

輸出
排版後的多行文本,每行文本字符數最多80個字符,單詞之間以一個空格分隔,每行文本首尾都沒有空格。

樣例輸入
84
One sweltering day, I was scooping ice cream into cones and told my four children they could "buy" a cone from me for a hug. Almost immediately, the kids lined up to make their purchases. The three youngest each gave me a quick hug, grabbed their cones and raced back outside. But when my teenage son at the end of the line finally got his turn to "buy" his ice cream, he gave me two hugs. "Keep the changes," he said with a smile.
樣例輸出
One sweltering day, I was scooping ice cream into cones and told my four
children they could "buy" a cone from me for a hug. Almost immediately, the kids
lined up to make their purchases. The three youngest each gave me a quick hug,
grabbed their cones and raced back outside. But when my teenage son at the end
of the line finally got his turn to "buy" his ice cream, he gave me two hugs.
"Keep the changes," he said with a smile.

 

第一題

#include<iostream>
using namespace std;
int main() {
    int k, t;
    cin >> k;
    int kk = k;
    int sumo[1000] = {0};
    int p = 0;
    while (k > 0) {
        int m, n, i, j;
        int sum = 0;
        cin >> m >> n;
        for (i=0; i < m; i++)
        {
            for (j=0; j < n; j++)
            {
                cin >> t;
                if (i==0||i==m-1||j==0||j==n-1) {
                    sum += t;
                }
            }
        }
        sumo[p] =sum;
        ++p;
        k--;
    }
    for(int i = 0;i < kk; ++i)
        cout << sumo[i] <<endl;
    return 0;
}

第二題


法一
#include<iostream>
using namespace std;
int main() {
	int row, col;
	cin >> row >> col;
	int a[100][100] = { 0 };
	for (int i = 0; i < row; ++i) {
		for (int j = 0; j < col; ++j) {
			cin >> a[i][j];
		}
	}
	for (int z = 0; z < row + col - 1; ++z) {
		for (int i = 0; i < row; ++i) {
			int j = z - i;
			if (j < 0 )
				break;
            if(j > col -1)
                continue;
			cout << a[i][j] << endl;
		}
	}
	return 0;
}

法二  參考於	https://blog.csdn.net/mayuan2017/article/details/78168350
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
    int A[100][100];
    int row, col, i, j, r=0, c=0;
    cin >> row >> col;
    for (i=0; i<row; i++)
        for (j=0; j<col; j++)
            cin >> A[i][j];
    // 首先以第0行,列遞增爲起點遍歷col次
    for (j=0; j<col; j++){
        r = 0;
        c = j;
        while (r<row && c>=0)
            cout << A[r++][c--] << endl;
    }
    // 然後以第col-1列,行遞增爲起點遍歷row-1次
    for (i=1; i<row; i++){
        r = i;
        c = col-1;
        while (r<row && c>=0)
            cout << A[r++][c--] << endl;
    }
    return 0;
}

 

第三題


法一
#include <iostream>
#include<string.h>
using namespace std;

int main()
{
	int words = 0, sum = 0;
	cin >> words;
	char str[41] = { 0 };
	for (int i = 0; i < words; i++)
	{
		cin >> str;
		if (sum + 1 + strlen(str) > 80)
		{
			cout << endl;
			sum = 0;
		}
		else if (i>0)
		{
			cout << " ";
			sum++;
		}
		cout << str;
		sum += strlen(str);
	}
	return 0;
}
法二
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
	char words[1000][40]; //用於保存每一個單詞
	int wordsLen[1000]; // 記錄每一個單詞的長度
	int n; // 需要處理的單詞總數
	cin >> n;
	cin.get();//cin之後要用cin.get()讀取換行符,一開始沒注意到錯誤在這裏,還是基礎不牢靠
	for (int i = 0; i < n;i++) //輸入單詞數據,處理後得到每個單詞的長度
	{
		char temp;
		for (int j = 0; j < 40;j++) {
			cin.get(temp); 				
				if (temp != ' '&&temp != '\n') {
					words[i][j] = temp;
					
				}
				else
				{
					wordsLen[i] = j;
					break;
				}
			}
		
	}
	//先輸出第一個單詞
	int length = wordsLen[0];
	for (int j = 0; j < wordsLen[0]; j++)
		cout << words[0][j];
	for (int i = 1; i < n; i++)
	{
		//如果該單詞,連同前面的一個空格加入後不換行,則輸出空格和該單詞
		if (length + 1 + wordsLen[i] <= 80)
		{
			length = length + 1 + wordsLen[i];
			cout << ' ';
			for (int j = 0; j < wordsLen[i]; j++)
				cout << words[i][j];
		}
		else//如果該單詞,連同前面的一個空格加入後換行,則輸出回車和該單詞,另外重置本行現有長度爲單詞長度
		{
			cout << endl;
			length = wordsLen[i];
			for (int j = 0; j < wordsLen[i]; j++)
				cout << words[i][j];
		}
	}
	return 0;
}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章