/*第一题的题目大概是输入整型数组求数组的最小数和最大数之和,例如输入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;ia[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 list = new ArrayList();
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 str3[n + 1]) {
temp = str3[n];
str3[n] = str3[n + 1];
str3[n + 1] = temp;
}
}
}
System.out.println();
for ( i=0;i< len3;i++)
System.out.print(str3[i]);
System.out.println();
int len4 = len2+len3;
int[] str4 = new int[len4];
int j=0,k=0;
for (i = 0; i < len4; i++) {
if(i % 2 == 0)
{
str4[i] = str2[j];
j++;
}
else
{
str4[i] = str3[k];
k++;
}
}
for (i = 0; i < len4; i++) {
System.out.print(str4[i] + " ");
}
}
}
/*38. 识别字符串中的整数并转换为数字形式(40分)
问题描述:
识别输入字符串中所有的整数,统计整数个数并将这些字符串形式的整数转换为数字形式整数。
要求实现函数:
void take_num(const char *strIn, int *n, unsigned int *outArray)
【输入】 strIn: 输入的字符串
【输出】 n: 统计识别出来的整数个数
outArray:识别出来的整数值,其中outArray[0]是输入字符串中从左到右第一个整数,
outArray[1]是第二个整数,以此类推。数组地址已经分配,可以直接使用
【返回】 无
注:
I、 不考虑字符串中出现的正负号(+, -),即所有转换结果为非负整数(包括0和正整数)
II、 不考虑转换后整数超出范围情况,即测试用例中可能出现的最大整数不会超过unsigned int可处理的范围
III、 需要考虑 '0' 开始的数字字符串情况,比如 "00035" ,应转换为整数35;
"000" 应转换为整数0;"00.0035" 应转换为整数0和35(忽略小数点:mmm.nnn当成两个数mmm和nnn来识别)
IV、 输入字符串不会超过100 Bytes,请不用考虑超长字符串的情况。
示例
输入:strIn = "ab00cd+123fght456-25 3.005fgh"
输出:n = 6
outArray = {0, 123, 456, 25, 3, 5}
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Huawei38 {
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();
String[] str1 = str.split("[^0-9]+");
int len = str1.length;
System.out.println();
for(int m=0;m len4)
System.out.println("null");
else
System.out.print(str2[num - 1]);
}
}
/*21.将第一行中含有第二行中“23”的数输出并排序
2.输入一行数字:123 423 5645 875 186523
在输入第二行:23
将第一行中含有第二行中“23”的数输出并排序
结果即:123 423 186523
*/
import java.util.Scanner;
public class Huawei21 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int i, j, temp;
int[] sort = new int[20];
int k = 0;
System.out.println("请输入一组数字,不超过20个:");
Scanner sc = new Scanner(System.in);
String inputString = sc.next().toString();
String stringArray[] = inputString.split(",");
System.out.println("请输入检测数字:");
String inputStrin1 = sc.next().toString();
sc.close();
int len = stringArray.length;
for (i = 0; i < len; i++) {
StringBuffer sb = new StringBuffer(stringArray[i]);
if (sb.indexOf(inputStrin1) != -1) {
sort[k] = Integer.parseInt(stringArray[i]);
k++;
}
}
for (i = 0; i < k - 1; i++) {
for (j = i + 1; j < k; j++) {
if (sort[i] > sort[j]) { // 交换两数的位置
temp = sort[i];
sort[i] = sort[j];
sort[j] = temp;
}
}
}
System.out.print("结果为:");
for (i = 0; i < k; i++) {
System.out.print(" " + sort[i]);
}
}
}
/*22输入m个字符串 和一个整数n, 把字符串M化成以N为单位的段,不足的位数用0补齐。
如 n=8 m=9 ,
123456789划分为: 12345678
90000000
123化为 :12300000
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class Huawei22 {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
int i, j = 0;
char[] c = new char[200];
System.out.println("请输入字符串:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
str = str.trim();
int len = str.length();
System.out.println("请输入单位n:");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
sc.close();
c = str.toCharArray();
for (i = 1; i <= len; i++) {
j = i % n;
System.out.print(c[i - 1]);
if (j == 0)
System.out.println();
}
if (j != 0) {
for (i = j + 1; i <= n; i++)
System.out.print("0");
}
}
}
/*8.输入一组身高在170到190之间(5个身高),比较身高差,选出身高差最小的两个身高;
若身高差相同,选平均身高高的那两个身高;从小到大输出;
如 输入 170 181 173 186 190 输出 170 173
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class Huawei08 {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
int[] Height = new int[5];
int dmin;
int H1, H2;
int i, j, temp;
System.out.println("请输入一组身高在170到190之间的数据(共5个):");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
String stringArray[] = str.split(" ");
System.out.println("您输入的5个身高数据为:");
for (i = 0; i < stringArray.length; i++) {
System.out.print(stringArray[i] + " ");
}
System.out.println();
System.out.println("转换为整形数组:");
for (int k = 0; k < 5; k++) {
Height[k] = Integer.parseInt(stringArray[k]);
System.out.print(Height[k] + " ");
}
for (i = 0; i < 5; i++) {
for (j = i; j < 5; j++) {
if (Height[i] > Height[j]) {
temp = Height[i];
Height[i] = Height[j];
Height[j] = temp;
}
}
}
System.out.println();
System.out.println("排序后输出:");
for (int k = 0; k < 5; k++) {
System.out.print(Height[k] + " ");
}
H1 = Height[0];
H2 = Height[1];
dmin = H2 - H1;
for (int m = 2; m < 5; m++) {
if (Height[m] - Height[m - 1] <= dmin) {
H1 = Height[m - 1];
H2 = Height[m];
dmin = Height[m] - Height[m - 1];
}
}
System.out.println();
System.out.println("身高差最小的两个身高为:");
System.out.println(H1 + " " + H2);
}
}
/*11. 描述:10个学生考完期末考试评卷完成后,A老师需要划出及格线,要求如下:
(1) 及格线是10的倍数;
(2) 保证至少有60%的学生及格;
(3) 如果所有的学生都高于60分,则及格线为60分
输入:输入10个整数,取值0~100
输出:输出及格线,10的倍数
*/
import java.util.Scanner;
public class Huawei11 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] a = new int[10];
int pass;
int temp; // 记录临时中间值
int size = 10; // 数组大小
System.out.println("请随机输入10个成绩(0-100):");
Scanner sc = new Scanner(System.in);
String inputString = sc.next().toString();
String stringArray[] = inputString.split(",");
for (int k = 0; k < 10; k++) {
a[k] = Integer.parseInt(stringArray[k]);
System.out.print(a[k] + " ");
}
System.out.println();
for (int i = 0; i < size - 1; i++) {
for (int j = i + 1; j < size; j++) {
if (a[i] > a[j]) { // 交换两数的位置
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
for (int k = 0; k < 10; k++) {
System.out.print(a[k] + " ");
}
if (a[0] >= 60)
System.out.println("及格线为:60");
else {
pass = (((int) a[4] / 10) * 10);
System.out.println("及格线为:" + pass);
}
}
}
/*12. 描述:一条长廊里依次装有n(1 ≤ n ≤ 65535)盏电灯,从头到尾编号1、2、3、…n-1、n。
每盏电灯由一个拉线开关控制。开始,电灯全部关着。
有n个学生从长廊穿过。第一个学生把号码凡是1的倍数的电灯的开关拉一下;
接着第二个学生把号码凡是2的倍数的电灯的开关拉一下;接着第三个学生把号码凡是3的倍数的电灯的开关拉一下;
如此继续下去,最后第n个学生把号码凡是n的倍数的电灯的开关拉一下。n个学生按此规定走完后,长廊里电灯有几盏亮着。
注:电灯数和学生数一致。
输入:电灯的数量
输出:亮着的电灯数量
样例输入:3
样例输出:1
*/
import java.util.Scanner;
public class Huawei12 {
private static int GetLightLampNum(int n) {
int ret = 0;
boolean[] flag = new boolean[n];
// 初始化电灯,全部关着
for (int i = 0; i < n; i++) {
flag[i] = false;
}
// 拉灯操作,true为亮,灭为false
for (int i = 1; i <= n; i++) {
for (int j = i; j <= n; j++) {
if (0 == j % i) {
flag[j - 1] = !flag[j - 1];
}
}
}
// 找出所有亮的灯
for (int i = 0; i < n; i++) {
if (flag[i]) {
ret++;
}
}
return ret;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int n, result;
System.out.println("请输入灯的数量(1-65535):");
Scanner input = new Scanner(System.in);
n = input.nextInt();
input.close();
if ((n < 1) || (n > 65535)) {
System.out.println("输入数值超出范围!");
} else {
result = GetLightLampNum(n);
System.out.println("最后亮灯的数量为:" + result);
}
}
}
/*16. 数组中数字都两两相同,只有一个不同,找出该数字:
异或运算的性质:任何一个数字异或它自己都等于0
因为其他数字都出现了两次,在异或中全部抵消掉了。
算法步骤:1,对1-n个数做异或运算,得到XOR = 1^2^3^4....^n。
2, 用XOR与当前n-1数组的所有元素依次取异或:
因为XOR中与当前数字相同的数,都在异或运算中抵消掉了,最终剩下的,就是我们要找的那个丢失的数。
所谓抵消:举个例子: 1,1,2
1: 0001
2: 0010 1^1=0000
1^1^2 ===== 0000 ^ 0010 = 0010=2,也就是说两个1抵消了。
任何一个数字异或它自己都等于0,而0与任何数字求异或,都为原数字!
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class Huawei16 {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
System.out.println("请输入整数:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
int len = str.length();
String[] chars = str.split("");
int[] a = new int[len];
for (int i = 0; i < len; i++) {
int num = Integer.parseInt(chars[i]);
a[i]=num;
}
int temp = a[0];
for (int j = 1; j < len; j++) {
temp ^= a[j];
}
System.out.println(temp);
}
}
/*17. 题目二:数组中数字两两相同,有两个不同,找出这两个:*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Huawei17 {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
System.out.println("请输入整数:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
int len = str.length();
String[] chars = str.split("");
int[] a = new int[len];
for (int i = 0; i < len; i++) {
int num = Integer.parseInt(chars[i]);
a[i] = num;
}
int result = 0;
for (int i = 0; i < a.length; i++) {// 异或结束相当于两个数异或的结果
result ^= a[i];
}
int count = 1;
int b = 1;
while (true) {
if ((result & b) == 1) {
break;
}
result = result >> 1;
count = count << 1;
}
int num1 = 0;
int num2 = 0;
for (int i = 0; i < a.length; i++) {
if ((a[i] & count) == 0) {
num1 ^= a[i];
} else {
num2 ^= a[i];
}
}
System.out.println("num1=" + num1 + " num2=" + num2);
}
}
/*05. 通过键盘输入100以内正整数的加、减运算式,请编写一个程序输出运算结果字符串。
输入字符串的格式为:“操作数1 运算符 操作数2”,“操作数”与“运算符”之间以一个空格隔开。
补充说明:
1. 操作数为正整数,不需要考虑计算结果溢出的情况。
2. 若输入算式格式错误,输出结果为“0”。
要求实现函数:
void arithmetic(const char *pInputStr, long lInputLen, char *pOutputStr);
【输入】 pInputStr: 输入字符串
lInputLen: 输入字符串长度
【输出】 pOutputStr: 输出字符串,空间已经开辟好,与输入字符串等长;
【注意】只需要完成该函数功能算法,中间不需要有任何IO的输入输出
示例
输入:“4 + 7” 输出:“11”
输入:“4 - 7” 输出:“-3”
输入:“9 ++ 7” 输出:“0” 注:格式错误
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Huawei05 {
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();
int len = str.length();
char[] chars = str.toCharArray();
int len1 = chars.length;
char ch;
int index1 = 0;
int index2 = 0;
int index3 = 0;
char[] op1 = new char[10];
char[] op = new char[10];
char[] op2 = new char[10];
int i, j;
int cnt = 0;
for (i = 0; i < len1; i++) {
ch = chars[i];
if (!Character.isDigit(ch) && ch != ' ' && ch != '+' && ch != '-' && ch !='*' && ch != '/')
return;
}
for (j = 0; j < len1; j++) {
ch = chars[j];
if (ch == ' ') {
cnt++;
if (cnt > 2) {
System.out.println("too much space!");
return;
}
continue;
}
if (cnt == 0) {
op1[index1++] = ch;
} else if (cnt == 1) {
op[index2++] = chars[j];
if (index2 > 1) {
System.out.println("0");
return;
}
} else if (cnt == 2) {
op2[index3++] = chars[j];
}
}
int iop1 = Integer.parseInt(String.valueOf(op1[0]));
int iop2 = Integer.parseInt(String.valueOf(op2[0]));
;
int temp = 0;
StringBuffer sb = new StringBuffer();
if (op[0] == '*') {
temp = iop1 * iop2;
sb.append(temp).toString();
} else if (op[0] == '/') {
temp = iop1 / iop2;
sb.append(temp).toString();
} else if (op[0] == '+') {
temp = iop1 + iop2;
sb.append(temp).toString();
} else if (op[0] == '-') {
if (iop1 > iop2) {
temp = iop1 - iop2;
sb.append(temp).toString();
} else {
temp = iop2 - iop1;
sb.append("-").append(temp).toString();
}
}
for (i = 1; i < len; i++) {
sb.append(" ").toString();
}
String str1 = sb.toString();
System.out.println(str1);
}
}
/*18. 链表翻转。给出一个链表和一个数k,比如链表1→2→3→4→5→6,k=2,
则翻转后2→1→4→3→6→5,若k=3,翻转后3→2→1→6→5→4,若k=4,翻转后4→3→2→1→5→6,用程序实现
思想:采用遍历链表,分成length/k组,对每组进行逆转,逆转的同时要将逆转后的尾和头连接起来
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Huawei18 {
private Node firstNode;
private int length;
public Huawei18() {
clear();
}
public final void clear() {
firstNode = null;
length = 0;
}
public boolean add(T newEntry) {
Node newNode = new Node(newEntry);
if (isEmpty())
firstNode = newNode;
else {
Node lastNode = getNodeAt(length);
lastNode.next = newNode;
}
length++;
return true;
}
public boolean add(int newPosition, T newEntry) {
boolean isSuccessful = true;
if ((newPosition >= 1) && (newPosition <= length + 1)) {
Node newNode = new Node(newEntry);
if (isEmpty() || (newPosition == 1)) {
newNode.next = firstNode;
firstNode = newNode;
} else {
Node nodeBefore = getNodeAt(newPosition - 1);
Node nodeAfter = nodeBefore.next;
newNode.next = nodeAfter;
nodeBefore.next = newNode;
}
length++;
} else
isSuccessful = false;
return isSuccessful;
}
private Node getNodeAt(int givenPosition) {
assert !isEmpty() && (1 <= givenPosition) && (givenPosition <= length);
Node currentNode = firstNode;
for (int counter = 1; counter < givenPosition; counter++) {
currentNode = currentNode.next;
}
assert currentNode != null;
return currentNode;
}
public boolean isEmpty() {
boolean result;
if (length == 0) {
assert firstNode == null;
result = true;
} else {
assert firstNode != null;
result = false;
}
return result;
}
public void display() {
Node currentNode = firstNode;
while (currentNode != null) {
System.out.print(currentNode.data + " ");
currentNode = currentNode.next;
}
}
public Object remove(int givenPosition) {
Object result = null;
if (!isEmpty() && (1 <= givenPosition) && (givenPosition <= length)) {
if (givenPosition == 1) {
result = firstNode.data;
firstNode = firstNode.next;
} else {
Node nodeBefore = getNodeAt(givenPosition);
Node nodeToRemove = nodeBefore.next;
Node nodeAfter = nodeToRemove.next;
nodeBefore.next = nodeAfter;
result = nodeToRemove.data;
}
length--;
}
return result;
}
public boolean replace(int givenPosition, T newEntry) {
boolean isSuccessful = true;
if (!isEmpty() && (1 <= givenPosition) && (givenPosition <= length)) {
Node desiredNode = getNodeAt(givenPosition);
desiredNode.data = newEntry;
} else
isSuccessful = false;
return isSuccessful;
}
public T getEntry(int givenPosition) {
T result = null;
if (!isEmpty() && (1 <= givenPosition) && (givenPosition <= length)) {
result = getNodeAt(givenPosition).data;
}
return result;
}
public boolean contains(T anEntry) {
boolean found = false;
Node currentNode = firstNode;
while (!found && (currentNode != null)) {
if (anEntry.equals(currentNode.data)) {
found = true;
} else {
currentNode = currentNode.next;
}
}
return found;
}
private class Node {
private T data;
private Node next;
private Node(T dataPortion) {
data = dataPortion;
next = null;
}
private Node(T dataPortion, Node nextNode) {
data = dataPortion;
next = nextNode;
}
}
public Node reverse(Node head) {
if (head == null)
return null;
Node p, q, t = null;
p = head;
q = p.next;
if (q != null)
t = q.next;
// 把头结点设置成尾节点
head.next = null;
while (q != null) {
q.next = p;
p = q;
q = t;
if (t != null)
t = t.next;
}
return p;
}
public void reverseKGroup(int k) {
if (isEmpty()) {
System.out.println("链表为空!");
} else {
Node p;
Node firstNode1, nextfirstNode;// 反转后的子链表的头指针和下一段链表的头指针,供反转后连接母链表时使用
Node headNode = firstNode;// 待翻转的子链表的头指针
Node preTail = null;// 指向上一段链表的尾节点
p = firstNode;
int count = 0;
while (p != null) {
if (count == 0)
headNode = p;
count++;
if (count == k) {
count = 0;
nextfirstNode = p.next;
p.next = null;
firstNode1 = reverse(headNode);
headNode.next = nextfirstNode;// 连接上母链表,现在headNode指向的是尾节点
if (preTail != null)
preTail.next = firstNode1;
if (preTail == null)
firstNode = firstNode1;
preTail = headNode;// 指向本次被翻转的链表的尾节点,作为下一次的上一段链表的尾节点
p = preTail.next;
continue;
}
p = p.next;
}
}
}
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Huawei18 myList = new Huawei18<>();
System.out.println("请输入:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
int len = str.length();
String[] str1 = new String[len];
str1 = str.split(" ");
for (int i = 0; i < str1.length; i++) {
myList.add(str1[i]);
}
myList.display();
System.out.println();
myList.display();
System.out.println();
myList.reverseKGroup(3);
myList.display();
}
/*19. 链表相邻元素翻转,如a->b->c->d->e->f-g,翻转后变为:b->a->d->c->f->e->g*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Huawei19 {
private Node firstNode;
private int length;
public Huawei19() {
clear();
}
public final void clear() {
firstNode = null;
length = 0;
}
public boolean add(T newEntry) {
Node newNode = new Node(newEntry);
if (isEmpty())
firstNode = newNode;
else {
Node lastNode = getNodeAt(length);
lastNode.next = newNode;
}
length++;
return true;
}
public boolean add(int newPosition, T newEntry) {
boolean isSuccessful = true;
if ((newPosition >= 1) && (newPosition <= length + 1)) {
Node newNode = new Node(newEntry);
if (isEmpty() || (newPosition == 1)) {
newNode.next = firstNode;
firstNode = newNode;
} else {
Node nodeBefore = getNodeAt(newPosition - 1);
Node nodeAfter = nodeBefore.next;
newNode.next = nodeAfter;
nodeBefore.next = newNode;
}
length++;
} else
isSuccessful = false;
return isSuccessful;
}
private Node getNodeAt(int givenPosition) {
assert !isEmpty() && (1 <= givenPosition) && (givenPosition <= length);
Node currentNode = firstNode;
for (int counter = 1; counter < givenPosition; counter++) {
currentNode = currentNode.next;
}
assert currentNode != null;
return currentNode;
}
public boolean isEmpty() {
boolean result;
if (length == 0) {
assert firstNode == null;
result = true;
} else {
assert firstNode != null;
result = false;
}
return result;
}
public void display() {
Node currentNode = firstNode;
while (currentNode != null) {
System.out.print(currentNode.data + " ");
currentNode = currentNode.next;
}
}
public Object remove(int givenPosition) {
Object result = null;
if (!isEmpty() && (1 <= givenPosition) && (givenPosition <= length)) {
if (givenPosition == 1) {
result = firstNode.data;
firstNode = firstNode.next;
} else {
Node nodeBefore = getNodeAt(givenPosition);
Node nodeToRemove = nodeBefore.next;
Node nodeAfter = nodeToRemove.next;
nodeBefore.next = nodeAfter;
result = nodeToRemove.data;
}
length--;
}
return result;
}
public boolean replace(int givenPosition, T newEntry) {
boolean isSuccessful = true;
if (!isEmpty() && (1 <= givenPosition) && (givenPosition <= length)) {
Node desiredNode = getNodeAt(givenPosition);
desiredNode.data = newEntry;
} else
isSuccessful = false;
return isSuccessful;
}
public T getEntry(int givenPosition) {
T result = null;
if (!isEmpty() && (1 <= givenPosition) && (givenPosition <= length)) {
result = getNodeAt(givenPosition).data;
}
return result;
}
public boolean contains(T anEntry) {
boolean found = false;
Node currentNode = firstNode;
while (!found && (currentNode != null)) {
if (anEntry.equals(currentNode.data)) {
found = true;
} else {
currentNode = currentNode.next;
}
}
return found;
}
private class Node {
private T data;
private Node next;
private Node(T dataPortion) {
data = dataPortion;
next = null;
}
private Node(T dataPortion, Node nextNode) {
data = dataPortion;
next = nextNode;
}
}
public Node reverse(Node head) {
if (head == null)
return null;
Node p, q, t = null;
p = head;
q = p.next;
if (q != null)
t = q.next;
// 把头结点设置成尾节点
head.next = null;
while (q != null) {
q.next = p;
p = q;
q = t;
if (t != null)
t = t.next;
}
return p;
}
public void reverseKGroup(int k) {
if (isEmpty()) {
System.out.println("链表为空!");
} else {
Node p;
Node firstNode1, nextfirstNode;// 反转后的子链表的头指针和下一段链表的头指针,供反转后连接母链表时使用
Node headNode = firstNode;// 待翻转的子链表的头指针
Node preTail = null;// 指向上一段链表的尾节点
p = firstNode;
int count = 0;
while (p != null) {
if (count == 0)
headNode = p;
count++;
if (count == k) {
count = 0;
nextfirstNode = p.next;
p.next = null;
firstNode1 = reverse(headNode);
headNode.next = nextfirstNode;// 连接上母链表,现在headNode指向的是尾节点
if (preTail != null)
preTail.next = firstNode1;
if (preTail == null)
firstNode = firstNode1;
preTail = headNode;// 指向本次被翻转的链表的尾节点,作为下一次的上一段链表的尾节点
p = preTail.next;
continue;
}
p = p.next;
}
}
}
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Huawei19 myList = new Huawei19<>();
System.out.println("请输入:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
int len = str.length();
String[] str1 = new String[len];
str1 = str.split(" ");
for (int i = 0; i < str1.length; i++) {
myList.add(str1[i]);
}
myList.display();
System.out.println();
myList.display();
System.out.println();
myList.reverseKGroup(2);
myList.display();
}
}
/*23将 电话号码 one two 。。。nine zero
翻译成1 2 。。9 0
中间会有double
例如输入:OneTwoThree
输出:123
输入:OneTwoDoubleTwo
输出:1222
输入:1Two2 输出:ERROR
输入:DoubleDoubleTwo 输出:ERROR
有空格,非法字符,两个Double相连,Double位于最后一个单词 都错误
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Huawei23 {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
String[] words = new String[] { "zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine", "double" };
int[] wordslen = new int[11];
StringBuffer sb1 = new StringBuffer();
String s1 = null;
int d = 0;
String tt = new String();
String[] result = new String[100];
String s = null;
StringBuffer aa = new StringBuffer();
for (int n = 0; n < 11; n++) {
wordslen[n] = words[n].length();
}
System.out.println("请输入英文电话号码:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
str = str.toLowerCase();
int lenStr1 = str.length();
str = str.replaceAll(" ", "");
int lenStr2 = str.length();
StringBuffer sb = new StringBuffer(str);
int now = 0, r, n = 0;
if ((lenStr1 != lenStr2) || !str.matches("[a-zA-Z]+"))
System.out.println("error");
else {
while (now < lenStr2) {
tt = sb.substring(now) + " ";
for (n = 0; n < 11; n++) {
if (tt.substring(0, wordslen[n]).equals(words[n])) {
s = aa.append(words[n]).append(",").toString();
now = now + wordslen[n];
break;
}
}
}
result = s.split(",");
int m1=result.length;
int f=0;
for (int i = 0; i 1 || f==1) {
System.out.println("error");
} else if(d<=1 && f==0) {
System.out.println(s1);
}
}
}
}
/*39将中文数字的拼音字符串转为最终的数字;每个字拼音的首字母大写,
比如:JiuWanJiuQianJiuBaiJiuShiJiu表示九万九千九百九十九,即阿拉伯数字,。
你当然记得每个数字的汉语拼音,但是还是提示你一下,
Ling、Yi、Er、San、Si、Wu、Liu、Qi、Ba、Jiu、Shi、Bai、Qian、Wan。
为简单起见,我们要处理的数字在万以内,不含负数,十、百、千、万等单位前面一定有数字,如YiShi表示。
输入: 中文数字的拼音字符串
输出: 阿拉伯数字
样例输入: SanBaiLingSan
样例输出: 303
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Huawei39 {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
String[] words = new String[] { "Shi", "Bai", "Qian", "Wan", "Ling",
"Yi", "Er", "San", "Si", "Wu", "Liu", "Qi", "Ba", "Jiu" };
int[] wordslen = new int[14];
int n;
for (n = 0; n < 14; n++) {
wordslen[n] = words[n].length();
}
System.out.println("请输入拼音:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
str = str.toLowerCase();
int lenStr = str.length();
StringBuffer sb = new StringBuffer(str);
StringBuffer sb1 = new StringBuffer();
String newString = new String();
String s = new String();
int now = 0;
// while循环把我输入的字符串就找出来了
while (now < lenStr) {
newString = sb.substring(now) + " ";
for (n = 0; n < 14; n++) {
if (newString.substring(0, wordslen[n]).equalsIgnoreCase(
words[n])) {
s = sb1.append(words[n]).append(",").toString();
now = now + wordslen[n];
break;
}
}
}
// System.out.print(s);
String[] result = new String[100];
result = s.split(",");
int resultLen = result.length;
int r = 0;
int pos = 0;
int num = 0;
for (int i = 0; i < resultLen; i++) {
for (int j = 0; j < 14; j++) {
if (result[i].equals(words[j]) && j > 3 && j < 14) {
r = j - 4;
} else if (result[i].equals(words[j]) && j >= 0 && j <= 3) {
if (words[j] == "Shi")
pos = r * 10;
if (words[j] == "Bai")
pos = r * 100;
if (words[j] == "Qian")
pos = r * 1000;
if (words[j] == "Wan")
pos = r * 10000;
}
num = num + pos;
pos = 0;
}
}
num += r;
System.out.println(num);
}
}
/*24.将整数倒序输出,剔除重复数据
输入一个整数,如12336544,或1750,然后从最后一位开始倒过来输出,最后如果是0,
则不输出,输出的数字是不带重复数字的,所以上面的输出是456321和571。
如果是负数,比如输入-175,输出-571。
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Huawei24 {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
System.out.println("请输入一个整数:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
StringBuffer sb = new StringBuffer(str);
int len = sb.length();
if (sb.charAt(len - 1) =='0') {
sb.deleteCharAt(len - 1);
}
String sb1 = null;
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--;
}
}
}
if (sb.charAt(0) == '-') {
sb1 = sb.substring(1);
StringBuffer sb2 = new StringBuffer(sb1);
// System.out.println();
System.out.print("-" + sb2.reverse());
} else {
System.out.println(sb.reverse());
}
}
}
/*25.编程的时候,if条件里面的“(”、“)”括号经常出现不匹配的情况导致编译不过,
* 请编写程序检测输入一行if语句中的圆括号是否匹配正确。
* 同时输出语句中出现的左括号和右括号数量,
* 如if((a==1)&&(b==1))是正确的,
* 而if((a==1))&&(b==1))是错误的。
* 注意if语句的最外面至少有一对括号。提示:用堆栈来做。
输入:if((a==1)&&(b==1))
输出:RIGTH 3 3
输入:if((a==1))&&(b==1))
输出:WRONG 3 4
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Huawei25 {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("请属于检测的if语句:");
String str = br.readLine();
int len = str.length();
char[] str1 = str.toCharArray();
int[] a = new int[800];
int flag = 1, k = 0, left = 0, right = 0;
for (int i = 0; i < len; i++) {
if (str1[i] == '(') {
left++;
a[k] = 1;
k++;
} else if (str1[i] == ')') {
right++;
if (a[k - 1] == 1 && k > 0) {
a[k - 1] = 0;
k--;
}
else
flag=0;
}
if((i==2 && str1[i]!='(')||(i==len-1 && str1[i]!=')'))
flag=0;
}
if(a[0]==0 && flag!=0)
System.out.println("RIGHT!");
else
System.out.println("WRONG!");
System.out.println("Left: "+ left);
System.out.println("Right: "+ right);
}
}
/*6.一组人(n个),围成一圈,从某人开始数到第三个的人出列,再接着从下一个人开始数,最终输出最终出列的人
(约瑟夫环是一个数学的应用问题:已知n个人(以编号1,2,3...n分别表示)
围坐在一张圆桌周围。从编号为k的人开始报数,数到m的那个人出列;他的下一个人又从1开始报数,
数到m的那个人又出列;依此规律重复下去,直到圆桌周围的人全部出列。)
*/
public class Huawei06 {
public static void main(String[] args) {
// TODO Auto-generated method stub
// 总共有M个人
int n = 9;
// 数到N的人出列
int m = 3;
// 开始报数的位置
int k = 1;
// 初始数组长度,以后出圈一个长度减1
int len = n;
int[] array = new int[n];
// 数组赋值
for (int i = 0; i < n; i++) {
array[i] = i + 1;
}
int i = 0; // 数组下标
int j = 1; // 当前要报的数
while (len > 0) {
if (array[i % n] > 0) {
if (j % m == 0) {// 找到出圈的人了,输出
System.out.print(array[i % n] + " ");
array[i % n] = -1;
j = 1;
i++;
len--;
} else {
i++;
j++;
}
} else {
// 遇到已经出圈的数字,直接跳过了
i++;
}
}
}
}
/*26. 约瑟夫问题
输入一个由随机数组成的数列(数列中每个数均是大于0的整数,长度已知),和初始计数值m。从数列首位置开始计数,计数到m后,
将数列该位置数值替换计数值m,并将数列该位置数值出列,然后从下一位置从新开始计数,直到数列所有数值出列为止。
如果计数到达数列尾段,则返回数列首位置继续计数。请编程实现上述计数过程,同时输出数值出列的顺序
比如:输入的随机数列为:3,1,2,4,初始计数值m=7,从数列首位置开始计数(数值3所在位置)
第一轮计数出列数字为2,计数值更新m=2,出列后数列为3,1,4,从数值4所在位置从新开始计数
第二轮计数出列数字为3,计数值更新m=3,出列后数列为1,4,从数值1所在位置开始计数
第三轮计数出列数字为1,计数值更新m=1,出列后数列为4,从数值4所在位置开始计数
最后一轮计数出列数字为4,计数过程完成。
输出数值出列顺序为:2,3,1,4。
要求实现函数:
void array_iterate(int len, int input_array[], int m, int output_array[])
【输入】 int len:输入数列的长度;
int intput_array[]:输入的初始数列
int m:初始计数值
【输出】 int output_array[]:输出的数值出列顺序
【返回】 无
示例:
输入:int input_array[] = {3,1,2,4},int len = 4, m=7
输出:output_array[] = {2,3,1,4}
解题思路:
每次出列一个数值,需要对m、input_array、output_array、输出位置outPos、起始位置startPos进行更新;
对于输出位置outPos的计算是关键!通过分析可知,outPos=(startPos+m-1)%num
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Huawei26 {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("请随机输入一个整形数列:");
String str = br.readLine();
System.out.println("请输入一个整数:");
String str1 = br.readLine();
String[] s1 = str.split(",");
int len = s1.length;
int[] array = new int[len];
int i=0,j=1;
for(int k=0;k 0) {
if (array[i % n] > 0) {
if (j % m == 0) {// 找到出圈的人了,输出
System.out.print(array[i % n] + " ");
m = array[i % n];
array[i % n] = -1;
j = 1;
i++;
len--;
} else {
i++;
j++;
}
} else {
// 遇到已经出圈的数字,直接跳过了
i++;
}
}
}
}
/*31. 按字母序输出定长排列组合。输入“ABC”,定长为2,则输出
AB AC BC
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Huawei31 {
public static void perm(char[] buf, int start, int end) {
if (start == end) {// 当只要求对数组中一个字母进行全排列时,只要就按该数组输出即可
for (int i = 0; i <= end; i++) {
System.out.print(buf[i]);
}
System.out.println();
} else {// 多个字母全排列
for (int i = start; i <= end; i++) {
char temp = buf[start];// 交换数组第一个元素与后续的元素
buf[start] = buf[i];
buf[i] = temp;
perm(buf, start + 1, end);// 后续元素递归全排列
temp = buf[start];// 将交换后的数组还原
buf[start] = buf[i];
buf[i] = temp;
}
}
}
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
System.out.println("请输入字符串:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
int len = str.length();
int m;
String[] str1 = str.split(" ");
char[] chars = str1[0].toCharArray();
m = Integer.parseInt(str1[1]);
perm(chars, 0, m);
}
}
/*33:输入若干整数,输出其中能被这些整数中其他元素整除的那些元素
描述
输入一组大于0小于1000的整数,且均不相同,逗号隔开,输出其中能被这些整数中其他元素整除的那些元素。
输入输出格式要求
输入要求同上述描述,输出要求整数顺序按照输入时的顺序输出。
样例
输入:2,4,6,8,10,12,3,9
输出:4,6,8,10,12,9
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Huawei33 {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入一组整数,以,隔开:");
String str = br.readLine();
int len = str.length();
String[] str1 = new String[len];
str1 = str.split(",");
int len1 = str1.length;
int[] str2 = new int[len1];
int[] str3 = new int[len1];
StringBuffer sb = new StringBuffer();
int i, j;
for (i = 0; i < len1; i++) {
str2[i] = Integer.parseInt(str1[i]);
str3[i] = str2[i];
}
int count = 0;
for (i = 0; i < len1; i++) {
for (j = 0; j < len1; j++) {
if ((str2[i] % str3[j] == 0)) {
count++;
}
}
if(count >= 2)
{
sb.append(str3[i]).append(",").toString();
count=0;
}
}
int len4=sb.length();
sb.deleteCharAt(len4-1);
System.out.print(sb.toString());
}
}
/* 37. 给定一个数组input[] ,如果数组长度n为奇数,则将数组中最大的元素放到 output[] 数组最中间的位置,
* 如果数组长度n为偶数,则将数组中最大的元素放到 output[] 数组中间两个位置偏右的那个位置上,
* 然后再按从大到小的顺序,依次在第一个位置的两边,按照一左一右的顺序,依次存放剩下的数。
例如:input[] = {3, 6, 1, 9, 7} output[] = {3, 7, 9, 6, 1};
input[] = {3, 6, 1, 9, 7, 8} output[] = {1, 6, 8, 9, 7, 3}
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Huawei37 {
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();
String[] str1 = str.split(",");
int len = str1.length;
int[] array = new int[len];
int[] output = new int[len];
int i, j, m, n = 1, temp;
for (i = 0; i < len; i++) {
array[i] = Integer.parseInt(str1[i]);
}
for (i = 0; i < len; i++) {
for (j = i; j < len; j++) {
if (array[i] < array[j]) {
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
m = len / 2;
output[m] = array[0];
if (len % 2 != 0) {
for (i = 1; i <= m; i++) {
output[m - i] = array[n++];
output[m + i] = array[n++];
}
} else if (len % 2 == 0){
for (i = 1; i < m; i++) {
output[m - i] = array[n++];
output[m + i] = array[n++];
}
output[0] = array[n];
}
for (i = 0; i < len; i++) {
System.out.print(output[i] + " ");
}
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/*36.比较一个数组的元素 是否为回文数组 */
public class Huawei36 {
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;
int i, j = 0;
for (i = 0; i < len / 2; i++) {
if (chars[i] != chars[len - 1 - i]) {
System.out.println("No");
j = 1;
break;
}
}
if (j == 0)
System.out.println("Yes");
}
}
/*输入一行字符串,将其中的数字与字母分离,并将数字放到字母后面*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String stdin = br.readLine();
int len = stdin.length();
String[] s1 = stdin.split("[a-zA-Z]+");
int i;
int len_s1 = s1.length;
StringBuffer sb = new StringBuffer();
StringBuffer sb1 = new StringBuffer();
for(i=0;i