张传旭 2016-03-23 03:56 采纳率: 92.3%
浏览 6651
已采纳

华为上机测试题,我只得了50分,为什么?

图片说明
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
void main()
{
char str[1024];
int num[1024] = {0};
scanf("%s", str);

for (int i = 0; i < strlen(str); i++)    //判断是否除了字母以外还有其他字符如果有则删去
{
    while (str[i]<'A' || str[i]>'z' )
    {
        for (int j = i; j < strlen(str); j++)
        {
            str[j] = str[j + 1];
        }
        if (str[i] == '\0')
        {
            break;
        }
    }
}
int i = 0;
while (str[i]!='\0')
{
    int count = 1;
    int j = i;
    while (str[j] == str[j + 1])
    {
        count++;
        j++;
    }
    num[i] = count;
    printf("%c", str[i]);
    printf("%d", num[i]);
    i += count;
}
//printf("%s", str);

system("pause");

}
图片说明

  • 写回答

6条回答 默认 最新

  • 天涯泪小武 博客专家认证 2016-03-23 07:25
    关注

    以华为的结果看,给50分也是正常的,结果对,但离最优解还差的远。华为肯定会更看重逻辑思维、算法复杂度等等。这道题还是比较简单的,但是你用了一个三层for循环嵌套,下面又一个双while循环嵌套,算法复杂度飙到了N的3次方。
    事实上完全可以用一个for就解决的问题,属于线性复杂度的问题。你的性能肯定相当之差。
    我用java写了一个,单层循环解决的。你可以参考一下。
    public class Test {
    public static void main(String[] args) {
    String s = "abbc65yyy&*ccc$b1baa00";
    StringBuffer sb = new StringBuffer();
    char[] array = s.toCharArray();
    char temp = '0';
    int nowCharCount = 1;
    for (int i = 0; i < array.length; i++) {
    char c = array[i];
    if (isEnglish(c)) {
    //如果刚才出现了字符c
    if (temp == c) {
    sb.deleteCharAt(sb.length() - 1);
    sb.append(nowCharCount + 1);
    nowCharCount++;
    continue;
    }
    nowCharCount = 1;
    temp = c;
    sb.append(c);
    sb.append(nowCharCount);
    } else {
    //初始化
    nowCharCount = 1;
    temp = '0';
    }
    }

        System.out.println(sb.toString());
    }
    
    private static boolean isEnglish(char c) {
        if ('a' <= c && c <= 'z') {
            return true;
        }
        return false;
    }
    

    }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)