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

华为上机测试题,我只得了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 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥15 一道python难题3
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试