华为机试题
来源:互联网 发布:excel 去掉空格数据 编辑:程序博客网 时间:2024/06/06 03:14
输入一行数字: 123 423 5645 875 186523
再输入第二行: 23
将第一行中含有第二行中“23”的数排序并输出
结果即: 123 423 186523
解法1:
#include <stdio.h>#include <algorithm>using namespace std;int main() { int a[20], ans[20], index = 0, index2 = 0, m, x; char c = '0'; while(c != '\n') { scanf("%d%c", &a[index++], &c); } scanf("%d", &m); for(int i = 0; i < index; i++) { if(a[i] > 0) { if(a[i]%100 == m) ans[index2++] = a[i]; else a[i] /= 10; } } sort(ans, ans+index2); for(int i = 0; i < index2-1; i++) printf("%d ", ans[i]); printf("%d\n", ans[index2-1]); return 0;}
解法2:
#include <iostream>#include <string>#include <vector>#include <sstream>#include <algorithm>using namespace std;int main() { stringstream ss,ss2; string words, word, key; int x; vector<int> ivec; getline(cin, words); cin >> key; ss << words; while(!ss.eof()) { ss >> word; if(word.find(key) != string::npos) { ss2 << word; ss2 >> x; ss2.str(""); ss2.clear(); ivec.push_back(x); } } sort(ivec.begin(), ivec.end()); bool flag = true; for(vector<int>::iterator iter = ivec.begin(); iter != ivec.end(); ++iter) { if(flag) { cout << *iter; flag = false; } else { cout << " " << *iter; } } cout << endl; return 0;}
0 0
- 华为机试题
- 2011 华为机试题
- 华为机试题
- 华为机试题
- 2013华为机试题
- 华为机试题
- 华为2013机试题
- 华为2012机试题
- 华为机试题
- 【华为机试题】1
- 华为C++机试题
- 华为2014机试题
- 华为2014机试题
- 华为一道机试题
- 华为机试题
- 华为 2014 机试题
- 华为机试题回忆
- 华为机试题
- C++ 高性能无锁日志系统
- CF 454A(Little Pony and Crystal Mine-画图[水])
- 社会化营销
- Android常用布局文件中元素的属性
- CF 259
- 华为机试题
- vs2010编译boost sgd版本的datetime库例子
- Mybatis_reveiw之Mybatis官方的一个很简单的Demo
- java中的接口
- MAC和PHY的区别
- Linux下ALSA声卡编程
- SQL注入原理讲解,很不错!
- android中handler详解
- CF 454B(Little Pony and Sort by Shift-序列位移后单调性判断及最小位移[水])