解题思路:
输入,删除,输出,简单直接
注意事项:
可能有点多此一举了,因为不知道题目有几组输入,弄了个 while(cin >> n){...}
参考代码:
#include<bits/stdc++.h> using namespace std; int main() { int n, tmp; // tmp 存输入数 list<int> li; // li 是STL的链表 list<int>::iterator iter; // li 的迭代器 while(cin >> n){ int target; // 要删除的数 li.clear(); // 清空一下链表,当然如果只有一组测试数据,那就没必要了 while(n--){ cin >> tmp; // 输入 li.push_back(tmp); // 插入链表 } cin >> target; li.remove(target); // 核心代码,一句搞定,remove会删除 list 里所有值为target的元素 // 遍历输出 for(iter = li.begin();iter!= li.end();++iter){ cout << (*iter) << ' '; } // 打个回车 cout << endl; } return 0; }
0.0分
0 人评分