zoukankan      html  css  js  c++  java
  • 第五周 c进阶 指针2

    编程题#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

     自己的代码,不知道为啥不成功

    //
    //  main.cpp
    //  kaka
    //
    //  Created by 吴铭英 on 2017/12/5.
    //  Copyright © 2017年 吴铭英. All rights reserved.
    
    #include <iostream>
    using namespace std;
    
    int main(){
    
        int k=0;
        cin>>k;
        while (k>0) {
            int m=0;
            int n=0;
            cin>>m;
            cin>>n;
            int t[100][100];
            int sum =0;
            for (int i = 0; i < m; i++)
            {
                for (int j = 0; j < n; j++)
                {
    
                    cin>>t[i][j];
                    if(i==0 || j==0 || i==n-1 || j==m-1)
                        sum +=t[i][j];
    
                }
            }
            cout<<sum<<endl;
            k--;
        }
        return 0;
    
    }
    

      网上的

    #include<iostream>
    #include<stdio.h>
    using namespace std;
    
    int main() {
        int k, l, t;
        cin >> k;
        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;
                    }
                }
            }
            cout << sum << endl;
            k--;
        }
        return 0;
    }
    

    编程题#2: 二维数组右上左下遍历

    来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩。)

    注意: 总时间限制: 1000ms 内存限制: 65536kB

    描述

    给定一个row行col列的整数数组array,要求从array[0][0]元素开始,按从左上到右下的对角线顺序遍历整个数组。

    输入

    输入的第一行上有两个整数,依次为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
     
     
     

    样例输出

    #include <iostream>
    using namespace std;
    
    int main() {
        int row = 0, col = 0;
        cin >> row >> col;
        int array[row][col];
        
        for (int m=0; m<row; m++) {
            for (int n=0; n<col; n++) {
                cin >> array[m][n];
            }
        }
        
        for (int p=0; p<row+col-1; p++) {
            for (int i=0; i<=p; i++) {//对i遍历,因为输出的顺序为00 ||01,10,||02,11,20...左边为0,1,2;
                if (i < row && p -i < col) {  //右边 对角线的编号p=0,1,2,减去i 小于列即可
                    cout << array[i][p-i] << endl;
                }
            }
        }
        
        return 0;
    
    
    }
    

      

      

    编程题#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>
    #include<string.h>
    using namespace std;
    
    int main()
    {
        int word = 0, sum = 0;
        cin >> word;
        char str[41]={0};
        
        for (int i = 0; i < word; 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;
    }
    

      

  • 相关阅读:
    jquery swiper自定义pagination的方法
    javascript获取地址栏参数的方法
    jquery trigger使用方法
    jquery on绑定事件叠加解决方法
    phpexcel无法导出的解决方法
    mysql left join和union结合的用法
    Linux项目一
    排序
    搜索
    递归
  • 原文地址:https://www.cnblogs.com/Gailsunset/p/7908208.html
Copyright © 2011-2022 走看看