贵州大学在线判题系统oj-面向对象程序设计-第一次在线作业

贵大懒人养成器,欢迎伸手。萌新写的问题肯定多多,轻喷。

第一题

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBARG9sbGFuWg==,size_20,color_FFFFFF,t_70,g_se,x_16

#include <iostream>
#include <string>
using namespace std;
class Test {
public:
	int DataLength(const string& a) {
		return a.length();
	}
	int DataLength(int b) {
		int d = 1;
		while ((b /= 10) > 0) {
			d++;
		}
		return d;
	}
};

第二题

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBARG9sbGFuWg==,size_20,color_FFFFFF,t_70,g_se,x_16

#include <iostream>
#include <string>
using namespace std;
class Rectangle {
public:
	int top, right, bottom, left;
	Rectangle(int a, int b, int c, int d) :top(a), right(b), bottom(c), left(d) {}
	int check() {
		if (top > bottom && right > left) { return 1; }
		else { return 0; }
	}
	void showLeftTop() {
		if(check())
		cout<< left << "," << top << endl;
		else
			cout << 0 << "," << 0 << endl;
	}
	void showRightTop() {
		if (check())
			cout << right << "," << top << endl;
		else
			cout << 0 << "," << 0 << endl;
	}
	void showLeftBottom() {
		if (check())
			cout << left << "," << bottom << endl;
		else
			cout << 0 << "," << 0 << endl;
	}
	void showRightBottom() {
		if (check())
			cout << right << "," << bottom << endl;
		else
			cout << 0 << "," << 0 << endl;
	}
	int getHeight(){
		if (check())
			return (top - bottom);
		else
			return 0;
		
	}
	int getWidth() {
		if (check())
			return (right - left);
		else
			return 0;
	}
	int getArea() {
		return (getHeight() * getWidth());
	}
	int getPerimeter() {
		return (2 * (getHeight() + getWidth()));
	}
};

 先看第四题watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBARG9sbGFuWg==,size_20,color_FFFFFF,t_70,g_se,x_16

#include <iostream>
#include <string>
using namespace std;
class Circle {
public:
	Circle(int c) :c(c) {}
private:
    int c;
};
class Rectangle {
public:
	Rectangle(int b, int c) :b(b), c(c) {}
private:	
    int b, c;
};
class Area {
public:
	static double calArea(Circle a) { return (a.c*a.c*3.14159265); }
	static int calArea(Rectangle a) {
		return (a.b * a.c);
	}
};

第三题

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBARG9sbGFuWg==,size_20,color_FFFFFF,t_70,g_se,x_16

class Area {
public:
	static double calArea(Circle &a) { return (a.getRadius() * a.getRadius() * 3.14159265); }
	static double calArea(Rectangle &a) {
		return (a.getHeight() * a.getWidth());
	}
	static double calArea(const Circle& a) { return (a.getRadius() * a.getRadius() * 3.14159265); }
	static double calArea(const Rectangle& a) {
		return (a.getHeight() * a.getWidth());
	}
};

第五题

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBARG9sbGFuWg==,size_20,color_FFFFFF,t_70,g_se,x_16

#include <iostream>
using namespace std;
class IntegerProcessing {
public:
	static int getNumberOfOne(int a) {
		int n = 0;//记录1的个数
		do{
			if (a &1!= 0) {
				n++;
			}
			a =a>>1;
		}while(a!=0);
		return n;
	}
	static int getBinMirror(int a) {
		int b = 0;
		for (int i = 0; i < 32; i++) {
			if (i < 16) {
				b |= ((a & (1 << i)) << (31 - 2 * i));//a&(1<<i)是数字a的第i+1位,将他左移(31-2*i)
			}
			else {
				b |= ((a & (1 << i)) >> (2 * i - 31));
			}
		}
		return b;
	}
};

 第六题

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBARG9sbGFuWg==,size_20,color_FFFFFF,t_70,g_se,x_16

#include <iostream>
#include <string>
using namespace std;

