字符串:
import java.util.Scanner;
public class Main{
public static void main(String [] args){
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int m = input.nextInt();
String[]a = new String[n+1];
for(int i=1 ;i<=n ;i++){
a[i] = input.next();
}
for(int i=1 ;i<=m ;i++){
String temp = a[1];
for(int j=1 ;j<n ;j++){
if(a[j].compareTo(a[j+1])>0){
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
for(int i=1 ;i<=n ;i++){
System.out.println(a[i]);
}
}
}
英语:
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String str = in.nextLine();
String strs[] = str.split(" ");
StringBuffer restr = new StringBuffer();
for (int i = strs.length - 1; i >= 0; i--) {
restr.append(strs[i] + " ");
}
System.out.println(restr);
}
}
IP地址:
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String a = input.nextLine();
int[] b = new int[4];
int[] c = new int[4];
for(int i =0;i<4;i++)
{
c[i] = Integer.parseInt(a.substring((i+1)*8-8, (i+1)*8));
}
for(int i =0;i<4;i++)
{
for(int j = 0;j<8;j++)
{
b[i]+=Math.pow(2, j)*(c[i]%10);
c[i] /= 10;
}
if(i!=3) {
System.out.print(b[i]+".");
} else {
System.out.print(b[i]);
}
}
input.close();
}
}
黑洞数
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main{
private int m = 0;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
Main ma = new Main();
ma.m = n;
ma.getNum(n);
}
public void getNum(int n) {
List list = new ArrayList<>();
int min = 0 ,max = 0 , count = 0 , position = 0 , temp = n;
Boolean over = true , flag = true;
while (over) {
count = 0;
temp = n;
while (flag) {
temp = temp / 10;
count++;
if (temp == 0) {
flag = false;
}
}
max = getMax(n, count);
min = getMin(n, count);
n = max -min;
System.out.println(max + "-" + min + "=" + n);
for(int i=0;i<list.size();i++){
if(n==(int)list.get(i)){
position = i;
over = false;
break;
}
}
if (over){
list.add(n);
}
flag = true;
}
for(int i=position;i<list.size();i++){
System.out.print(list.get(i)+ " ");
}
}
public int getMax(int n, int count) {
int[] arr = new int[count];
for (int m = 0; m < count; m++) {
arr[m] = n - (n / 10) * 10;
n = n / 10;
}
int c = count;
int max = 0;
int j = 0;
while (c != 0) {
for (int i = 0; i < c; i++) {
j = i + 1;
if (j != c) {
if (arr[i] > arr[j]) {
int k = arr[i];
arr[i] = arr[j];
arr[j] = k;
}
}
}
c--;
}
for (int m = 0; m < count; m++) {
int p = m;
while (p != 0) {
arr[m] = arr[m]*10;
p--;
}
max = max +arr[m];
}
return max;
}
public int getMin(int n, int count) {
int[] a = new int[count];
for (int m = 0; m < count; m++) {
a[m] = n - (n / 10)*10;
n = n / 10;
}
int c = count;
int min = 0;
int j = 0;
while (c != 0) {
for (int i = 0; i < c; i++) {
j = i + 1;
if (j != c) {
if (a[i] < a[j]) {
int k = a[i];
a[i] = a[j];
a[j] = k;
}
}
}
c--;
}
for (int m = 0; m < count; m++) {
int p =m;
while (p != 0) {
a[m] = a[m]*10;
p--;
}
min = min +a[m];
}
return min;
}
}