Problem B: Array Class (II)

Source: Internet
Author: User
Tags array length

Problem B: Array class (II) time limit:1 Sec Memory limit:128 MB
submit:3606 solved:1760
[Submit] [Status] [Web Board] The description encapsulates a template array class for storing array and processing related functions, and supports the following operations: 1. Array::array (int L) Construction method: Creates a group object of length L. 2. Array::size () Method: Returns the number of elements in an Array object. 3. Array::p ut (int n) method: Output The first n large elements in order from large to small, and output all elements from large to small if the array length is less than N. 4. Subscript operator: Returns the element to which the subscript refers. -----------------------------------------------------------------------------you design a template array class array so that the main () function works correctly. The function call format is shown in append.cc. The main () function has been given in append.cc.

The first integer n of input inputs, indicating that there are n sets of test data. Each subsequent line begins with an integer k, indicating that there are k elements of the same type followed. There are three types of array elements: integers, floating-point numbers, and characters, and appear in fixed sequential intervals.

Output the input array, by value from large to small outputs the first 10 elements, if the input less than 10 output. Each row of data corresponds to one output. The format is shown in Sample.

Sample Input
310 1 2 3 4 5 6 7 8 9 1.1 2.2 3.3 4.4 5.520 Abcdeghijmnprstuvwxy

  

Sample Output
9 8 7 6 5 4 3 2 1 05.5 4.4 3.3 2.2 1.1Y X W V U T S R P N

  

HINT Append codeappend.cc,
int main () {    int cases, Len;    cin >> cases;    for (int CA = 1; CA <= cases; ca++)    {        cin >> len;        if (ca% 3 = = 0)        {            array<char> chr_arr (len);            for (int i = 0; i < chr_arr.size (); i++)                cin >> Chr_arr[i];            Chr_arr.put (ten);        }        if (ca% 3 = = 1)        {            array<int> int_arr (len);            for (int i = 0; i < int_arr.size (); i++)                cin >> Int_arr[i];            Int_arr.put (ten);        }        if (ca% 3 = = 2)        {            array<double> dbl_arr (len);            for (int i = 0; i < dbl_arr.size (); i++)                cin >> Dbl_arr[i];            Dbl_arr.put (Ten);}}}    

  

#include <iostream> #include <algorithm>using namespace std;template <typename t>class array{public:    T *num;    int Len;    Array (int L): Len (l) {num=new T[len];}    int size () {return len;        } void put (int n) {sort (num, num+len);            if (N<=len) for (int i=len-1; i>=len-n; i--) {if (i==len-1) cout<<num[i];        else cout<< "" <<num[i];            } else for (int i=len-1; i>=0; i--) {if (i==len-1) cout<<num[i];        else cout<< "" <<num[i];    } cout<<endl;    } t& operator[] (int n) {return num[n];    }};int Main () {int cases, Len;    CIN >> cases;        for (int CA = 1; CA <= cases; ca++) {cin >> len;            if (ca% 3 = = 0) {array<char> chr_arr (len);    for (int i = 0; i < chr_arr.size (); i++)            CIN >> Chr_arr[i];        Chr_arr.put (10);            } if (ca% 3 = = 1) {array<int> int_arr (len);            for (int i = 0; i < int_arr.size (); i++) cin >> Int_arr[i];        Int_arr.put (10);            } if (ca% 3 = = 2) {array<double> dbl_arr (len);            for (int i = 0; i < dbl_arr.size (); i++) cin >> Dbl_arr[i];        Dbl_arr.put (10); }    }}

  

Problem B: Array Class (II)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.