字符串最后一个单词的长度
1 程序
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String s="";
// 下面这句一定要有
while(input.hasNextLine()){
s=input.nextLine(); // 输入一个字符串
// lastIndexOf 表示" "在字符串中最后一次出现的为
System.out.println(s.length()-1-s.lastIndexOf(" "));
}
}
}
2 测试
hello world
5
计算字符个数
1 程序
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner s = new Scanner(System.in);
String str = s.next(); // 输入的字符串
char c = s.next().charAt(0); // 输入的要比较的字符
int i = getCount(str,c); // 定义功能函数
System.out.println(i);
}
public static int getCount(String str,char c){
int count = 0;
if(str != null && str.length() > 0){ // 字符串的长度必须不为空
for(int i = 0;i < str.length();i++){ // 遍历字符串的每一个字符
char ch = str.charAt(i); // 定位到每一个要比较的字符
char temp =ch; // 比较临时变量
char temp1=c; // 比较临时变量
if(ch>='a'&&ch<='z'){ // 小写转大写,注意是32
temp = (char)((int)ch -32);
}
if(c>='a'&&c<='z'){ // 小写转大写,注意是32
temp1 = (char)((int)c -32);
}
if(temp1 == temp){ // 计数
count++;
}
}
}else{
count = 0; // 空串计数为0
}
return count;
}
}
2 测试
ABCDEF
A
1
明明的随机数
1 程序
import java.util.Scanner;
import java.util.TreeSet;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int num = sc.nextInt(); // 要进行排序的随机数个数
TreeSet<Integer> set = new TreeSet<Integer>(); // 有排序和去重的功能,默认升序
for(int i = 0 ; i < num ;i++){
int curr = sc.nextInt(); // 输入要排序的随机数
set.add(curr); // 加入的集合,进行去重和排序
}
for(Integer i : set){ // 注意这种循环写法
System.out.println(i); // 默认就是按照从小到大的顺序输出
}
}
}
}
2 测试
11
12
21
12
21
34
56
78
98
56
43
23
12
21
23
34
43
56
78
98
字符串分隔
1 程序
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
while(scanner.hasNextLine()){ // 使得具有循环功能
String s=scanner.nextLine(); //输入字符串
split(s); //分割字符串
}
}
public static void split(String s){
while(s.length()>=8){
// 输出前8位
System.out.println(s.substring(0, 8));
// 去掉前8位
s=s.substring(8);
}
if(s.length()>0){
s=s+"00000000"; // 不足8位加8位
System.out.println(s.substring(0, 8)); // 再截取最后一个8位
}
}
}
2 测试
abc
abc00000
123456789
12345678
90000000
进制转换
1 程序
import java.lang.Math;
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){ // 使得具有循环功能
String str = sc.nextLine(); //输入一个十六进制数
System.out.println(fun(str.substring(2))); //去掉0x
}
}
public static int fun(String s){
int n=0; // 十进制数
int temp = 0; // 表示每一位的数字
char ch; // 字符串中的每一位
// 0xABCD = 10*pow(16,3)+11*pow(16,2)+12*pow(16,1)+10*pow(16,0)+
for(int i =0 ;i<s.length();i++)
{
ch = s.charAt(i);
if(ch>='0'&&ch<='9'){ // 0到9对应的整型值
temp = ch-'0';
}else if(ch>='A'&&ch<='Z'){ // A到Z对应的整型值
temp = ch-'A'+10;
}else if(ch>='a'&&ch<='z'){ // a到z对应的整型值
temp = ch-'a'+10;
}else{
break; // 非法跳出
}
n += temp*Math.pow(16,(s.length()-i-1));
}
return n;
}
}
2 测试
OxABC
2748
质数因子
1 程序
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner str = new Scanner(System.in);
long num = str.nextLong(); // 输入一个整数
String result = getResult(num); // 功能函数
System.out.println(result);
}
public static String getResult(long num){
int pum = 2; // 这里必须从2开始,先把所有为2的除尽,然后再把所有位3的除尽,依次类推
String result = ""; // 记录分解后的质数
while(num != 1){
while(num%pum == 0){
num = num/pum; // 得出下一次要分解的数
result = result + pum + " "; // 从小到大依次输出质数
}
pum++;
}
return result;
}
}
2 测试
180
2 2 3 3 5
取近似值
1 程序
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
double d=scanner.nextDouble(); // 输入一个浮点数
System.out.println(getReturn(d));
}
public static int getReturn(double d) {
int i=(int)d; //浮点数的整数部分
// 下面实现四舍五入
if((d-i)>=0.5){
return i+1;
}else{
return i;
}
}
}
2 测试
5.5
6
合并表记录
1 程序
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
// TreeMap是具有排序功能的Map
Map<Integer, Integer> map = new TreeMap<Integer, Integer>();
int n = sc.nextInt(); // 要合并的健值对个数
for (int i = 0; i < n; i++) {
int s=sc.nextInt(); // 键
int value=sc.nextInt(); // 值
if (map.containsKey(s)) { // map中已包含了这个键
map.put(s, map.get(s) + value); // 将该键的值类加
} else
map.put(s, value); // 键值对加入到map中
}
for (Integer key : map.keySet()) { // 默认按照键的升序排列,直接输出map的键值对
System.out.println(key + " " + map.get(key));
}
}
}
}
2 测试
4
0 1
0 2
1 2
3 4
0 3
1 2
3 4
提取不重复的整数
1 程序
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanne