首页 > 编程语言 >Java基础语法3

Java基础语法3

时间:2023-02-14 13:04:24浏览次数:42  
标签:Java int 基础 System 语法 ++ str sc out


排序(SDUT 1582)

import java.util.*;

public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n;
int a[] = new int[200];
n = sc.nextInt();
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
}
Arrays.sort(a, 0, n);
for (int i = 0; i < n; i++) {
if (i == 0)
System.out.print(a[i]);
else
System.out.print(" " + a[i]);
}
System.out.println("");
}
}

期末考试之排名次(SDUT 2255)

import java.util.*;

public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n, c, m, e;
int a[] = new int[200];
n = sc.nextInt();
for (int i = 0; i < n; i++) {
c = sc.nextInt();
m = sc.nextInt();
e = sc.nextInt();
a[i] = c + m + e;
}
Arrays.sort(a, 0, n);
for (int i = n - 1; i >= 0; i--) {
System.out.println(a[i]);
}
}
}

冒泡排序中数据交换的次数(SDUT 2554)

import java.util.*;

public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n, t;
int a[] = new int[200];
t = sc.nextInt();
for (int q = 0; q < t; q++) {
n = sc.nextInt();
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
}
int ans = 0;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - 1 - i; j++) {
if (a[j] > a[j + 1]) {
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
ans++;
}
}
}
System.out.println(ans);
}
}
}

小鑫の日常系列故事(五)——卡片游戏(SDUT 2736)

import java.util.*;

public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int ans1 = 0,ans2 = 0;
for(int i = 1; i <= n; i ++)
{
int x = sc.nextInt();
if(i%2 == 1)ans1 += x;
else ans2 += x;
}
if(ans1==ans2)System.out.println("Equal");
else if(ans1>ans2)System.out.println("Greater than");
else System.out.println("Less than");
}
}

统计元音(SDUT 1250)

import java.util.*;

public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str;
int n;
n = sc.nextInt();
sc.nextLine();
for (int q = 0; q < n; q++) {
str = sc.nextLine();
int len = str.length();
int a, e, i, o, u;
a = e = i = o = u = 0;
for (int j = 0; j < len; j++) {
char op = str.charAt(j);
if (op == 'a')
a++;
else if (op == 'e')
e++;
else if (op == 'i')
i++;
else if (op == 'o')
o++;
else if (op == 'u')
u++;
}
System.out.printf("a:%d\ne:%d\ni:%d\no:%d\nu:%d\n\n", a, e, i, o, u);
}
}
}

回文串判定(SDUT 1524)

import java.util.*;

public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str;
int flag = 0;
str = sc.nextLine();
int len = str.length();
for(int i = 0; i < len/2; i++) {
char t1 = str.charAt(i);
char t2 = str.charAt(len - i - 1);
if(t1 != t2) {
flag = 1;
break;
}
}
if(flag == 0) System.out.println("yes");
else System.out.println("no");

}
}

U     字符统计2(SDUT 1525)

import java.util.*;

public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a[] = new int[200];
String str;
while (sc.hasNext()) {
str = sc.nextLine();
Arrays.fill(a, 0);
int len = str.length();
for (int i = 0; i < len; i++) {
char op = str.charAt(i);
if (op != ' ')
a[op]++;
}
int max = -1, ans = 0;
for (int i = 0; i < 200; i++) {
if (a[i] > max) {
max = a[i];
ans = i;
}
}
System.out.println((char) ans + " " + max);
}
}
}

V     传说中的数据结构(SDUT 2556)

import java.util.*;

public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a[] = new int[2000];
int top = 0, n,x;
String str;
while (sc.hasNext()) {
top = -1;
n = sc.nextInt();
sc.nextLine();
for(int i = 0; i < n; i ++) {
// sc.nextLine(); //读入一行
str = sc.next(); // 只是读入字符串
// System.out.println(str);
if(str.equals("push")==true) {
x = sc.nextInt();
top ++;
a[top] = x;
}
else if(str.equals("pop") == true) {
if(top < 0)System.out.println("error");
else top --;
}
else if(str.equals("top") == true) {
if(top < 0)System.out.println("empty");
else System.out.println(a[top]);
}
}
System.out.println("");
}
}
}

W    小鑫の日常系列故事(十)——排名次

 

标签:Java,int,基础,System,语法,++,str,sc,out
From: https://blog.51cto.com/u_15965659/6056616

相关文章

  • Java面向对象2(G~J)
    G   织女的红线(SDUT2240)importjava.util.Scanner;importjava.text.DecimalFormat;classSum{doublex1,y1,x2,y2;Sum(doublen1,doublem1,doublen2,dou......
  • Java面向对象6(AA ~ AE)
    AE 简单的复数运算(类和对象)(SDUT4303)importjava.util.*;classComplex{ inta,b; Complex(){ } Complex(intn,intm){ a=n; b=m; } voi......
  • Java面向对象3(K~O)
     K    正方形(SDUT2444)importjava.lang.reflect.Array;importjava.util.*;publicclassMain{ publicstaticvoidmain(String[]args){ Scanners......
  • JAVA-studyDay02
    java-day02一.注释//书写注释是非常好的习惯//单行注释////多行注释/**///JavaDoc:文档注释/***///平时写代码一定要注意规范注意:注释不会影响代码二.运算符1.......
  • rxjava之复习
    1.rxjava之操作符1).转换类操作符(mapflatMapconcatMapflatMapIterableswitchMapscangroupBy...);map及flatMap以及concatMap区别?map和flatMap都可以对RxJava传入......
  • java根据地址获取百度API经纬度
    java根据地址获取百度API经纬度(详细文档)1publicvoidgetLarLng(Stringaddress)throwsException{23Stringak="vZ5wAkH9uc6mCnrhtYWey2fBHBmU9Rh......
  • Java 通过get post 请求url
    .已获取小程序的access_token为例,通过Get请求url1importcom.alibaba.fastjson.JSONObject;23StringwechatUrl="https://api.weixin.qq.com/cgi-bin/tok......
  • java删除字符串最后一位
    Strings="1,2,3,4,5,6,7,8,";//目标:删除最后一个","s=s.substring(0,s.length()-1);System.out.println(s); ......
  • eclipse 为javaWeb项目更改jdk版本的正确姿势
    1.情景展示在使用eclipse进行web项目开发时,针对公司以前的老项目,从SVN下载下来后,我们往往会需要更换jdk版本,使其与本地jdk版本保持一致。如何切换jdk版本?2.具体实现第一步:切......
  • 从混沌到规范:JavaScript模块化方案的演进史
    前言JavaScript语言诞生至今,模块规范化之路曲曲折折。社区先后出现了各种解决方案,包括AMD、CMD、CommonJS等,而后ECMA组织在JavaScript语言标准层面,增加了模块功能(因......