标签:最大值 cin tin spl code util port for system.in
码云:https://gitee.com/gs717/codes/u91v8fltja5rcqgi7o04m70
7-1 输出数组元素
import java.util.Scanner;
public class Main{
public static void main(String args[]){
Scanner reader = new Scanner(System.in);
int n = reader.nextInt();
int[] a = new int[n];
int i=0;
int cnt=0;
for(i=0;i<n;i++){
a[i]=reader.nextInt();
}
for (i = 0; i < n - 1; i++){
a[i] = a[i + 1] - a[i];
}
for (i = 0; i < n - 1; i++){
if (i == 0){
System.out.printf("%d", a[0]);
}
else if (cnt == 3){
System.out.printf("\n");
System.out.printf("%d", a[i]);
cnt = 0;
}
else{
System.out.printf(" %d", a[i]);
}
cnt++;
}
}
}
7-2 字符串逆序
import java.util.Scanner;
public class Main{
public static void main (String [] args){
Scanner cin=new Scanner (System.in);
String str=cin.nextLine();
StringBuffer sb =new StringBuffer(str);
System.out.print(sb.reverse().toString());
}
}
7-3 选择法排序
import java.util.Scanner;
public class Main{
public static void main(String args[]){
Scanner reader = new Scanner(System.in);
int n = reader.nextInt();
int[] a = new int[n];
int x=0;
for(int i=0;i<n;i++){
a[i]=reader.nextInt();
}
for(int i=0;i<n;i++){
for(int j=1;j<n;j++){
if(a[j]>a[j-1]){
x=a[j];
a[j]=a[j-1];
a[j-1]=x;
}
}
}
for(int i=0;i<n;i++){
System.out.print(a[i]);
if(i!=n-1){
System.out.print(" ");
}
}
}
}
7-4 说反话-加强版
import java.io.IOException;
import java.text.ParseException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws ParseException, IOException {
Scanner scanner = new Scanner(System.in);
String str = scanner.nextLine().trim();
String[] strs = str.split(" +");
for(int i=strs.length-1;i>=0;--i)
{
System.out.print(strs[i]);
if(i!=0)
System.out.print(" ");
}
}
}
7-5 简化的插入排序
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner read=new Scanner(System.in);
int n=read.nextInt();
int a[]=new int[n+1];
int i;
for(i=0;i<n;i++){
a[i]=read.nextInt();
}
a[n]=read.nextInt();
Arrays.sort(a);
for(i=0;i<=n;i++){
System.out.print(a[i]+" ");
}
}
}
7-6 交换最小值和最大值
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner read=new Scanner(System.in);
int n=read.nextInt();
int a[]=new int[n];
int i;
for(i=0;i<n;i++){
a[i]=read.nextInt();
}
int max=0;
int min=0;
for(i=1;i<n;i++){
max=(a[i]>a[max])?i:max;
min=(a[i]<a[min])?i:min;
}
int change;
change=a[0];
a[0]=a[min];
a[min]=change;
if(max==0){
max=min;
}
change=a[n-1];
a[n-1]=a[max];
a[max]=change;
for(i=0;i<n;i++){
System.out.print(a[i]+" ");
}
}
}
7-8 IP地址转换
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner read=new Scanner(System.in);
String n=read.nextLine();
String a=n.substring(0,8);
String b=n.substring(8,16);
String c=n.substring(16,24);
String d=n.substring(24,32);
int a1=Integer.parseInt(a,2);
int b1=Integer.parseInt(b,2);
int c1=Integer.parseInt(c,2);
int d1=Integer.parseInt(d,2);
System.out.print(a1+"."+b1+"."+c1+"."+d1);
}
}
因电脑中为安装java软件 所以无法展现操作结果 但所有程序在pta上测试正常 。
标签:最大值 cin tin spl code util port for system.in
原文地址:https://www.cnblogs.com/gs717/p/9901693.html