首页 > 其他分享 >Problem A: 整型数组运算符重载

Problem A: 整型数组运算符重载

时间:2023-05-29 14:05:22浏览次数:45  
标签:mems int 运算符 length 整型 数组 Problem Array


Home

Web Board

ProblemSet

Standing

Status

Statistics


Problem A: 整型数组运算符重载


Time Limit: 1 Sec   Memory Limit: 128 MB

Submit: 1458  

Solved: 954

[Submit][Status][Web Board]


Description



定义Array类:

1.拥有数据成员int length和int *mems,分别是数组中元素的个数和元素列表。

2. 无参构造函数,将mems设置为NULL,length为0。

3. 重载==运算符,用于判断两个Array对象是否相等。相等包括两种情况:(1)两个对象是同一个对象,即它们拥有相同的地址(记住: this指针指向当前对象,是当前对象的地址);(2)两个对象的length相同,且mems中对应元素的值相同。其他情况均为不相等。

4. 利用友元函数重载<<和>>运算符。输入、输出格式见下。



Input



输入分多行。

第一行是一个正整数M,表示有M个数组。

每个数组是一行,其中第一个非负整数N表示该数组的元素个数,之后有N个整数。



Output



输出有M行。

第一行输出即为第一个数组。

自第二行开始,首先输出对应的数组元素(两两之间用空格隔开,首尾不能有空格),如果数组为空,则不输出元素。之后根据这个数组与上个数组是否相同,输出“unequal to above.”(不相等)和“equal to above”(相等)。



Sample Input



53 1 2 33 1 2 307 1 2 3 4 5 6 77 1 2 3 4 5 6 8



Sample Output



1 2 31 2 3 equal to above. unequal to above.1 2 3 4 5 6 7 unequal to above.1 2 3 4 5 6 8 unequal to above.



HINT

Append Code



append.cc,


[ Submit][Status][Web Board]


한국어<  中文 فارسی English ไทย All Copyright Reserved 2010-2011 SDUSTOJ TEAM
GPL2.0 2003-2011 HUSTOJ Project TEAM
Anything about the Problems, Please Contact Admin:admin


#include<iostream>
#include<cmath>
#define dd delete
using namespace std;
class Array{
public:
    int length;
    int *mems;
    Array():length(0),mems(NULL){}
    int operator==(Array &a)
    {
        int i;
        for(i=0;i<length;i++)
        {
            if(mems[i]!=a.mems[i])
                break;
        }
        if(this==&a)
            return 1;
        else if(a.length==length&&length==i)
            return 1;
        else
            return 0;
    }
    ~Array(){if(mems!=NULL) dd []mems;}
    friend istream &operator>>(istream &is,Array &a);
    friend ostream &operator<<(ostream &os,Array &a);
 
};
istream &operator>>(istream &is,Array &a)
{
    is>>a.length;
    a.mems = new int[a.length];
    for(int i=0;i<a.length;i++){
        is>>a.mems[i];
    }
    return is;
}
ostream &operator<<(ostream &os,Array &a)
{
    for(int i=0;i<a.length;i++)
        if(i!=a.length-1)
            os<<a.mems[i]<<" ";
        else
            os<<a.mems[i];
        return os;
}
int main()
{
    int cases;
    cin>>cases;
    Array arraies[cases];
    for (int i = 0; i < cases; i++)
    {
        cin>>arraies[i];
    }
    cout<<arraies[0]<<endl;
    for (int i = 1; i < cases; i++)
    {
        if (arraies[i] == arraies[i - 1])
        {
            cout<<arraies[i]<<" "<<"equal to above."<<endl;
        }
        else
        {
            cout<<arraies[i]<<" "<<"unequal to above."<<endl;
        }
    }
    return 0;
}



标签:mems,int,运算符,length,整型,数组,Problem,Array
From: https://blog.51cto.com/u_16129621/6370406

相关文章

  • Problem D: 字符构成的图形
    HomeWebBoardProblemSetStandingStatusStatisticsProblemD:字符构成的图形TimeLimit:1Sec  MemoryLimit:128MBSubmit:1342  Solved:832[Submit][Status][WebBoard]Description定义CharGraph类,用于输出一个由指定字符组成的图形。该类包括:1......
  • Problem A: 克隆人来了!
    HomeWebBoardProblemSetStandingStatusStatisticsProblemA:克隆人来了!TimeLimit:1Sec  MemoryLimit:128MBSubmit:1979  Solved:1072[Submit][Status][WebBoard]Description克隆技术飞速发展,克隆人已经成为现实了!!所以,现在由你来编写一个Pe......
  • Problem L: STL——字符串排序
    HomeWebBoardProblemSetStandingStatusStatisticsProblemL:STL——字符串排序TimeLimit:1Sec  MemoryLimit:128MBSubmit:3482  Solved:1666[Submit][Status][WebBoard]Description  对N个字符串排序。  0<N<=5000......
  • Problem G: 时间类的12小时制输出
    HomeWebBoardProblemSetStandingStatusStatisticsProblemG:时间类的12小时制输出TimeLimit:4Sec  MemoryLimit:128MBSubmit:4541  Solved:2405[Submit][Status][WebBoard]Description封装一个时间类Time,用于时间处理的相关功能,支持24......
  • Problem F: 时间类的常量
    HomeWebBoardProblemSetStandingStatusStatisticsProblemF:时间类的常量TimeLimit:4Sec  MemoryLimit:128MBSubmit:2103  Solved:1715[Submit][Status][WebBoard]Description封装一个时间类Time,用于时间处理的相关功能,支持以下操作:......
  • Problem A: 平面上的点和线——Point类、Line类 (I)
    HomeWebBoardProblemSetStandingStatusStatisticsProblemA:平面上的点和线——Point类、Line类(I)TimeLimit:1Sec  MemoryLimit:128MBSubmit:3609  Solved:2357[Submit][Status][WebBoard]Description在数学上,平面直角坐标系上的点......
  • Problem B: 类的初体验(II)
    HomeWebBoardProblemSetStandingStatusStatisticsProblemB:类的初体验(II)TimeLimit:1Sec  MemoryLimit:128MBSubmit:715  Solved:653[Submit][Status][WebBoard]Description定义一个类Data,只有一个double类型的属性和如下3个方法:1. 带1......
  • Problem D: 类的初体验(IV)
    HomeWebBoardProblemSetStandingStatusStatisticsProblemD:类的初体验(IV)TimeLimit:1Sec  MemoryLimit:128MBSubmit:1075  Solved:657[Submit][Status][WebBoard]Description定义一个类Data,只有一个int类型的属性和如下方法:1. 缺省构造......
  • Problem A: 平面上的点——Point类 (I)
    ProblemA:平面上的点——Point类(I)TimeLimit:1Sec  MemoryLimit:4MBSubmit:8255  Solved:3705[Submit][Status][WebBoard]Description在数学上,平面直角坐标系上的点用X轴和Y轴上的两个坐标值唯一确定。现在我们封装一个“Point类”来实现平面上......
  • Problem A: 你会定义类吗?
    ProblemA:你会定义类吗?TimeLimit:1Sec  MemoryLimit:128MBSubmit:1373  Solved:1078[Submit][Status][WebBoard]Description定义一个类Demo,有构造函数、析构函数和成员函数show(),其中show()根据样例的格式输出具体属性值。该类只有一个int类型的成......