class Array2D {
public:
	Array2D(int a):rows(a){
		array = (double(*)[8])malloc(a * 8 * sizeof(double));
	}
	~Array2D()
	{
		cout << "释放了一个" << rows << "行8列的数组" << endl;
	}
	void setElem(int a, int b, double c) {
		array[a][b] = c;
	}
	int getRows() {
		return rows;
	}
	int getColumns() {
		return 8;
	}
	double getMaxOfRow(int a) {
		double b = array[a][0];//存最大的数
		for (int i = 0; i < 8; i++) {
			if (array[a][i] > b) {
				b = array[a][i];
			}
		}
		return b;
	}
	double getMinOfRow(int a) {
		double b =array[a][0];//存最小的数
		for (int i = 0; i < 8; i++) {
			if (array[a][i] < b) {
				b = array[a][i];
			}
		}
		return b;
	}
	double getAvgOfRow(int a) {
		double b = 0;//存数的和
		for (int i = 0; i < 8; i++) {
				b += array[a][i];
		}
		return (b/8);
	}
	double getMinOfArray() {
		double b = array[0][0];//存最小的数
		for (int a = 0; a < rows; a++) {
			for (int i = 0; i < 8; i++) {
				if (array[a][i] < b) {
					b = array[a][i];
				}
			}
		}
		return b;
	}
	double getMaxOfArray() {
		double b = array[0][0];//存最大的数
		for (int a = 0; a < rows; a++) {
			for (int i = 0; i < 8; i++) {
				if (array[a][i] > b) {
					b = array[a][i];
				}
			}
		}
		return b;
	}
	double getAvgOfArray() {
		double b =0;//存最小的数
		for (int a = 0; a < rows; a++) {
			for (int i = 0; i < 8; i++) {
					b += array[a][i];
			}
		}
		return (b/(rows*8));
	}
private:
	int rows;
	double(*array)[8];
};

第七题

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBARG9sbGFuWg==,size_20,color_FFFFFF,t_70,g_se,x_16

#include <iostream>
#include <string>
#include <cmath>
using namespace std;
class Point {
public:
	Point(double x, double y) :x(x), y(y) {}
	friend double dist(Point& a, Point& b) {
		return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
	}
private:
	double x, y;
};
class Circle {
public:
	Circle(double x, double y,double radius) :a(x,y), radius(radius) {}
	double getArea() {
		return (radius * radius * 3.14159265);
	}
	double getPerimeter() {
		return (2 * radius * 3.14159265);
	}
	int isInTheCircle(Point &p){
		if (dist(p, a) < radius) {
			return 1;
		}
		else {
			return 0;
		}
	}
private:
	Point a;
	double radius;
};

 第八题

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBARG9sbGFuWg==,size_20,color_FFFFFF,t_70,g_se,x_16

#include <iostream>
#include <string>
#include <cmath>
class A{
public:
    A(){
        std::cout << "A's constructor is called!" << std::endl;}
    ~A(){
        std::cout << "A's destructor is called!" << std::endl;}
};
class B {
public:
    B(){
        std::cout << "B's constructor is called!" << std::endl;}
    ~B(){
        std::cout << "B's destructor is called!" << std::endl;}
};
class C {
public:
    C() {
        std::cout << "C's constructor is called!" << std::endl;}
    ~C() {
        std::cout << "C's destructor is called!" << std::endl;}
};
class D :public B, public C, public A {//教材p286
public:
    D() {
        std::cout << "D's constructor is called!" << std::endl;}
    ~D() {
        std::cout << "D's destructor is called!" << std::endl;}
};

 第九题

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBARG9sbGFuWg==,size_20,color_FFFFFF,t_70,g_se,x_16

#include <iostream>
#include <string>
#include <cmath>
using namespace std;
class Test {
private:
    int x;
public:
    Test(int x):x(x){}
    void printInfo() {
        cout << 2 * x << endl;
    }
    void printInfo() const{
        cout <<x << endl;
    }
    void setX(int a) {
        x = a;
    }
};

 第十题

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBARG9sbGFuWg==,size_20,color_FFFFFF,t_70,g_se,x_16

