当前位置:   article > 正文

华为机考Java版_华为机考java环境

华为机考java环境
  1. /*第一题的题目大概是输入整型数组求数组的最小数和最大数之和,例如输入1,2,3,4则输出为5,当输入只有一个数的时候,
  2. 则最小数和最大数都是该数,例如只输入1,则输出为2;另外数组的长度不超过50
  3. */
  4. import java.util.Scanner;
  5. public class Huawei01
  6. {
  7. public static void main(String[] args)
  8. {
  9. System.out.println("请输入数组长度1~50:");
  10. Scanner s = new Scanner(System.in);
  11. int n = s.nextInt();
  12. System.out.println("请输入数组:");
  13. Scanner sc = new Scanner(System.in);
  14. int []a=new int[n];
  15. for(int i=0;i<n;i++)
  16. {
  17. a[i] = sc.nextInt();
  18. }
  19. int max=a[0],min=a[0];//这两个一定要放到数组赋值以后不然min始终为0
  20. for(int j=0;j<n;j++)
  21. {
  22. if(max<a[j])
  23. max=a[j];
  24. else if(min>a[j])
  25. min=a[j];
  26. }
  27. System.out.println("max=" + max);
  28. System.out.println("min=" + min);
  29. int sum=min+max;
  30. System.out.println(sum + ",");
  31. s.close();
  32. sc.close();
  33. }
  34. }
  35. /*求两个长长整型的数据的和并输出,例如输入1233333333333333 。。。 3111111111111111111111111.。。。,则输出*/
  36. import java.math.BigDecimal;
  37. import java.util.Scanner;
  38. public class Huawei02 {
  39. public static void main(String[] args) {
  40. Huawei02 c = new Huawei02();
  41. System.out.println("请输入两个长整形数:");
  42. Scanner s = new Scanner(System.in);
  43. long n = s.nextLong();
  44. String str1 = "" + n;
  45. BigDecimal bi1 = new BigDecimal(str1);
  46. long m = s.nextLong();
  47. String str2 = "" + m;
  48. BigDecimal bi2 = new BigDecimal(str2);
  49. BigDecimal bi3 = bi1.add(bi2);
  50. // System.out.println(c.add("1234567890123456789043876945","123456781098765432112345622232323232323"));
  51. System.out.println("bi3=" + bi3);
  52. }
  53. public String add(String a, String b) {
  54. if (a.length() > b.length()) {
  55. return adds(a.trim(), b.trim());
  56. } else {
  57. return adds(b.trim(), a.trim());
  58. }
  59. }
  60. private String adds(String a, String b) {
  61. int tmp = 0;
  62. int t = 0;
  63. char[] nc = new char[a.length()];
  64. for (int i = a.length() - 1, j = i - (a.length() - b.length()); i >= 0; i--) {
  65. if (a.charAt(i) > 47 && a.charAt(i) < 58) {
  66. t = tmp + (int) a.charAt(i) - 48;
  67. } else {
  68. throw new FormatException(a + "第" + (i + 1) + "个字符不是数字");
  69. }
  70. if (j >= 0) {
  71. if (b.charAt(j) > 47 && b.charAt(j) < 58) {
  72. t = t + (int) b.charAt(j) - 48;
  73. } else {
  74. throw new FormatException(b + "第" + (j + 1) + "个字符不是数字");
  75. }
  76. }
  77. tmp = t / 10;
  78. nc[i] = (char) (t % 10 + 48);
  79. j--;
  80. }
  81. if (tmp > 0) {
  82. return tmp + new String(nc);
  83. } else {
  84. return new String(nc);
  85. }
  86. }
  87. }
  88. class FormatException extends RuntimeException {
  89. public FormatException(String string) {
  90. super(string);
  91. }
  92. /*03. 通过键盘输入一串小写字母(a~z)组成的字符串。请编写一个字符串过滤程序,若字符串中出现多个相同的字符,将非首次出现的字符过滤掉。
  93. 比如字符串“abacacde”过滤结果为“abcde”。
  94. 要求实现函数:
  95. void stringFilter(const char *pInputStr, long lInputLen, char *pOutputStr);
  96. 【输入】 pInputStr: 输入字符串
  97. lInputLen: 输入字符串长度
  98. 【输出】 pOutputStr: 输出字符串,空间已经开辟好,与输入字符串等长;
  99. */
  100. import java.io.BufferedReader;
  101. import java.io.IOException;
  102. import java.io.InputStreamReader;
  103. public class Huawei03 {
  104. public static void main(String[] args) throws IOException {
  105. // TODO Auto-generated method stub
  106. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  107. System.out.println("Please enter the string:");
  108. String str = br.readLine();
  109. str = str.trim();
  110. StringBuffer sb = new StringBuffer(str);
  111. for (int i = 0; i < sb.length(); i++) {
  112. for (int j = i + 1; j < sb.length(); j++) {
  113. if (sb.charAt(i) == (sb.charAt(j))) {
  114. sb.deleteCharAt(j);
  115. j--;
  116. }
  117. }
  118. }
  119. System.out.println(sb);
  120. }
  121. }
  122. /*04. 通过键盘输入一串小写字母(a~z)组成的字符串。请编写一个字符串压缩程序,将字符串中连续出席的重复字母进行压缩,并输出压缩后的字符串。
  123. 压缩规则:
  124. 1. 仅压缩连续重复出现的字符。比如字符串"abcbc"由于无连续重复字符,压缩后的字符串还是"abcbc".
  125. 2. 压缩字段的格式为"字符重复的次数+字符"。例如:字符串"xxxyyyyyyz"压缩后就成为"3x6yz"
  126. 要求实现函数:
  127. void stringZip(const char *pInputStr, long lInputLen, char *pOutputStr);
  128. 【输入】 pInputStr: 输入字符串
  129. lInputLen: 输入字符串长度
  130. 【输出】 pOutputStr: 输出字符串,空间已经开辟好,与输入字符串等长;
  131. */
  132. import java.io.BufferedReader;
  133. import java.io.IOException;
  134. import java.io.InputStreamReader;
  135. public class Huawei04 {
  136. public static void main(String[] args) throws IOException {
  137. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  138. System.out.println("Please enter the string:");
  139. String str = br.readLine();
  140. str = str.trim();
  141. StringBuffer sb = new StringBuffer(str);
  142. StringBuffer sb1 = new StringBuffer();
  143. int len = sb.length();
  144. String str1 = sb.toString() + " ";
  145. char[] chars = str1.toCharArray();
  146. int i;
  147. int count = 1;
  148. for (i = 0; i < len; i++) {
  149. if (chars[i] == chars[i + 1]) {
  150. count++;
  151. continue;
  152. }
  153. if (count != 0 && count != 1)
  154. sb1.append(count).append(chars[i]).toString();
  155. else
  156. sb1.append(chars[i]).toString();
  157. count = 1;
  158. }
  159. System.out.print(sb1);
  160. }
  161. }
  162. /*30. 计算重复字符个数
  163. 描述: 输入一行字符串。如果字符是英文字母,则输出为输入的英文字符+连续出现的次数 ,
  164. 例如 “ABBCCCC”-> “A1B2C4”;否则,丢弃该字符,不输出。
  165. 运行时间限制: 无限制
  166. 内存限制: 无限制
  167. 输入: 输入的字符串,长度小于1024
  168. 输出: ?输入的英文字符以及其重复的次数
  169. 样例输入: ABBC67%%%CCCAA99
  170. 样例输出: A1B2C4A2
  171. */
  172. import java.io.BufferedReader;
  173. import java.io.IOException;
  174. import java.io.InputStreamReader;
  175. import java.util.ArrayList;
  176. public class Huawei30 {
  177. public static void main(String[] args) throws IOException {
  178. // TODO Auto-generated method stub
  179. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  180. System.out.println("Please enter the string:");
  181. String str = br.readLine();
  182. str = str.trim();
  183. int len = str.length();
  184. String[] str1 = new String[len];
  185. str1 = str.split("[^a-zA-Z]+");
  186. int len1 = str1.length;
  187. int i = 0;
  188. StringBuffer sb = new StringBuffer();
  189. StringBuffer sb1 = new StringBuffer();
  190. for (i = 0; i < len1; i++) {
  191. sb.append(str1[i]).toString();
  192. }
  193. sb.toString();
  194. String str2 = sb.toString();
  195. str2=str2+" ";
  196. int len2 = sb.length();
  197. char[] chars=str2.toCharArray();
  198. int counter = 1;
  199. for (i = 0; i < len2; i++) {
  200. if (chars[i] == chars[i + 1]) {
  201. counter++;
  202. continue;
  203. }
  204. sb1.append(chars[i]).append(counter).toString();
  205. counter = 1;
  206. }
  207. System.out.print(sb1);
  208. }
  209. }
  210. /*27.统计数字出现的次数,最大次数的统计出来
  211. 举例:
  212. 输入:323324423343
  213. 输出:3,6
  214. */
  215. import java.io.BufferedReader;
  216. import java.io.IOException;
  217. import java.io.InputStreamReader;
  218. import java.util.ArrayList;
  219. public class Huawei27 {
  220. public static void main(String[] args) throws IOException {
  221. // TODO Auto-generated method stub
  222. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  223. System.out.println("Please enter the string:");
  224. String str = br.readLine();
  225. str = str.trim();
  226. int maxnum = 0;
  227. int temp = 0;
  228. int j;
  229. char[] chars = str.toCharArray();
  230. ArrayList<Character> list = new ArrayList<Character>();
  231. for (char c : chars) {
  232. if (!list.contains(c)) {
  233. list.add(c);
  234. }
  235. }
  236. int len = list.size();
  237. int lenc = chars.length;
  238. for (int i = 0; i < len; i++) {
  239. int count = 0;
  240. for (j = 0; j < lenc; j++) {
  241. if (list.get(i).equals(chars[j])) {
  242. count++;
  243. if (count > maxnum) {
  244. maxnum = count;
  245. temp = j;
  246. }
  247. }
  248. }
  249. }
  250. System.out.println(chars[temp] + "," + maxnum);
  251. }
  252. }
  253. /*07. 输入一串字符,只包含“0-10”和“,”找出其中最小的数字和最大的数字(可能不止一个),
  254. 输出最后剩余数字个数。如 输入 “3,3,4,5,6,7,7”
  255. */
  256. import java.io.BufferedReader;
  257. import java.io.IOException;
  258. import java.io.InputStreamReader;
  259. import java.util.HashMap;
  260. import java.util.Map;
  261. import java.util.Scanner;
  262. public class Huawei07 {
  263. public static void main(String[] args) throws IOException {
  264. // TODO Auto-generated method stub
  265. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  266. System.out.println("Please enter the string:");
  267. String str = br.readLine();
  268. str = str.trim();
  269. char[] chars = str.toCharArray();
  270. int len = chars.length;
  271. int array[] = new int[100];
  272. int count = 0;
  273. for (int i = 0; i < len; i++) {
  274. if (chars[i] >= '0' && chars[i] <= '9')
  275. array[count++] = chars[i] - '0';
  276. }
  277. int result = count;
  278. int min = array[0];
  279. int max = array[0];
  280. for (int j = 0; j < count; j++) {
  281. if (max < array[j])
  282. max = array[j];
  283. else if (min > array[j])
  284. min = array[j];
  285. }
  286. System.out.println("最大数是:" + max);
  287. System.out.println("最小数是:" + min);
  288. StringBuffer sb = new StringBuffer();
  289. for (int k = 0; k < count; k++) {
  290. if (array[k] == min)
  291. result--;
  292. if (array[k] == max)
  293. result--;
  294. }
  295. for (int k = 0; k < count; k++) {
  296. if (array[k] != min && array[k] != max)
  297. sb.append(array[k] + " ");
  298. }
  299. System.out.println("除去最大值与最小值,剩余数字个数为:" + result + "个");
  300. System.out.println("剩余的数字为:" + sb);
  301. }
  302. }
  303. /*9. 删除子串,只要是原串中有相同的子串就删掉,不管有多少个,返回子串个数。*/
  304. public class Huawei09 {
  305. public static void main(String[] args) {
  306. String str = "123abc12de1234fg1hi34j123k";
  307. String sub_str = "123";
  308. int count = 0;
  309. StringBuffer sb = new StringBuffer(str);
  310. // if (str.length() != str.replace(sub_str, "").length()) 判断字符串包含
  311. while (sb.indexOf(sub_str) != -1) {
  312. int a = sb.indexOf(sub_str);
  313. sb.delete(a, a + 3);
  314. count++;
  315. }
  316. System.out.println("包含子串个数为:" + count);
  317. System.out.println("去除子串后的字符串为:" + sb);
  318. }
  319. }
  320. /*14. 字串转换
  321. 问题描述:
  322. 将输入的字符串(字符串仅包含小写字母‘a’到‘z’),按照如下规则,循环转换后输出:a->b,b->c,…,y->z,z->a
  323. ;若输入的字符串连续出现两个字母相同时,后一个字母需要连续转换2次。
  324. 例如:aa 转换为 bc,zz 转换为 ab;当连续相同字母超过两个时,第三个出现的字母按第一次出现算。
  325. 要求实现函数:
  326. void convert(char *input,char* output)
  327. 【输入】 char *input , 输入的字符串
  328. 【输出】 char *output ,输出的字符串
  329. 【返回】 无
  330. */
  331. import java.io.BufferedReader;
  332. import java.io.IOException;
  333. import java.io.InputStreamReader;
  334. public class Huawei14 {
  335. public static void main(String[] args) throws IOException {
  336. // TODO Auto-generated method stub
  337. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  338. System.out.println("Please enter the string:");
  339. String str = br.readLine();
  340. str = str.trim();
  341. char[] input = str.toCharArray();
  342. char[] output = new char[256];
  343. int len = input.length;
  344. int i;
  345. int flag = 0;
  346. char temp = (char) ((input[0] - 'a' + 1) % 26 + 'a');
  347. for (i = 0; i < len; i++) {
  348. if (input[i]!=temp) {
  349. output[i] = (char) ((input[i] - 'a' + 1) % 26 + 'a');
  350. temp = input[i];
  351. flag = 1;
  352. } else
  353. if (flag == 1) {
  354. output[i] = (char) ((input[i] - 'a' + 2) % 26 + 'a');
  355. temp = input[i];
  356. flag = 0;
  357. } else {
  358. output[i] = (char) ((input[i] - 'a' + 1) % 26 + 'a');
  359. temp = input[i];
  360. flag = 1;
  361. }
  362. }
  363. System.out.print("变换后字符串为:");
  364. for(i=0;i<len;i++)
  365. System.out.print(" "+ output[i]);
  366. }
  367. }
  368. /*35给定一个字符串,把字符串内的字母转换成该字母的下一个字母,a换成b,z换成a,Z换成A,
  369. * 如aBf转换成bCg,字符串内的其他字符不改变,给定函数,编写函数
  370. void Stringchang(const char*inpu,char*output)
  371. 其中input是输入字符串,output是输出字符串
  372. */
  373. import java.io.BufferedReader;
  374. import java.io.IOException;
  375. import java.io.InputStreamReader;
  376. public class Huawei35 {
  377. public static void main(String[] args) throws IOException {
  378. // TODO Auto-generated method stub
  379. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  380. System.out.println("Please enter the string:");
  381. String str = br.readLine();
  382. char[] chars = str.toCharArray();
  383. int len = chars.length;
  384. StringBuffer sb = new StringBuffer();
  385. char[] chars1 = new char[len];
  386. int i;
  387. for (i = 0; i < len; i++) {
  388. if (String.valueOf(chars[i]).matches("[a-zA-Z]+")) {
  389. if (chars[i] == 'z')
  390. chars1[i] = 'a';
  391. else if (chars[i] == 'Z')
  392. chars1[i] = 'A';
  393. else {
  394. chars1[i] = (char) (chars[i] + 1);
  395. }
  396. } else
  397. chars1[i] = chars[i];
  398. sb.append(chars1[i]).append(" ").toString();
  399. }
  400. System.out.println(sb.toString());
  401. }
  402. }
  403. /*29. 子串分离
  404. 题目描述:
  405. 通过键盘输入任意一个字符串序列,字符串可能包含多个子串,子串以空格分隔。请编写一
  406. 个程序,自动分离出各个子串,并使用’,’将其分隔,并且在最后也补充一个
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/183264
推荐阅读
相关标签
  

闽ICP备14008679号