沐沐Mnn 2019-04-28 11:55 采纳率: 72.7%
浏览 901
已采纳

C++编程填空:数组输出

描述

填写代码,创建Print模板类,要求对输入的字符串数组或整数数组,用模板类进行输出并自动换行

#include <iostream>
#include <string>
#include <numeric>
#include <algorithm>
using namespace std;
// 在此处补充你的代码
int main(){
    string s[20];
    int num[20];
    int m,n;

    while(cin >> m >> n){
        for(int i=0; i<m; i++){
            cin >> s[i];
        }
        accumulate(s, s+m, Print<string>(m));
        for(int i=0; i<n; i++){
            cin >> num[i];
        }
        accumulate(num, num+n, Print<int>(n));
    }
}

输入

有多个输入样例

每个样例的第一行为两个整数m,n(m,n不超过20)

每个样例的第二行为m个字符串

每个样例的第三行为n个整数

输出
对每个样例输出两行

第一行为输入的字符串(去除空格)

第二行为输入的整数(去除空格)

样例输入

3 3
abc def hij
12 34 56
2 5
Peking University
20 18 05 1 3

样例输出

abcdefhij
123456
PekingUniversity
2018513

http://cxsjsx.openjudge.cn/2018midexamcloze/4/

  • 写回答

1条回答 默认 最新

  • threenewbee 2019-04-28 12:11
    关注
    // Q759044.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    
    
    #include <iostream>
    #include <string>
    #include <numeric>
    #include <algorithm>
    using namespace std;
    // 在此处补充你的代码
    
    template <class T>
    class Print
    {
    public:
        Print(int n)
        {
        }
        void PrintIt(T val)
        {
            cout << val;
        }
    };
    
    template<class T>
    void accumulate(T * b, T * e, Print<T> p)
    {
        do 
        {
            p.PrintIt(*b);
        }
        while (++b != e);
        cout << endl;
    }
    
    int main(){
        string s[20];
        int num[20];
        int m,n;
    
        while(cin >> m >> n){
            for(int i=0; i<m; i++){
                cin >> s[i];
            }
            accumulate(s, s+m, Print<string>(m));
            for(int i=0; i<n; i++){
                cin >> num[i];
            }
            accumulate(num, num+n, Print<int>(n));
        }
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?