二〇二三 三月十五日

孤独数字

题目描述

在一组数中,仅有一个数字出现1次,其它数字都出现2次。只出现一次的数称为孤独的数字,你的任务是找出孤独的数字。
### 输入格式
输入数字有多组,每组数字以n开头,后面有n个数字。
### 输出格式
输出每组数字中的孤独数字。
### 样例输入
1
2
3 1 1 2
5 2 1 2 3 3

### 样例输出
1
2
2
1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import java.util.*;
public class Main
{
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
while(input.hasNext())
{
int N;
N = input.nextInt();
int s[] = new int[N];
int n[] = new int[100];
for(int i = 0; i < s.length; i++)
{
s[i] = input.nextInt();
}
for(int i = 0; i < s.length; i++)
{
n[s[i]]++;
}
for(int i = 0; i < n.length; i++)
{
if(n[i] == 1)
{
System.out.print(i + " ");
}
}
}
}
}

查找最大元素

题目描述

对于输入的每个字符串,查找其中最大的字母,在该字母后插入字符串”(max)”。
### 输入格式
输入数据包括多个测试实例,每个实例由一行长度不超过100的字符串组成,字符串仅由大小写字母及数字构成。
### 输出格式
对于每个测试实例输出一行字符串,输出的结果是插入字符串“(max)”后的结果,如果存在多个最大的字母,就在每一个最大字母后面都插入”(max)”。
### 样例输入
1
2
abcdefgfedcba
xxxxx

### 样例输出
1
2
abcdefg(max)fedcba
x(max)x(max)x(max)x(max)x(max)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
while(input.hasNext())
{
String str = input.next();
char max = 'A';
char[] chars = str.toCharArray();
for (char s : chars)
{
if(s>max)
{
max = s;
}
}
for (char s : chars)
{
if(s==max)
{
System.out.print(s+"(max)");
}
else
{
System.out.print(s);
}
}
}
}
}

绝对值排序

问题描述

输入n(n<=100)个整数,按照绝对值从大到小排序后输出。题目保证对于每一个测试实例,所有的数的绝对值都不相等。
### 输入格式
输入数据有多组,每组占一行,每行的第一个数字为n,接着是n个整数,n=0表示输入数据的结束,不做处理。
### 输出格式
对于每个测试实例,输出排序后的结果,两个数之间用一个空格隔开。每个测试实例占一行。
### 样例输入
1
2
3
3 3 -4 2
4 0 1 2 -3
0

### 样例输出
1
2
-4 3 2
-3 2 1 0

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import java.util.*;
public class Main
{
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
while(input.hasNext())
{
int n = input.nextInt();
if(n == 0) break;
int a[] = new int[n];

for(int i = 0; i < a.length; i++)
{
a[i] = input.nextInt();
}

for(int i = 0; i<a.length - 1; i++)
{
for(int j = i + 1; j < n; j++)
{
if(Math.abs(a[i]) < Math.abs(a[j]))
{
int x = a[j];
a[j] = a[i];
a[i] = x;
}
}
}

for(int j = 0; j < a.length; j++)
{
System.out.print(a[j]+" ");
}
System.out.println( );
}
}
}

二〇二三 三月十五日
http://example.com/2023/03/16/2023.3.15/
作者
oyxb_HT
发布于
2023年3月16日
更新于
2023年6月15日
许可协议