网站页面框架设计影响用户怎样在百度上打广告
文章目录
- 前言
- 一、求奇数的乘积
- 二、数值统计
- 三、青年歌手大奖赛_评委会打分
前言
本文为《C++学习》的第12篇文章,今天我们做三道HDOJ的题目来训练我们之前所学的顺序表的,模板代码。
一、求奇数的乘积
#include<iostream>
#include<stdexcept>#define eleType intusing namespace std;struct SeqList{eleType *elements;int size;int capacity;
};void init_list(SeqList *list, int capacity){list->elements = new eleType[capacity];list->size = 0;list->capacity = capacity;
}void insert_list(SeqList *list, int index, eleType element){if(index < 0 || index > list->size){throw std::invalid_argument("invalid index");}if(list->size >= list->capacity){//注意>=号,以防越界int newcapacity = list->capacity * 2;eleType *newelements = new eleType[newcapacity];for(int i = 0; i < list->size; ++i){newelements[i] = list->elements[i];}delete[] list->elements;list->elements = newelements;list->capacity = newcapacity;}for(int i = list->size; i > index; --i){list->elements[i] = list->elements[i - 1];}list->elements[index] = element;list->size++;
}eleType get_element(SeqList *list, int index){if(index < 0 || index >= list->size){//注意>=号,以防越界 throw std::invalid_argument("invalid index");}return list->elements[index];
}int main(){int n;while(cin >> n){SeqList list;init_list(&list, n > 0 ? n : 1);//根据输入规模来确定动态数组的范围,防止频繁扩容导致效率低下 for(int i = 0; i < n; ++i){int x;cin >> x;insert_list(&list, i, x);}int mul = 1, val = 1;for(int i = 0; i < n; ++i){val = get_element(&list, i);if(val % 2 ==1){mul = mul * val;}}cout << mul << endl;}return 0;
}
这里为了加深我们对顺序表模板代码的熟悉程度,希望大家在学习的时候能把之前模板代码里在这题目中需要用到的函数接口重新手写一遍加深印象。
当然,这道题完全可以只用数组或者用STL里的vector来解决,这里便不多赘述,感兴趣的可以自己去了解一下。
二、数值统计
#include <iostream>
#include <stdexcept>#define eleType doubleusing namespace std;struct SeqList {eleType *elements;int size;int capacity;
};void init_list(SeqList *list, int capacity) {list->elements = new eleType[capacity];list->size = 0;list->capacity = capacity;
}void insert_list(SeqList *list, int index, eleType element) {if (index < 0 || index > list->size) {throw std::invalid_argument("invalid index");}if (list->size >= list->capacity) {int newcapacity = list->capacity * 2;eleType *newelements = new eleType[newcapacity];for (int i = 0; i < list->size; ++i) {newelements[i] = list->elements[i];}delete[] list->elements;list->elements = newelements;list->capacity = newcapacity;}for (int i = list->size; i > index; --i) {list->elements[i] = list->elements[i - 1];}list->elements[index] = element;list->size++;
}eleType get_element(SeqList *list, int index) {if (index < 0 || index >= list->size) {throw std::invalid_argument("invalid index");}return list->elements[index];
}int main() {int n;while (cin >> n && n) {SeqList list;init_list(&list, n > 0 ? n : 1);for (int i = 0; i < n; ++i) {eleType x;cin >> x;insert_list(&list, i, x);}int a = 0, b = 0, c = 0;for (int i = 0; i < n; ++i) {eleType ele = get_element(&list, i);if (ele > 0) {++c;} else if (ele < 0) {++a;} else {++b;}}cout << a << " " << b << " " << c << endl;// 释放内存delete[] list.elements;}return 0;
}
三、青年歌手大奖赛_评委会打分
#include<iostream>
#include<stdexcept>#define eleType doubleusing namespace std;struct SeqList{eleType *elements;int size;int capacity;
};void init_list(SeqList *list, int capacity){list->elements = new eleType[capacity];list->size = 0;list->capacity = capacity;
}void insert_list(SeqList *list, int index, eleType element){if(index < 0 || index > list->size){throw std::invalid_argument("invalid index");}if(list->size >= list->capacity){//注意>=号,以防越界int newcapacity = list->capacity * 2;eleType *newelements = new eleType[newcapacity];for(int i = 0; i < list->size; ++i){newelements[i] = list->elements[i];}delete[] list->elements;list->elements = newelements;list->capacity = newcapacity;}for(int i = list->size; i > index; --i){list->elements[i] = list->elements[i - 1];}list->elements[index] = element;list->size++;
}eleType get_element(SeqList *list, int index){if(index < 0 || index >= list->size){//注意>=号,以防越界 throw std::invalid_argument("invalid index");}return list->elements[index];
}int main(){int n;while(cin >> n){ SeqList list;init_list(&list, 10); for(int i = 0; i < n; ++i){eleType x;cin >> x;insert_list(&list, i, x);} eleType max = -1, min =101;eleType score = 0;for(int i = 0; i < n; ++i){eleType curr = get_element(&list, i);if(curr > max) max = curr;if(curr < min) min = curr; score += curr;}score -= max;score -= min;score /= (n - 2);printf("%.2lf\n", score);}return 0;
}
这就是今天的全部内容了,谢谢大家的观看,不要忘了给一个免费的赞哦!