#include <iostream>
#include <string>
#include <cmath>
using namespace std;
class Student
{
private:
	string name;
	string subject;
	int age;
	int id;
	static int num;//存储人数
public:
	Student(string a,string b, int c) :name(a), subject(b), age(c) {
		num++;
		id = 20200000 + num;
	};
	Student() :name("Mike"), subject("Math"), age(18) {
		num++;
		id = 20200000 + num;
	};
	Student(Student &a) :name(a.name), subject(a.subject), age(a.age) {
		num++;
		id = 20200000 + num;
	};
	void showInfo() {
		cout << name << " " << id << " " << subject << " " << age << endl;
	}
};
int Student::num = 0;

 第十一题

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBARG9sbGFuWg==,size_20,color_FFFFFF,t_70,g_se,x_16

#include <iostream>
#include <string>
#include <cmath>
using namespace std;
class Point {
public:
    Point(double x, double y) :x(x), y(y) {}
    friend double dist(Point& a, Point& b) {
        return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
    }
private:
    double x, y;
};
class Triangle {
public:
	Triangle(Point a, Point b, Point c) :a(a), b(b), c(c) {
		x = dist(a, b);
		y = dist(a, c);
		z = dist(b, c);
		p = (x + y + z) / 2;
	}
	double getArea() {
		return sqrt((p-x)*(p-y)*(p-z)*p);
	}
private:
	Point a, b, c;
	double x, y, z,p;//海伦公式求面积
};

第十二题

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBARG9sbGFuWg==,size_20,color_FFFFFF,t_70,g_se,x_16

#include <iostream>
#include <string>
#include <cmath>
using namespace std;
class Date
{
private:
	int year, month, day;
public:
	Date(int a, int b, int c) :year(a), month(b), day(c) {};
	Date() :year(2021), month(4), day(16) {};
	int getYear() const { return year; }
	int getMonth() const { return month; }
	int getDay() const { return day; }
	void showDate() {
		cout << getYear() << "/" << getMonth() << "/" << getDay()<<endl;
	}
	void setDate(int a, int b, int c) {
		year = a; month = b; day = c;
	}
};

  • 10
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
这是一道经典的位运算题目,考察对二进制的理解和位运算的熟练程度。 题目描述: 给定一个长度为 $n$ 的数组 $a$,初始时每个数的值都为 $0$。现在有 $m$ 个操作,每个操作为一次询问或修改。 对于询问,给出两个整数 $l,r$,求 $a_l \oplus a_{l+1} \oplus \cdots \oplus a_r$ 的值。 对于修改,给出一个整数 $x$,表示将 $a_x$ 的值加 $1$。 输入格式: 第一行两个整数 $n,m$。 接下来 $m$ 行,每行描述一次操作,格式如下: 1 l r:表示询问区间 $[l,r]$ 的异或和。 2 x:表示将 $a_x$ 的值加 $1$。 输出格式: 对于每个询问操作,输出一个整数表示答案,每个答案占一行。 数据范围: $1 \leq n,m \leq 10^5$,$0 \leq a_i \leq 2^{30}$,$1 \leq l \leq r \leq n$,$1 \leq x \leq n$ 输入样例: 5 5 2 1 2 3 1 2 4 2 2 1 1 5 输出样例: 0 2 解题思路: 对于询问操作,可以利用异或的性质,即 $a \oplus b \oplus a = b$,将 $a_l \oplus a_{l+1} \oplus \cdots \oplus a_r$ 转化为 $(a_1 \oplus \cdots \oplus a_{l-1}) \oplus (a_1 \oplus \cdots \oplus a_r)$,因为两个前缀异或后的结果可以相互抵消,最后的结果即为 $a_1 \oplus \cdots \oplus a_{l-1} \oplus a_1 \oplus \cdots \oplus a_r = a_l \oplus \cdots \oplus a_r$。 对于修改操作,可以将 $a_x$ 对应的二进制数的每一位都分离出来,然后对应位置进行修改即可。由于只有加 $1$ 操作,所以只需将最后一位加 $1$ 即可,其余位不变。 参考代码:

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值