华为机试题+答案

[编程|100分] 在字符串中找出连续最长的数字串

时间限制:3秒

空间限制:32768K

题目描述

请一个在字符串中找出连续最长的数字串,并把这个串的长度返回;如果存在长度相同的连续数字串,返回最后一个连续数字串;

注意:数字串只需要是数字组成的就可以,并不要求顺序,比如数字串“1234”的长度就小于数字串“1359055”,如果没有数字,则返回空字符串(“”)而不是NULL!(说明:不需要考虑负数)

输入描述:

字符串输出描述:

连续数字串&在所有数字串最长的长度

示例1

输入

abcd12345ed125ss123058789

输出

123058789

9

#include <iostream>
#include <string>
using namespace std;

int main() {
  string s;
  while (cin >> s) {
    int len = 0;
    int maxLen = 0;
    int pos = -1;
    for (int i = 0; i < s.size(); i++) {
      if (s[i] >= '0' && s[i] <= '9') {
        len++;
        if (maxLen <= len) {
          maxLen = len;
          pos = i;
        }
      } else {
        len = 0;
      }
    }
    if(maxLen)
        cout << s.substr(pos - maxLen + 1, maxLen) << endl;
    cout << maxLen << endl;
  }
  return 0;
}

2.[编程|200分] 报数游戏

时间限制:3秒

空间限制:32768K

题目描述

100个人围成一圈,每个人有一个编码,编号从1开始到100。他们从1开始依次报数,报到为M的人自动退出圈圈,然后下一个人接着从1开始报数,直到剩余的人数小于M。请问最后剩余的人在原先的编号为多少? 例如输入M=3时,输出为: “58,91” ,输入M=4时,输出为: “34,45,97”。

输入描述:

函数原型:

void NumberOffGame(int m, char * output);

输入的m,m为大于1且小于100的整数。输出描述:

输出output,其中output已经分配足够的内存空间,无需单独分配;

如果m输入小于等于1,则输出“ERROR!”;

如果m输入大于等于100,则输出“ERROR!”;

否则按照编号从小到大的顺序,以“,”分割输出编号字符串,输出的字符串中标点符号均为半角字符。

 

示例1

输入

3

输出

58,91

#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <functional>
#include <list>
using namespace std;

int main() {
  int n = 100, m;
  while (cin >> m) {
    list<int> linkedlist;
    for (int i = 1; i <= n; i++) {
      linkedlist.push_back(i);
    }
    if (m <= 1 || m >= 100) {
      cout << "ERROR!" << endl;
      continue;
    }
    list<int>::iterator iter = linkedlist.begin();
    while (linkedlist.size() >= m) {
      int d = m - 1;
      while (d--) {
        iter++;
        if (iter == linkedlist.end())
          iter = linkedlist.begin();
      }
      list<int>::iterator temp = iter;
      iter++;
      if (iter == linkedlist.end())
        iter = linkedlist.begin();
      linkedlist.erase(temp);
    }
    iter = linkedlist.begin();
    cout << *iter;
    while (++iter != linkedlist.end())
      cout << "," << *iter;
    cout << endl;
  }

  return 0;
}

3.[编程|300分] 接啤酒

时间限制:3秒

空间限制:32768K

题目描述

酒馆里有m个龙头可供顾客们接啤酒,每个龙头每秒的出酒量相等,都是1。现有n名顾客准备接酒,他们初始的接酒顺序已经确定。将这些顾客按接酒顺序从1到n编号,i号顾客的接酒量为w_i。接酒开始时,1到m号顾客各占一个酒龙头,并同时打开龙头接酒。当其中某个顾客j完成其接酒量要求wj后,下一名排队等候接酒的顾客k马上接替j顾客的位置开始接酒。这个换人的过程是瞬间完成的,且没有任何酒的浪费。即j顾客第x秒结束时完成接酒,则k顾客第x+1秒立刻开始接酒。若当前接酒人数n’不足m,则只有n’个龙头供酒,其它m-n’个龙头关闭。现在给出n名顾客的接酒量,按照上述规则,问所有顾客都接完酒需要多少秒?

输入描述:

输入包括两行,第一行为以空格分割的两个数n和m,分别表示接酒的人数和酒龙头个数,均为正整数。

 

第二行n个整数w_i(1 <= w_i <= 100)表示每个顾客接酒量输出描述:

如果输入合法输出酒所需总时间(秒)。

 

注意:最终交付的函数代码中不要向控制台打印输出任何无关信息。

示例1

输入

5 3

4 4 1 2 1

输出

4

#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <functional>
using namespace std;

int main() {
  //freopen("C:\\Users\\Ethan\\Desktop\\huawei3.txt", "r", stdin);
  int n, m;
  while (cin >> n >> m) {
    priority_queue< int, vector<int>, greater<int> > s;
    int w;
    for (int i = 1; i <= m; i++) s.push(0);
    for (int i = 0; i < n; i++) {
      cin >> w;
      s.push(s.top() + w);
      s.pop();
    }
    while (s.size() != 1) s.pop();
    cout << s.top() << endl;
  }

  return 0;
}



  • 7
    点赞
  • 70
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值