C. 成绩查询(指针运算)

时间限制1s   内存限制128MB

题目描述

已知一组学生成绩,然后根据输入的序号查询成绩

要求:

1. 使用一个整数数组存储学生成绩

2. 使用一个指针指向数组中间元素

3. 使用++和--运算符,求出数组中间元素的前一个成绩和后一个成绩

4. 输入一个序号,然后计算这个序号的元素和中间元素的距离,然后使用指针去访问

例如有11个学生,指针指向中间的学生也就是第6个学生,若输入序号3,即查询第3个学生的成绩,第3和第6之间距离为3,那么指针应该怎么运算呢???

5. 整个程序除了输入时可以使用数组下标,其他部分尽量使用使用指针进行访问。

输入

第一行输入t表示有t个测试实例

第二行先输入n,表示有n个学生,然后再输入n个成绩(正整数)

第三行输入1个序号,表示要查询成绩的学生的序号。

依次输入t个实例

按自然意义,序号是从1开始计算

提示:在数组中是从........

输出

对于每个测试实例:

第一行输出数组中间元素的前一个成绩和后一个成绩

第二行根据序号输出1个成绩

输入样例: 

2
7 66 99 88 44 77 33 11
2
11 60 80 50 20 90 35 70 40 10 95 30
9

输出样例: 

88 77
99
90 70
10

#include<iostream>
using namespace std;
int main() {
	int t,n,search;
	cin >> t;
	int* score;
	while (t--) {
		cin >> n;
		score = new int[n];
		int* p = score + n / 2;
		for (int i = 0; i < n; i++) {
			cin >> score[i];
		}
		cin >> search;
		cout << *(--p) << " ";
		p = score + n / 2;
		cout << *(++p) << endl;
		//以上三行也可写为cout << *(p-1) << " " << *(p+1) << endl;
		p = score +search- 1;
		cout << *p << endl;
		delete[] score;
	}
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 复数类的加减乘除运算符重载代码如下: ```c++ #include <iostream> using namespace std; class Complex { private: double real; double imag; public: Complex(double r = 0, double i = 0) : real(r), imag(i) {} Complex operator+(const Complex& c) const; Complex operator-(const Complex& c) const; friend Complex operator*(const Complex& c1, const Complex& c2); friend Complex operator/(const Complex& c1, const Complex& c2); friend ostream& operator<<(ostream& os, const Complex& c); }; Complex Complex::operator+(const Complex& c) const { return Complex(real + c.real, imag + c.imag); } Complex Complex::operator-(const Complex& c) const { return Complex(real - c.real, imag - c.imag); } Complex operator*(const Complex& c1, const Complex& c2) { return Complex(c1.real * c2.real - c1.imag * c2.imag, c1.real * c2.imag + c1.imag * c2.real); } Complex operator/(const Complex& c1, const Complex& c2) { double denominator = c2.real * c2.real + c2.imag * c2.imag; return Complex((c1.real * c2.real + c1.imag * c2.imag) / denominator, (c1.imag * c2.real - c1.real * c2.imag) / denominator); } ostream& operator<<(ostream& os, const Complex& c) { os << "(" << c.real << ", " << c.imag << "i)"; return os; } int main() { Complex c1(1, 2), c2(3, 4); cout << c1 + c2 << endl; // (4, 6i) cout << c1 - c2 << endl; // (-2, -2i) cout << c1 * c2 << endl; // (-5, 10i) cout << c1 / c2 << endl; // (0.44, -0.08i) return 0; } ``` 2. 学生类的流插入运算符和流提取符重载代码如下: ```c++ #include <iostream> #include <string> using namespace std; class Student { private: int id; char gender; string name; double score; public: Student(int i = 0, char g = 'F', string n = "", double s = 0) : id(i), gender(g), name(n), score(s) {} friend ostream& operator<<(ostream& os, const Student& s); friend istream& operator>>(istream& is, Student& s); }; ostream& operator<<(ostream& os, const Student& s) { os << s.id << " " << s.gender << " " << s.name << " " << s.score; return os; } istream& operator>>(istream& is, Student& s) { is >> s.id >> s.gender >> s.name >> s.score; return is; } int main() { Student s(123, 'F', "Alice", 89.5); cout << s << endl; // 123 F Alice 89.5 Student t; cin >> t; cout << t << endl; return 0; } ``` 3. 图形类的面积计算和基类指针数组代码如下: ```c++ #include <iostream> using namespace std; class Shape { public: virtual double area() const = 0; }; class Circle : public Shape { private: double radius; public: Circle(double r = 0) : radius(r) {} virtual double area() const { return 3.14159 * radius * radius; } }; class Square : public Shape { private: double side; public: Square(double s = 0) : side(s) {} virtual double area() const { return side * side; } }; class Rectangle : public Shape { private: double length; double width; public: Rectangle(double l = 0, double w = 0) : length(l), width(w) {} virtual double area() const { return length * width; } }; class Trapezoid : public Shape { private: double top; double bottom; double height; public: Trapezoid(double t = 0, double b = 0, double h = 0) : top(t), bottom(b), height(h) {} virtual double area() const { return (top + bottom) * height / 2; } }; class Triangle : public Shape { private: double base; double height; public: Triangle(double b = 0, double h = 0) : base(b), height(h) {} virtual double area() const { return base * height / 2; } }; int main() { Shape* shapes[5]; shapes[0] = new Circle(2); shapes[1] = new Square(3); shapes[2] = new Rectangle(4, 5); shapes[3] = new Trapezoid(2, 4, 3); shapes[4] = new Triangle(2, 3); double totalArea = 0; for (int i = 0; i < 5; i++) { totalArea += shapes[i]->area(); delete shapes[i]; } cout << "Total area: " << totalArea << endl; return 0; } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值