网络营销网站建设,怎样注册公司流程,企业管理系统项目经历,模板之家html5今天继续练习C和ACM模式 在写题之前先了解一些新的知识 1.#include algorithm #include algorithm 是 C 标准库中的头文件之一#xff0c;其中包含了一系列用于处理各种容器#xff08;如数组、向量、列表等#xff09;和其他数据结构的算法。这个头文件提供…今天继续练习C和ACM模式 在写题之前先了解一些新的知识 1.#include algorithm #include algorithm 是 C 标准库中的头文件之一其中包含了一系列用于处理各种容器如数组、向量、列表等和其他数据结构的算法。这个头文件提供了多种常用的算法包括排序、查找、删除等以及一些用于操作迭代器的函数。 以下是一些 algorithm 头文件中常用的函数
1. **排序算法** - std::sort(begin, end)对范围 [begin, end) 的元素进行排序。 - std::stable_sort(begin, end)稳定排序保留相等元素的相对顺序。
2. **查找算法** - std::find(begin, end, value)在范围 [begin, end) 中查找值为 value 的元素返回指向该元素的迭代器。 - std::binary_search(begin, end, value)在已排序的范围 [begin, end) 中二分查找值为 value 的元素。
3. **删除和修改算法** - std::remove(begin, end, value)从范围 [begin, end) 中删除所有值为 value 的元素返回指向删除后范围末尾的迭代器。 - std::replace(begin, end, old_value, new_value)将范围 [begin, end) 中所有值为 old_value 的元素替换为 new_value。
4. **其他算法** - std::max_element(begin, end)在范围 [begin, end) 中找到最大元素的迭代器。 - std::min_element(begin, end)在范围 [begin, end) 中找到最小元素的迭代器。 这些算法提供了一种方便且高效的方式来操作和处理各种数据结构。通过 #include algorithm你可以在程序中使用这些算法而不必自己实现它们。 2.第一题 输入两行第一行是n第二行是n个字符每个字符之间使用空格隔开然后输出的是一个已经排好序的字符串。ok开干
代码如下
#include iostream
#include sstream
#include string
#include vector
#include algorithmusing namespace std;int main(){int n;// 获取n的值cin n;// 创建一个字符串数组记录所有的字符串vectorstring result(n);// 存入字符串for(int i 0; i n; i){cin result[i];}//因为要的是排序好的字符串所以我们再进行排序sort(result.begin(), result.end());//输出字符串for(int i 0; i n; i){cout result[i];// 但是记住要添加空格if(i n - 1){cout ;}}return 0;
}呃头文件有些是不需要使用的但是我现在就学了这么几个所以就有几个放几个啦嘿嘿 3.第二题 这里相对于上一题就没有给出明确的判断循环结束的条件了但是仍然也是不影响这种情况昨天已经遇到过了直接拿下
#include iostream
#include string
#include vector
#include algorithmusing namespace std;int main(){vectorstring result;string one;// cin one这个写法就是只要没有到Null就一直循环while(cin one){// 二话不说直接放入字符数组result.push_back(one);// 如果是换行符// 那么到这里这一行就结束了if(cin.get() \n){// 先将数组进行排序处理sort(result.begin(), result.end());// 然后进行输出for(int i 0; i result.size(); i){cout result[i];if(i result.size() - 1){cout ;}}// 然后记得换行cout endl;// 最后记得清除数组元素result.clear();// 然后跳过这次循环continue;}}return 0;
}说是直接拿下但是在这里我和 cin.get()方法周旋了好久
我一直不明白为什么我前面已经把 \n换行符加进去了为什么还需要后面自己手动换行
那么我就做了很多测试最后通过如下代码测试出来了。
#include iostream
#include string
#include vector
#include algorithmusing namespace std;int main() {vectorstring result;string one;// cin one这个写法就是只要没有到Null就一直循环while (cin one) {// 二话不说直接放入字符数组cout one 我是one endl;result.push_back(one);// 如果是换行符// 那么到这里这一行就结束了char get cin.get();cout B get E endl;if ( get \n) {cout 换行了;// 先将数组进行排序处理sort(result.begin(), result.end());// 然后进行输出for (int i 0; i result.size(); i) {cout result[i];if (i result.size() - 1) {cout ;}}// 然后记得换行cout endl;// 最后记得清除数组元素result.clear();// 然后跳过这次循环continue;}}return 0;
}然后输出的样子是这样的、 我输入了 a空格b空格c
你会发现 cin.get()每次读取的都是已经读取之后的下一个元素这就是为什么\n没了需要自己手动的添加get()方法会将它消耗掉所以你可以采用peek()方法但是由于并没用将cin.peek()这个方法的数据添加到数组里所以还是要自己手动添加。这个问题到这里就完美的结束了。
欧耶\(^o^)/
欧耶\(^o^)/
欧耶\(^o^)/
欧耶\(^o^)/
欧耶\(^o^)/ 4.第三题 使用逗号隔开其实也差不多但是由于C的输入法则 “” 是没有办法变成输入的隔开符号的所以就只能采用读取一行然后根据 逗号 隔开的方法来操作了
#include iostream
#include string
#include vector
#include algorithm
#include sstreamusing namespace std;int main(){vectorstring result;string line;// 直接读取一行文字while(getline(cin, line)){// 放入字符串流stringstream strLine(line);// 用于存储每个单词string word;// 通过 ‘,’分割while(getline(strLine, word, ,)){result.push_back(word);}// 排序sort(result.begin(), result.end());// 输出排序之后的结果for(int i 0; i result.size(); i){cout result[i];if(i result.size() - 1){cout ,;}}// 记得换行cout endl;// 记得清空数组result.clear();}return 0;
}结束了
这几天的突击基础训练就这样结束了说实话基本上算是基础的过渡到C了已经貌似好像可以摆脱使用Java写算法的痛苦了。ok
ヾ(▽)Bye~Bye~