华为笔试题总结
一、查找连续出现次数最多的字符(可以用哈希,但不用哈希会更方便)
#include<stdio.h> int main(void) { char a[128] = { 0 }; int char_count = 0; int char_max = 0; char find_char; scanf_s("%s",a,128); int length = strlen(a); int i = 0; char_count = 1; for (i = 1; i < length; i++) { if (a[i] == a[i -1]) { char_count = char_count + 1; continue; } if (char_max == char_count && find_char > a[i - 1]) { find_char = a[i - 1]; } else if (char_max < char_count) { char_max = char_count; find_char = a[i - 1]; } char_count = 1; } if (char_max == char_count && find_char > a[i - 1]) { find_char = a[i - 1]; } else if (char_max < char_count) { char_max = char_count; find_char = a[i - 1]; } for (i = 0; i < char_max; i++) printf("%c", find_char); while (1); return 0; }
用哈希的方法:
/***** 问题:统计出一个字符串中连续出现字母最多的字符串 比如: aaaaaaaaabbbbbbbbccccccccccccddddddddaaaaaa 会输出ccccccccccccc(如果相等按照ascii码顺序) *****/ #include<stdio.h> #include<string.h> int main() { char s[128] = { 0 }; int count[26] = { 0 }; int count_temp[26] = { 0 }; int len = 0, i = 0; int max_num, max_index; int temp = 0; scanf_s("%s", s,128); len = strlen(s); count_temp[s[0] - 'a'] = 1; //连续字符查找,使用hash的方法 for (i = 1; i < len; i++) { if (s[i] == s[i - 1]) { count_temp[s[i] - 'a']++; continue; } if (count[s[i -1 ] - 'a'] < count_temp[s[i -1] - 'a']) { count[s[i-1] - 'a'] = count_temp[s[i-1] - 'a']; } count_temp[s[i - 1] - 'a'] = 0; count_temp[s[i] - 'a'] = 1; } //最后一个字符的次数,上述没有去对比,所以又对比了一次。 if (count[s[i - 1] - 'a'] < count_temp[s[i - 1] - 'a']) { count[s[i - 1] - 'a'] = count_temp[s[i - 1] - 'a']; } max_num = count[0]; max_index = 0; for (i = 1; i < 26; i++) { if (count[i] > max_num) { max_num = count[i]; max_index = i; } } for (i = 0; i < max_num; i++) { printf("%c", max_index + 'a'); } while (1); return 0; }
二、查找一个次数出现最多的字符(用哈希)
/*也可以演变为:来统计每个字符出现的次数和出现的字符个数(value值一直重复赋值为1),下面就是统计出现的字符个数
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s;
getline(cin ,s);
int ar[128] = {0};
for (int i = 0; i < s.length(); ++i) {
ar[int(s[i])]++;
}
int num = 0;
for (int j = 0; j < 128; ++j) {
if(ar[j])
num++;
}
cout << num << endl;
return 0;
}
三、统计字符串中某个字符串的个数,不区分大小写。
#include<stdio.h> #include<string.h> int main(int argc, char **argv) { char a[1000] = { 0 }; char b = 0; gets(a); b = getchar(); int count_number = 0; switch (b) { case '0'...'9': { for (int i = strlen(a) - 1; i >= 0; i--) { if (b == a[i]) count_number++; } } break; case 'a' ... 'z': { for (int i = strlen(a) - 1; i >= 0; i--) { if (b == a[i] || b - 32 == a[i]) count_number++; } } break; case 'A' ... 'Z': { for (int i = strlen(a) - 1; i >= 0; i--) { if (b == a[i] || b + 32 == a[i]) count_number++; } } break; } printf("%d\n", count_number); return 0; }
四、长整数相加
#include<stdio.h> #include<string.h> #include<stdlib.h> #define min(a,b) ((a>b)?b:a) #define max(a,b) ((a>b)?a:b) void add(char array_a[], char array_b[]) { int length_a = strlen(array_a); int length_b = strlen(array_b); printf("%d,%d\n", length_a, length_b); int min_length = min(length_a, length_b); char i = 0; char flag = 0; char sum = 0; for (i = 0; i < min_length; i++) { (sum = ((array_a[i] - '0') + (array_b[i] - '0') + flag)) >= 10 ? (flag = 1, array_a[i] = sum - 10 + '0') : (flag = 0, array_a[i] = sum + '0'); } if (length_a > min_length) { for (i = min_length; i < length_a; i++) { (sum = ((array_a[i] - '0') + flag)) >= 10 ? (flag = 1, array_a[i] = sum - 10 + '0') : (flag = 0, array_a[i] = sum + '0'); } } if (length_b > min_length) { for (i = min_length; i < length_b; i++) { (sum = ((array_b[i] - '0') + flag)) >= 10 ? (flag = 1, array_a[i] = sum - 10 + '0') : (flag = 0, array_a[i] = sum + '0'); } } if (1 == flag) { array_a[i] = flag + '0'; } } //反转字符串或者数组 void mystrrev(char *a) { int len = strlen(a); char temp = 0; char i; for (i = 0; i < len / 2; i++) { temp = a[i]; a[i] = a[len - i - 1]; a[len - i - 1] = temp; } } int main(int argc, char **argv) { char *a = malloc(100); memset(a, 100, '\0'); if (NULL == a){ printf("malloc is error\n"); } char *b = malloc(100); if (NULL == b){ printf("malloc is error\n"); } memset(b, 100, '\0'); scanf_s("%s", a, 100); scanf_s("%s", b, 100); mystrrev(a); mystrrev(b); add(a, b); mystrrev(a); printf("%s", a); system("pause"); return 0; }
五、找字符串
#include<stdio.h> #include<string.h> #define min(a,b) ((a > b)?(b):(a)) int main(int argc, char **argv) { char str[1000]; gets_s(str,1000); int len = strlen(str); int count_num = 0; int max = 0; int local_pos = 0;//记录下位置 char douhao_flag = 0; int i = 0; for (i = 0; i < len; i++) { if (str[i] >= '0' && str[i] <= '9' || str[i] == '.' || str[i] == '+' || str[i] == '-') { if (str[i] == '+' || str[i] == '-') { count_num = 0;//如果有正负号开始重新计数,确保每次只有一个正负号 } if (str[i] == '.') { if (douhao_flag == 1)//确保逗号不能有两个 { count_num = 0; douhao_flag = 0; } if (count_num == 0)//确保前面有数字,下面确保前面数字不为+-号 { continue; } else { if (count_num == 1 && str[i - 1] == '+' && str[i - 1] == '-')//确保+-号后不为逗号 { count_num = 0; continue; } else { if (str[i + 1] >= '0' && str[i + 1] <= '9')//确保.号后为数字 douhao_flag = 1; else continue; } } } count_num++; if (count_num >= max) { max = count_num; local_pos = i - max + 1; } } else { count_num = 0;//不是数字清0 douhao_flag = 0; } } //输出字符串 for (i = 0; i < max; i++) { if (max == 1 && (str[i + local_pos] == '+' || str[i + local_pos] == '-' || str[i + local_pos] == '.')) printf(""); else printf("%c", str[i + local_pos]); } if (0 == max)//返回空字符串 { printf(""); } printf("\n"); while (1); return 0; }