赞
踩
- /*第一题的题目大概是输入整型数组求数组的最小数和最大数之和,例如输入1,2,3,4则输出为5,当输入只有一个数的时候,
- 则最小数和最大数都是该数,例如只输入1,则输出为2;另外数组的长度不超过50
- */
- import java.util.Scanner;
- public class Huawei01
- {
- public static void main(String[] args)
- {
- System.out.println("请输入数组长度1~50:");
- Scanner s = new Scanner(System.in);
- int n = s.nextInt();
- System.out.println("请输入数组:");
- Scanner sc = new Scanner(System.in);
- int []a=new int[n];
-
- for(int i=0;i<n;i++)
-
- {
- a[i] = sc.nextInt();
- }
-
- int max=a[0],min=a[0];//这两个一定要放到数组赋值以后不然min始终为0
-
- for(int j=0;j<n;j++)
- {
- if(max<a[j])
- max=a[j];
-
- else if(min>a[j])
- min=a[j];
- }
- System.out.println("max=" + max);
- System.out.println("min=" + min);
- int sum=min+max;
- System.out.println(sum + ",");
-
- s.close();
- sc.close();
-
- }
- }
-
- /*求两个长长整型的数据的和并输出,例如输入1233333333333333 。。。 3111111111111111111111111.。。。,则输出*/
- import java.math.BigDecimal;
- import java.util.Scanner;
-
- public class Huawei02 {
-
- public static void main(String[] args) {
-
- Huawei02 c = new Huawei02();
-
- System.out.println("请输入两个长整形数:");
- Scanner s = new Scanner(System.in);
-
- long n = s.nextLong();
- String str1 = "" + n;
- BigDecimal bi1 = new BigDecimal(str1);
-
- long m = s.nextLong();
- String str2 = "" + m;
- BigDecimal bi2 = new BigDecimal(str2);
-
- BigDecimal bi3 = bi1.add(bi2);
-
- // System.out.println(c.add("1234567890123456789043876945","123456781098765432112345622232323232323"));
-
- System.out.println("bi3=" + bi3);
- }
-
- public String add(String a, String b) {
-
- if (a.length() > b.length()) {
-
- return adds(a.trim(), b.trim());
- } else {
-
- return adds(b.trim(), a.trim());
- }
- }
-
- private String adds(String a, String b) {
- int tmp = 0;
- int t = 0;
-
- char[] nc = new char[a.length()];
-
- for (int i = a.length() - 1, j = i - (a.length() - b.length()); i >= 0; i--) {
-
- if (a.charAt(i) > 47 && a.charAt(i) < 58) {
-
- t = tmp + (int) a.charAt(i) - 48;
-
- } else {
-
- throw new FormatException(a + "第" + (i + 1) + "个字符不是数字");
-
- }
-
- if (j >= 0) {
-
- if (b.charAt(j) > 47 && b.charAt(j) < 58) {
-
- t = t + (int) b.charAt(j) - 48;
-
- } else {
-
- throw new FormatException(b + "第" + (j + 1) + "个字符不是数字");
-
- }
-
- }
- tmp = t / 10;
- nc[i] = (char) (t % 10 + 48);
- j--;
- }
- if (tmp > 0) {
- return tmp + new String(nc);
- } else {
- return new String(nc);
- }
-
- }
- }
-
- class FormatException extends RuntimeException {
- public FormatException(String string) {
- super(string);
- }
-
- /*03. 通过键盘输入一串小写字母(a~z)组成的字符串。请编写一个字符串过滤程序,若字符串中出现多个相同的字符,将非首次出现的字符过滤掉。
- 比如字符串“abacacde”过滤结果为“abcde”。
- 要求实现函数:
- void stringFilter(const char *pInputStr, long lInputLen, char *pOutputStr);
- 【输入】 pInputStr: 输入字符串
- lInputLen: 输入字符串长度
- 【输出】 pOutputStr: 输出字符串,空间已经开辟好,与输入字符串等长;
- */
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- public class Huawei03 {
- public static void main(String[] args) throws IOException {
- // TODO Auto-generated method stub
-
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- System.out.println("Please enter the string:");
- String str = br.readLine();
- str = str.trim();
-
- StringBuffer sb = new StringBuffer(str);
-
- for (int i = 0; i < sb.length(); i++) {
-
- for (int j = i + 1; j < sb.length(); j++) {
- if (sb.charAt(i) == (sb.charAt(j))) {
-
- sb.deleteCharAt(j);
- j--;
- }
- }
- }
- System.out.println(sb);
- }
- }
-
- /*04. 通过键盘输入一串小写字母(a~z)组成的字符串。请编写一个字符串压缩程序,将字符串中连续出席的重复字母进行压缩,并输出压缩后的字符串。
- 压缩规则:
- 1. 仅压缩连续重复出现的字符。比如字符串"abcbc"由于无连续重复字符,压缩后的字符串还是"abcbc".
- 2. 压缩字段的格式为"字符重复的次数+字符"。例如:字符串"xxxyyyyyyz"压缩后就成为"3x6yz"
- 要求实现函数:
- void stringZip(const char *pInputStr, long lInputLen, char *pOutputStr);
- 【输入】 pInputStr: 输入字符串
- lInputLen: 输入字符串长度
- 【输出】 pOutputStr: 输出字符串,空间已经开辟好,与输入字符串等长;
- */
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- public class Huawei04 {
- public static void main(String[] args) throws IOException {
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- System.out.println("Please enter the string:");
- String str = br.readLine();
- str = str.trim();
-
- StringBuffer sb = new StringBuffer(str);
- StringBuffer sb1 = new StringBuffer();
-
- int len = sb.length();
-
- String str1 = sb.toString() + " ";
-
- char[] chars = str1.toCharArray();
-
- int i;
- int count = 1;
-
- for (i = 0; i < len; i++) {
- if (chars[i] == chars[i + 1]) {
- count++;
- continue;
- }
- if (count != 0 && count != 1)
- sb1.append(count).append(chars[i]).toString();
- else
- sb1.append(chars[i]).toString();
- count = 1;
- }
- System.out.print(sb1);
- }
- }
-
- /*30. 计算重复字符个数
- 描述: 输入一行字符串。如果字符是英文字母,则输出为输入的英文字符+连续出现的次数 ,
- 例如 “ABBCCCC”-> “A1B2C4”;否则,丢弃该字符,不输出。
- 运行时间限制: 无限制
- 内存限制: 无限制
- 输入: 输入的字符串,长度小于1024
- 输出: ?输入的英文字符以及其重复的次数
- 样例输入: ABBC67%%%CCCAA99
- 样例输出: A1B2C4A2
- */
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- public class Huawei30 {
- public static void main(String[] args) throws IOException {
- // TODO Auto-generated method stub
-
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- System.out.println("Please enter the string:");
- String str = br.readLine();
- str = str.trim();
- int len = str.length();
-
- String[] str1 = new String[len];
- str1 = str.split("[^a-zA-Z]+");
- int len1 = str1.length;
- int i = 0;
-
- StringBuffer sb = new StringBuffer();
- StringBuffer sb1 = new StringBuffer();
- for (i = 0; i < len1; i++) {
- sb.append(str1[i]).toString();
-
- }
- sb.toString();
- String str2 = sb.toString();
-
- str2=str2+" ";
- int len2 = sb.length();
- char[] chars=str2.toCharArray();
- int counter = 1;
-
-
- for (i = 0; i < len2; i++) {
- if (chars[i] == chars[i + 1]) {
- counter++;
- continue;
- }
- sb1.append(chars[i]).append(counter).toString();
- counter = 1;
-
- }
- System.out.print(sb1);
- }
- }
-
-
- /*27.统计数字出现的次数,最大次数的统计出来
- 举例:
- 输入:323324423343
- 输出:3,6
- */
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- public class Huawei27 {
- public static void main(String[] args) throws IOException {
- // TODO Auto-generated method stub
-
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- System.out.println("Please enter the string:");
- String str = br.readLine();
- str = str.trim();
- int maxnum = 0;
- int temp = 0;
- int j;
- char[] chars = str.toCharArray();
- ArrayList<Character> list = new ArrayList<Character>();
- for (char c : chars) {
- if (!list.contains(c)) {
- list.add(c);
- }
- }
-
- int len = list.size();
- int lenc = chars.length;
- for (int i = 0; i < len; i++) {
- int count = 0;
- for (j = 0; j < lenc; j++) {
- if (list.get(i).equals(chars[j])) {
- count++;
- if (count > maxnum) {
- maxnum = count;
- temp = j;
-
- }
-
- }
- }
-
- }
- System.out.println(chars[temp] + "," + maxnum);
-
- }
-
- }
-
- /*07. 输入一串字符,只包含“0-10”和“,”找出其中最小的数字和最大的数字(可能不止一个),
- 输出最后剩余数字个数。如 输入 “3,3,4,5,6,7,7”
- */
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Scanner;
- public class Huawei07 {
- public static void main(String[] args) throws IOException {
- // TODO Auto-generated method stub
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- System.out.println("Please enter the string:");
- String str = br.readLine();
- str = str.trim();
- char[] chars = str.toCharArray();
-
- int len = chars.length;
-
- int array[] = new int[100];
- int count = 0;
- for (int i = 0; i < len; i++) {
- if (chars[i] >= '0' && chars[i] <= '9')
- array[count++] = chars[i] - '0';
-
- }
- int result = count;
- int min = array[0];
- int max = array[0];
- for (int j = 0; j < count; j++) {
- if (max < array[j])
- max = array[j];
- else if (min > array[j])
- min = array[j];
-
- }
- System.out.println("最大数是:" + max);
- System.out.println("最小数是:" + min);
- StringBuffer sb = new StringBuffer();
- for (int k = 0; k < count; k++) {
- if (array[k] == min)
- result--;
-
- if (array[k] == max)
- result--;
-
- }
- for (int k = 0; k < count; k++) {
- if (array[k] != min && array[k] != max)
- sb.append(array[k] + " ");
-
- }
- System.out.println("除去最大值与最小值,剩余数字个数为:" + result + "个");
- System.out.println("剩余的数字为:" + sb);
- }
- }
-
- /*9. 删除子串,只要是原串中有相同的子串就删掉,不管有多少个,返回子串个数。*/
- public class Huawei09 {
-
- public static void main(String[] args) {
- String str = "123abc12de1234fg1hi34j123k";
- String sub_str = "123";
-
- int count = 0;
- StringBuffer sb = new StringBuffer(str);
-
- // if (str.length() != str.replace(sub_str, "").length()) 判断字符串包含
-
- while (sb.indexOf(sub_str) != -1) {
-
- int a = sb.indexOf(sub_str);
- sb.delete(a, a + 3);
- count++;
-
- }
-
- System.out.println("包含子串个数为:" + count);
- System.out.println("去除子串后的字符串为:" + sb);
- }
- }
-
-
-
- /*14. 字串转换
- 问题描述:
- 将输入的字符串(字符串仅包含小写字母‘a’到‘z’),按照如下规则,循环转换后输出:a->b,b->c,…,y->z,z->a
- ;若输入的字符串连续出现两个字母相同时,后一个字母需要连续转换2次。
- 例如:aa 转换为 bc,zz 转换为 ab;当连续相同字母超过两个时,第三个出现的字母按第一次出现算。
- 要求实现函数:
- void convert(char *input,char* output)
- 【输入】 char *input , 输入的字符串
- 【输出】 char *output ,输出的字符串
- 【返回】 无
- */
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- public class Huawei14 {
- public static void main(String[] args) throws IOException {
- // TODO Auto-generated method stub
-
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- System.out.println("Please enter the string:");
- String str = br.readLine();
- str = str.trim();
-
- char[] input = str.toCharArray();
- char[] output = new char[256];
- int len = input.length;
- int i;
- int flag = 0;
- char temp = (char) ((input[0] - 'a' + 1) % 26 + 'a');
-
-
-
- for (i = 0; i < len; i++) {
- if (input[i]!=temp) {
- output[i] = (char) ((input[i] - 'a' + 1) % 26 + 'a');
- temp = input[i];
- flag = 1;
- } else
-
- if (flag == 1) {
- output[i] = (char) ((input[i] - 'a' + 2) % 26 + 'a');
- temp = input[i];
- flag = 0;
- } else {
- output[i] = (char) ((input[i] - 'a' + 1) % 26 + 'a');
- temp = input[i];
- flag = 1;
- }
-
- }
- System.out.print("变换后字符串为:");
- for(i=0;i<len;i++)
- System.out.print(" "+ output[i]);
-
- }
-
- }
-
-
- /*35给定一个字符串,把字符串内的字母转换成该字母的下一个字母,a换成b,z换成a,Z换成A,
- * 如aBf转换成bCg,字符串内的其他字符不改变,给定函数,编写函数
- void Stringchang(const char*inpu,char*output)
- 其中input是输入字符串,output是输出字符串
- */
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
-
- public class Huawei35 {
-
- public static void main(String[] args) throws IOException {
- // TODO Auto-generated method stub
-
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- System.out.println("Please enter the string:");
- String str = br.readLine();
-
- char[] chars = str.toCharArray();
-
- int len = chars.length;
- StringBuffer sb = new StringBuffer();
- char[] chars1 = new char[len];
- int i;
- for (i = 0; i < len; i++) {
- if (String.valueOf(chars[i]).matches("[a-zA-Z]+")) {
- if (chars[i] == 'z')
- chars1[i] = 'a';
- else if (chars[i] == 'Z')
- chars1[i] = 'A';
- else {
-
- chars1[i] = (char) (chars[i] + 1);
- }
-
- } else
- chars1[i] = chars[i];
- sb.append(chars1[i]).append(" ").toString();
- }
-
- System.out.println(sb.toString());
-
- }
-
- }
-
-
- /*29. 子串分离
- 题目描述:
- 通过键盘输入任意一个字符串序列,字符串可能包含多个子串,子串以空格分隔。请编写一
- 个程序,自动分离出各个子串,并使用’,’将其分隔,并且在最后也补充一个

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。