1:奥运奖牌计数
import java.util.Scanner;
//输入n+1行,第1行是A国参与决赛项目的天数n,其后n行,每一行是该国某一天获得的金、银、铜牌数目,以一个空格分开
//输出1行,包括4个整数,为A国所获得的金、银、铜牌总数及总奖牌数,以一个空格分开。
public class 奥运奖牌计数 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int x=sc.nextInt();
int a=0; //金牌计数器
int b=0; //银牌计数器
int c=0; //铜牌计数器
//天数控制语句
for (int i = 0; i < x; i++) {
a+=sc.nextInt();//金牌数
b+=sc.nextInt();//银牌数
c+=sc.nextInt();//铜牌数
}
System.out.print(a+" "+b+" "+c+" "+(a+b+c));
}
}
2:病历单加密
import java.util.Scanner;
/*
* 小英是药学专业大三的学生,暑假期间获得了去医院药房实习的机会。
在药房实习期间,小英扎实的专业基础获得了医生的一致好评,得知小英在计算概论中取得过好成绩后,
主任又额外交给她一项任务,解密抗战时期被加密过的一些伤员的名单。
经过研究,小英发现了如下加密规律(括号中是一个“原文 -> 密文”的例子)
1. 原文中所有的字符都在字母表中被循环左移了三个位置(dec -> abz)
2. 逆序存储(abcd -> dcba )
3. 大小写反转(abXY -> ABxy)
*
* */
public class 病历单加密 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String str=sc.nextLine();
String str1=""; //定义字符串用于存放新的字符串
char x=' '; //定义字符,用于单个字符操作
//以字符串长度控制循环
for (int i = 0; i < str.length(); i++) {
if('W'<str.charAt(i)&&str.charAt(i)<='Z'){
x=(char)('A'+str.charAt(i)+2-'Z'+32);//算出其字符对应的ASCII的值,将其转换为char类型,并将大小写互换
}else if('A'<=str.charAt(i)&&str.charAt(i)<='W'){
x=(char)(str.charAt(i)+3+32);
}else if('w'<str.charAt(i)&&str.charAt(i)<='z'){
x=(char)('a'+str.charAt(i)+2-'z'-32);
}else{
x=(char)(str.charAt(i)+3-32);
}
str1=x+str1; //循环相加得到新的倒叙的字符串
}
System.out.println(str1);
}
}
3:大象喝水
/*
*一只大象口渴了,要喝20升水才能解渴,但现在只有一个深h厘米,底面半径为r厘米的小圆桶(h和r都是整数)。问大象至少要喝多少桶水才会解渴。
输入
输入有一行:包行两个整数,以一个空格分开,分别表示小圆桶的深h和底面半径r,单位都是厘米。
输出
输出一行,包含一个整数,表示大象至少要喝水的桶数。
样例输入
*
* */
import java.util.Scanner;
public class 大象喝水 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int b=sc.nextInt();
double pi=3.14159;
int num=0;
//小圆桶的装水的多少和大象喝水的多少进行判断
if(20*1000<pi*b*b*a){
num=1;
}
else{
num=(int) (20*1000/(pi*b*b*a)+1); //桶数,进行向上取整
}
System.out.println(num);
}
}
4:病人排队
import java.util.Scanner;
/*
*病人登记看病,编写一个程序,将登记的病人按照以下原则排出看病的先后顺序:
1. 老年人(年龄 >= 60岁)比非老年人优先看病。
2. 老年人按年龄从大到小的顺序看病,年龄相同的按登记的先后顺序排序。
3. 非老年人按登记的先后顺序看病。
输入
第1行,输入一个小于100的正整数,表示病人的个数;
后面按照病人登记的先后顺序,每行输入一个病人的信息,包括:一个长度小于10的字符串表示病人的ID
(每个病人的ID各不相同且只含数字和字母),一个整数表示病人的年龄,中间用单个空格隔开。
输出
按排好的看病顺序输出病人的ID,每行一个。
样例输入
5
021075 40
004003 15
010158 67
021033 75
102012 30
样例输出
021033
010158
021075
004003
102012
*
* */
public class 病人排队 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
// 定义字符串数组用于存放年龄小于60的病人ID
String[] str = new String[num];
// 定义字符串数组用于存放年龄大于等于60的病人ID
String[] str1 = new String[num];
// 定义整型数组存放大于等于60的病人年龄
int[] age = new int[num];
// 定义年龄大于等于60以及小于60的计数器
int count = 0;
int sum = 0;
for (int i = 0; i < num; i++) {
// 定义变量用于临时存放ID和年龄
String s = sc.next();
int x = sc.nextInt();
if (x >= 60) {
age[count] = x;
str1[count] = s;
count++;
} else {
str[sum] = s;
sum++;
}
}
// 利用冒泡排序法根据年龄降序对病人ID进行对应存储
for (int i = 0; i < count; i++) {
for (int j = i + 1; j <= count - 1; j++) {
if (age[i] < age[j]) {
int temp=age[i];
age[i]=age[j];
age[j]=temp;
String z = str1[i];
str1[i] = str1[j];
str1[j] = z;
}
}
}
// 输出看病顺序病人ID
for (int i = 0; i < count; i++) {
System.out.println(str1[i]);
}
for (int i = 0; i < sum; i++) {
System.out.println(str[i]);
}
}
}
5:不高兴的津津
import java.util.Scanner;
/*
*津津上初中了。妈妈认为津津应该更加用功学习,所以津津除了上学之外,还要参加妈妈为她报名的各科复习班。
*另外每周妈妈还会送她去学习朗诵、舞蹈和钢琴。但是津津如果一天上课超过八个小时就会不高兴,而且上得越久就会越不高兴。
*假设津津不会因为其它事不高兴,并且她的不高兴不会持续到第二天。请你帮忙检查一下津津下周的日程安排,看看下周她会不会不高兴;
*如果会的话,哪天最不高兴。
输入
包括七行数据,分别表示周一到周日的日程安排。每行包括两个小于10的非负整数,用空格隔开,
分别表示津津在学校上课的时间和妈妈安排她上课的时间。
输出
包括一行,这一行只包含一个数字。如果不会不高兴则输出0,如果会则输出最不高兴的是周几(用1, 2, 3, 4, 5, 6, 7
分别表示周一,周二,周三,周四,周五,周六,周日)。如果有两天或两天以上不高兴的程度相当,则输出时间最靠前的一天。
样例输入
5 3
6 2
7 2
5 3
5 4
0 4
0 6
样例输出
3
* */
public class 不高兴的津津 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int[] arr=new int[7];
int max=0;
//定义计数器
int count=1;
for (int i = 0; i < arr.length; i++) {
//将津津上课的时间相加
for (int j = 0; j < 2; j++) {
arr[i]+=sc.nextInt();
}
}
//将星期一上课的时间相加
max=arr[0];
//找出津津上课时间的最大值
for (int i = 1; i < arr.length; i++) {
if(max<arr[i]){
max=arr[i];
count=i+1;
}
}
//判断津津是否高兴
if(max>8)
System.out.println(count);
else
System.out.println(0);
}
}
6:不与最大数相同的数字之和
package 算法;
import java.util.Arrays;
import java.util.Scanner;
/*
*输出一个整数数列中不与最大数相同的数字之和。
输入
输入分为两行:
第一行为N(N为接下来数的个数,N <= 100);
第二行为N个整数,数与数之间以一个空格分开,每个整数的范围是-1000,000到1000,000。
输出
输出为N个数中除去最大数其余数字之和。
样例输入
3
1 2 3
样例输出
3
*
* */
public class 不与最大数相同的数字之和 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
// 定义数组用于存放整数
int[] arr = new int[num];
// 定义数组和的变量
int sum = 0;
for (int i = 0; i < arr.length; i++) {
arr[i] = sc.nextInt();
}
// 利用工具类对数组进行排序
Arrays.sort(arr);
if (num == 1||arr[0]==arr[arr.length-1])
System.out.println(0);
else {
// 对除去最大数的数进行加法运算
for (int i = 0; i < arr.length - 1; i++) {
//判断,如果数组中的数不等于最大值则进行加法运算
if(arr[arr.length-1]!=arr[i])
sum += arr[i];
}
System.out.println(sum);
}
}
}
7:财务管理
package 算法;
import java.util.Scanner;
public class 财务管理 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
double[] moneys=new double[12];//钱数存储容器
double sum=0;//钱数总和
//将容器中的钱数相加
for(int i=0;i<moneys.length;i++){
sum+=sc.nextDouble();
}
//输出平均钱数,保留两位小数并在其前面加"$"
System.out.printf("$"+"%.2f",sum/12);
}
}
8:成绩排序
package 算法;
import java.util.Scanner;
/*
*给出班里某门课程的成绩单,请你按成绩从高到低对成绩单排序输出,如果有相同分数则名字字典序小的在前。
输入
第一行为n (0 < n < 20),表示班里的学生数目;
接下来的n行,每行为每个学生的名字和他的成绩, 中间用单个空格隔开。名字只包含字母且长度不超过20,成绩为一个不大于100的非负整数。
输出
把成绩单按分数从高到低的顺序进行排序并输出,每行包含名字和分数两项,之间有一个空格。
样例输入
4
Kitty 80
Hanmeimei 90
Joey 92
Tim 28
样例输出
Joey 92
Hanmeimei 90
Kitty 80
Tim 28
*
* */
public class 成绩排序 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int num=sc.nextInt();
//定义字符串数组用于存放姓名
String[] str=new String[num];
//定义整型数组用于存放成绩
int[] arr=new int[num];
for (int i = 0; i < num; i++) {
str[i]=sc.next();
arr[i]=sc.nextInt();
}
//利用冒泡排序法对成绩高低进行排序,并对应姓名
for (int i = 0; i < arr.length; i++) {
for (int j = i+1; j <=arr.length-1; j++) {
//按成绩由小到大的顺序排序
if(arr[i]<arr[j]){
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
String name=str[i];
str[i]=str[j];
str[j]=name;
}
//当成绩相同时,按名字的字典顺序从小到大进行排序
if(arr[i]==arr[j]){
int m=str[i].compareTo(str[j]);
if(m>0){
String name=str[i];
str[i]=str[j];
str[j]=name;
}
}
}
}
for (int i = 0; i < arr.length; i++) {
System.out.print(str[i]+" "+arr[i]+"\r\n");
}
}
}
9:乘方问题
package 算法;
import java.util.Scanner;
//一行,包含两个整数a和n。-1000000 <= a <= 1000000,1 <= n <= 10000。
//一个整数,即乘方结果。题目保证最终结果的绝对值不超过1000000。
public class 乘方问题 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int n=sc.nextInt();
int prodect=1; //初始数据
//控制乘a的个数
for (int i = 0; i < n; i++) {
prodect*=a;
}
System.out.println(prodect);
}
}
10:虫子和苹果2
package 算法;
/*
*你买了一箱n个苹果,很不幸的是买完时箱子里混进了一条虫子。虫子每x小时能吃掉一个苹果,假设虫子在吃完一个苹果之前不会吃另一个,那么经过y小时你还有多少个完整的苹果?
输入
输入仅一行,包括n,x和y(均为整数)。
输出
输出也仅一行,剩下的苹果个数
*
* */
import java.util.Scanner;
public class 虫子和苹果2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int y = sc.nextInt();
int apple = 0; //苹果的剩余个数
double x = sc.nextDouble();
int z = sc.nextInt();
//总苹果树减去被虫子吃过的苹果数(向上取整)
apple = y - (int) (Math.ceil(z / x));
//对剩余苹果数进行判断输出
if (apple >= 0)
System.out.println(apple);
else
System.out.println(0);
}
}
11:串的简单处理
package 算法;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/*1. 把每个单词的首字母变为大写。
2. 把数字与字母之间用下划线字符(_)分开,使得更清晰
3. 把单词中间有多个空格的调整为1个空格。
例如:
用户输入:
you and me what cpp2005program
则程序输出:
You And Me What Cpp_2005_program*/
@SuppressWarnings("all")
public class 串的简单处理 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("请输入你要进行操作的字符串:");
String str = sc.nextLine();
String regex = "\\s+"; // 匹配空格正则
String regex1 = "(\\d+)";// 数字匹配字符串
String regex2 = "[a-zA-Z]+";
String str1 = str.replaceAll(regex, " ");// 将每次出现的空格用一个空格代替
String[] str2 = str1.split(" "); // 利用空格将其拆分为字符数组
String s = "";
String s2 = "";
for (int i = 0; i < str2.length; i++) {
String s1 = str2[i].substring(0, 1).toUpperCase().concat(str2[i].substring(1).toLowerCase());
if (!s1.startsWith(regex1)) {
s2 = s1.replaceAll(regex1, "$1_");
}
else {
s2 = s1.replaceAll(regex1, "_$1_");
}
System.out.print(s2 + " ");
// s += s1 + " ";
}
// System.out.println(s);
}
}
12:串排序
package 算法;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/*
* 将01串首先按长度排序,长度相同按1的个数排序,1的个数相同按字典顺序排序。
输入
输入包含一个整数n,代表要录入的字符串个数n,接下来输入n个字符串
输出
输出排序后的字符串,每行输出一个。
样例输入
5
10010010
11100010
100101
1000111
11100001
样例输出
100101
1000111
10010010
11100001
11100010
*
* */
@SuppressWarnings("unchecked")
public class 串排序 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int num=Integer.parseInt(sc.nextLine());
List list=new ArrayList<>();
for (int i = 0; i < num; i++) {
list.add(sc.nextLine());
}
bijiao(list);
for (Object str : list) {
System.out.println(str);
}
}
private static void change(List list,int i,int j) {
String temp=(String) list.get(i);
list.set(i, list.get(j));
list.set(j, temp);
}
private static void bijiao(List list) {
for (int i = 0; i < list.size(); i++) {
for (int j = i+1; j <= list.size()-1; j++) {
if(list.get(i).toString().length()>list.get(j).toString().length()){
change(list, i, j);
}else if(list.get(i).toString().length()==list.get(j).toString().length()){
if(list.get(i).toString().replaceAll("0+", "").length()>list.get(j).toString().replaceAll("0+", "").length()){
change(list, i, j);;
}
if(list.get(i).toString().replaceAll("0+", "").length()==list.get(j).toString().replaceAll("0+", "").length()){
int x=list.get(i).toString().compareTo(list.get(j).toString());
if(x>0){
change(list, i, j);
}
}
}
}
}
}
}
13:行程长度编码
package 算法;
import java.util.Scanner;
/*
*在数据压缩中,一个常用的途径是行程长度压缩。对于一个待压缩的字符串而言,我们可以依次记录每个字符及重复的次数。
*这种压缩,对于相邻数据重复较多的情况比较有效。 例如,如果待压缩串为"AAABBBBCBB",则压缩的结果是(A,3)(B,4)(C,1)(B,2)。
*当然,如果相邻字符重复情况较少,则压缩效率就较低。
现要求根据输入的字符串,得到大小写不敏感压缩后的结果(即所有小写字母均视为相应的大写字母)。
输入
一个字符串,长度大于0,且不超过1000,全部由大写或小写字母组成。
输出
输出为一行,表示压缩结果,形式为:
(A,3)(B,4)(C,1)(B,2)
即每对括号内部分别为字符(都为大写)及重复出现的次数,不含任何空格。
样例输入
aAABBbBCCCaaaaa
样例输出
(A,3)(B,4)(C,3)(A,5)
*
* */
public class 行程长度编码 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
//输入字符串将其全部转换为大写,并在字符串尾部加一个小写的字母a,用来进行循环控制
String str=sc.nextLine().toUpperCase()+"a";
//定义每个不同字母的计数器
int count=1;
//定义一个长为字符串长度的数组
String[] arr=new String[str.length()];
//定义需要用到的字符串数组长度
int x=0;
//根据数组长度控制循环次数
for (int i = 0; i < str.length()-1; i++) {
//如果前一个字符等于后一个字符则计数器加1
if(str.charAt(i)==str.charAt(i+1)){
count++;
}
else{
//将相等的字符以及出现的次数存入字符串数组中
arr[x]="("+str.charAt(i)+","+count+")";
//每存储一次计数器归1
count=1;
//每存储一次,所用到的字符串数组加1
x++;
}
}
//遍历数组
for (int i = 0; i < x; i++) {
System.out.print(arr[i]);
}
}
}
14:大小写字母互换
package 算法;
import java.util.Scanner;
public class 大小写字母互换 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String str=sc.nextLine();
//字符存放容器
char[] ch=new char[str.length()];
//根据字符串的长度控制循环语句
for (int i = 0; i < str.length(); i++) {
//对单个字符所在的区间进行判断,将大小写相互转换存放进字符容器
if(str.charAt(i)>='a'&&str.charAt(i)<='z')
ch[i]=(char)(str.charAt(i)-32); //小写转大写减32
else if(str.charAt(i)>='A'&&str.charAt(i)<='Z')
ch[i]=(char)(str.charAt(i)+32); //大写转小写加32
else
ch[i]=str.charAt(i);
}
//将字符数组转换为字符串输出
System.out.println(String.valueOf(ch));
}
}
15:大整数加法
package 算法;
import java.util.Scanner;
public class 大小写字母互换 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String str=sc.nextLine();
//字符存放容器
char[] ch=new char[str.length()];
//根据字符串的长度控制循环语句
for (int i = 0; i < str.length(); i++) {
//对单个字符所在的区间进行判断,将大小写相互转换存放进字符容器
if(str.charAt(i)>='a'&&str.charAt(i)<='z')
ch[i]=(char)(str.charAt(i)-32); //小写转大写减32
else if(str.charAt(i)>='A'&&str.charAt(i)<='Z')
ch[i]=(char)(str.charAt(i)+32); //大写转小写加32
else
ch[i]=str.charAt(i);
}
//将字符数组转换为字符串输出
System.out.println(String.valueOf(ch));
}
}
16:大整数减法
package 算法;
import java.math.BigInteger;
import java.util.Scanner;
public class 大整数减法 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String x=sc.nextLine();
String y=sc.nextLine();
//定义BigInteger变量用于存储大整数
BigInteger x1=new BigInteger(x);
BigInteger y1=new BigInteger(y);
//大整数相减
x1=x1.add(y1.negate());
//将大整数转换为字符串输出
System.out.println(x1.toString());
}
}
17:单词排序
package 算法;
import java.util.Scanner;
/*
*编写程序,读入一行英文(只包含字母和空格,单词间以单个空格分隔),将所有单词的顺序倒排并输出,依然以单个空格分隔。
输入
输入为一个字符串(字符串长度至多为100)。
输出
输出为按要求排序后的字符串。
样例输入
I am a student
样例输出
student a am I
*
* */
public class 单词倒排 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String str=sc.nextLine();
String[] arr=str.split(" ");
String str1="";
for (int i = 0; i < arr.length; i++) {
str1+=arr[arr.length-i-1]+" ";
}
System.out.println(str1);
}
}
18:单词的长度
package 算法;
import java.util.Scanner;
/*
*输入一行单词序列,相邻单词之间由1个或多个空格间隔,请对应地计算各个单词的长度。
注意,如果有标点符号(如连字符,逗号),标点符号算作与之相连的词的一部分。没有被空格间开的符号串,都算作单词。
输入
一行单词序列,最少1个单词,最多300个单词,单词之间用至少1个空格间隔。单词序列总长度不超过1000。
输出
依次输出对应单词的长度,之间以逗号间隔。
样例输入
She was born in 1990-01-02 and from Beijing city.
样例输出
3,3,4,2,10,3,4,7,5
*
* */
public class 单词的长度 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine().replaceAll(" +", " ");
String[] str1 = str.split(" ");
for (int i = 0; i < str1.length; i++) {
if (i < str1.length - 1)
System.out.print(str1[i].length() + ",");
else
System.out.println(str1[i].length());
}
}
}
19:单词翻转
package 算法;
import java.util.Scanner;
/*
*输入一个句子(一行),将句子中的每一个单词翻转后输出。
输入
只有一行,为一个字符串,不超过500个字符。单词之间以空格隔开。
输出
翻转每一个单词后的字符串,单词之间的空格需与原文一致。
样例输入
hello world
样例输出
olleh dlrow
*
* */
public class 单词翻转 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//给每个输入的字符串后面加一个空格
String str = sc.nextLine() + " ";
//定义 空字符串,用于存储新字符串
String str1 = "";
//定义字符串,用于对每个单词进行操作
String str2 = "";
//根据单词的长度控制循环语句
for (int i = 0; i < str.length(); i++) {
//将每个单词翻转
if (str.charAt(i) != ' ') {
str2 = str.charAt(i) + str2;
} else {
str1 += str2; //将每个单词逐个加起来
str2 = ""; //将存放单词的字符串赋值为空字符串
}
//如果是空格则直接相加
if (str.charAt(i) == ' ')
str1 += str.charAt(i);
}
//由于开=开始给每个字符串尾部加了个空格,因此截取到其字符串长度减1的位置
System.out.println(str1.substring(0, str1.length()-1));
}
}
20:单词替换
package 算法;
import java.util.Scanner;
/*
*输入一个字符串,以回车结束(字符串长度<=100)。该字符串由若干个单词组成,单词之间用一个空格隔开,所有单词区分大小写。
*现需要将其中的某个单词替换成另一个单词,并输出替换之后的字符串。
输入
输入包括3行,
第1行是包含多个单词的字符串 s;
第2行是待替换的单词a(长度 <= 100);
第3行是a将被替换的单词b(长度 <= 100).
*
*
* */
public class 单词替换 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
//输入字符串,并将其以空格分割开
String[] str=sc.nextLine().split(" ");
String a=sc.nextLine();
String b=sc.nextLine();
for (int i = 0; i < str.length; i++) {
if(str[i].equals(a))
str[i]=b;
}
for (int i = 0; i < str.length; i++) {
System.out.print(str[i]+" ");
}
}
}
21:点和正方形的关系
package 算法;
import java.util.Scanner;
/*
* 有一个正方形,四个角的坐标(x,y)分别是(1,-1),(1,1),(-1,-1),(-1,1),
* x是横轴,y是纵轴。写一个程序,判断一个给定的点是否在这个正方形内(包括正方形边界)。
* 输入一行,包括两个整数x、y,以一个空格分开,表示坐标(x,y)。
* 输出一行,如果点在正方形内,则输出yes,否则输出no。
* */
public class 点和正方形的关系 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int x=sc.nextInt();
int y=sc.nextInt();
//判断坐标点是否在正方形的范围
if(x>=-1&&x<=1&&y>=-1&&y<=1)
System.out.println("yes");
else
System.out.println("no");
}
}
22:方便记忆的电话号码
package 算法;
import java.util.Arrays;
import java.util.Scanner;
/*
*英文字母(除Q和Z外)和电话号码存在着对应关系,如下所示:
A,B,C -> 2
D,E,F -> 3
G,H,I -> 4
J,K,L -> 5
M,N,O -> 6
P,R,S -> 7
T,U,V -> 8
W,X,Y -> 9
标准的电话号码格式是xxx-xxxx,其中x表示0-9中的一个数字。有时为了方便记忆电话号码,我们会将电话号码的数字转变为英文字母,
如把263-7422记成America。有时,我们还加上“-”作为分隔符,如把449-6753记成Hi-World。当然,我们未必要将所有的数字都转变为字母,
比如474-6635可以记成iPhone-5。
总之,一个方便记忆的电话号码由数字和除Q、Z外的英文字母组成,并且可以在任意位置插入任意多的“-”符号。
现在 ,我们有一个列表,记录着许多方便记忆的电话号码。不同的方便记忆的电话号码可能对应相同的标准号码,你的任务就是找出它们。
输入
第一行是一个正整数n(n <= 100000),表示列表中的电话号码数。
其后n行,每行是一个方便记忆的电话号码,它由数字和除Q、Z外的英文字母、“-”符号组成,其中数字和字母的总数一定为7,字符串总长度不超过200。
输出
输出包括若干行,每行包括一个标准电话号码(xxx-xxxx)以及它重复出现的次数k(k >= 2),中间用空格分隔。输出的标准电话号码需按照升序排序。
如果没有重复出现的标准电话号码,则输出一行“No duplicates.”。
样例输入
12
4873279
ITS-EASY
888-4567
3-10-10-10
888-GLOP
TUT-GLOP
967-11-11
310-GINO
F101010
888-1200
-4-8-7-3-2-7-9-
487-3279
样例输出
310-1010 2
487-3279 4
888-4567 3
* */
public class 方便记忆的电话号码 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int num=sc.nextInt();
//定义一个整型数组用于存放转换后的电话号码
int[] call=new int[num];
//定义相同电话号码计数器
int count=1;
int y=1;
//根据输入的个数控制循环
for (int i = 0; i < num; i++) {
//定义一个字符串用于临时存储电话号码
String phone=sc.next();
//根据字符串的长度判断字符串中是否包含"-",如果包含则去除"-"
phone=phone.replaceAll("-+", "");
//定义一个空字符串,用于连接每个字符
String tel="";
//定义一个空字符,用于临时存放其字母对应的数字
char x=' ';
//对字符串进行判断如果包含字母则将其转换为对应的数字
for (int j = 0; j < phone.length(); j++) {
if('A'<=phone.charAt(j)&&phone.charAt(j)<='C')
x='2';
else if('D'<=phone.charAt(j)&&phone.charAt(j)<='F')
x='3';
else if('G'<=phone.charAt(j)&&phone.charAt(j)<='I')
x='4';
else if('J'<=phone.charAt(j)&&phone.charAt(j)<='L')
x='5';
else if('M'<=phone.charAt(j)&&phone.charAt(j)<='O')
x='6';
else if('P'==phone.charAt(j)||phone.charAt(j)=='R'||phone.charAt(j)=='S')
x='7';
else if('T'<=phone.charAt(j)&&phone.charAt(j)<='V')
x='8';
else if('W'<=phone.charAt(j)&&phone.charAt(j)<='Y')
x='9';
else
x=phone.charAt(j);
tel+=x;
}
//将电话号码转换为整型存放进数组,用于排序
call[i]=Integer.parseInt(tel);
}
//对电话号码进行升序排序
Arrays.sort(call);
//判断电话号码出现的次数
for (int i = 0; i < call.length-1; i++) {
if(call[i]==call[i+1]){
count++;
}else{
if(count>1)
System.out.print(String.valueOf(call[i]).substring(0,3)+"-"+String.valueOf(call[i]).substring(3)+" "+count+"\r\n");
count=1;
y++;
}
if(call[i]==call[i+1]&&i==call.length-2)
System.out.print(String.valueOf(call[i]).substring(0,3)+"-"+String.valueOf(call[i]).substring(3)+" "+count+"\r\n");
}
if(num==1||y==num)
System.out.println("No duplicates.");
}
}
23:斐波那切数列
package 算法;
import java.util.Scanner;
public class 菲波那切数列 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
//对于斐波那契额数列第一第二的数都为1
if (x == 1||x==2) {
System.out.println(1);
} else {
//定义数组,用于存放数字
int[] fib = new int[x];
//令第一第二个数为1
fib[0] = 1;
fib[1] = 1;
//循环控制语句,且后一个数等于前两个数之和
for (int i = 2; i < fib.length; i++) {
fib[i] = fib[i - 1] + fib[i - 2];
}
System.out.println(fib[fib.length-1]);
}
}
}
24:费式数列
package 算法;
/*Fibonacci为1200年代的欧洲数学家,在他的著作中曾经提到:“若有一只免子每个月生一只小免子,
* 一个月后小免子也开始生产。起初只有一只免子,一个月后就有两只免子,二个月后有三只免子,三个月后有五只免子(小免子投入生产)......”。
如果不太理解这个例子的话,举个图就知道了,注意新生的小免子需一个月成长期才会投入生产,类似的道理也可以用于植物的生长,
这就是Fibonacci数列,一般习惯称之为费氏数列,例如以下:
1、1 、2、3、5、8、13、21、34、55、89...... */
public class 费式数列 {
public static void main(String[] args) {
int[] fib=new int[2];
fib[0]=0;
fib[1]=1;
for(int i=2;i<fib.length;i++){
fib[i]=fib[i-1]+fib[i-2];
}
for(int i=0;i<fib.length;i++){
System.out.print(fib[i]+" ");
}
System.out.println();
}
}
25:分离整数的各个位数
package 算法;
import java.util.Scanner;
//给定一个整数,要求从个位开始分离出它的每一位数字
//从个位开始按照从低位到高位的顺序依次输出每一位数字。数字之间以一个空格分开。
public class 分离整数的各个位数 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int num=sc.nextInt();
//将整数转换为字符串,并计算其长度
int lon=String.valueOf(num).length();
//定义存放单个数字的容器
int[] s=new int[String.valueOf(num).length()];
if(num<10){
System.out.println(num);
}
else{
for(int i=0;i<lon;i++){
//依次取出数字的低位和高位并存放进容器
s[i]=num%10;
num=num/10;
}
for(int i=0;i<lon;i++){
System.out.print(s[i]+" ");
}
}
}
}
26:根据公式计算
package 算法;
import java.util.Scanner;
/*
* 对某些带电感L和电阻R的电路,其自然衰减频率由等式:频率=给定L、R,希望研究频率随电容C的波动情况。
* 请编程,用于计算从0.01到0.1、步长为0.01的不同C值时的频率(保留3位小数)。
* 输入一行,包含两个单精度浮点数,表示L 和 R的值。数与数之间以空格分开。
(L > 0, R >= 0, L * R2 <= 0.04)
输出10行,每行一个数,对应C值从小到大计算所得的频率,每个数保留3位小数。
* */
public class 根据公式计算 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
float l = sc.nextFloat();
float r = sc.nextFloat();
float c = 0;
for (int i = 0; i < 10; i++) {
c = (float) (c + 0.01);
System.out.printf("%.3f", Math.sqrt(1 / (l * c) - r * r / (4 * c * c)));
System.out.println();
}
}
}
27:过滤多余的空格
package 算法;
import java.util.Scanner;
import org.omg.Messaging.SyncScopeHelper;
/*
*一个句子中也许有多个连续空格,过滤掉多余的空格,只留下一个空格。
输入
一行,一个字符串(长度不超过200),句子的头和尾都没有空格。
输出
过滤之后的句子。
样例输入
Hello world.This is c language.
样例输出
Hello world.This is c language
*
* */
public class 过滤多余的空格 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String str=sc.nextLine().replaceAll(" +", " ");
System.out.println(str);
}
}
28:紧急措施
package 算法;
import java.util.Scanner;
/*
*近日,一些热门网站遭受黑客入侵,这些网站的账号、密码及email的数据惨遭泄露。你在这些网站上注册若干账号(使用的用户名不一定相同),但是注册时使用了相同的email。你此时拿到了那份泄露的数据,希望尽快将自己的密码更改。策略如下:根据email找到你的用户名和密码,然后更改密码。更改的规则为:小写和大写交换,非字母字符保持不变。
输入
第一行为你的email地址,长度不超过50个字符且只包含字母、数字和‘@’符号。
第二行为账号数N,N(0 < N < 10000)。
接下来N行,每行表示一个账号,格式为:
用户名 密码 email
它们之间用单个空格分开。用户名、密码、email均不含空格,且长度不超过50个字符。
输出
有若干行,每行为你的一个账号,包括:你的账号,修改后的密码(之间用单个空格分隔)。
如果没有你的账号,则输出empty。
*
* */
public class 紧急措施 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
//email地址
String str=sc.nextLine();
//账号数
int x=Integer.parseInt(sc.nextLine());
String[] str1=new String[x];
//非自己email账号计数器
int count=0;
//根据账号数控制循环语句
for (int i = 0; i < x; i++) {
//输入用户名,密码,email并将其根据空格截取放入字符串数组中
String[] str2=sc.nextLine().split(" ");
//判断email账号是否为自己的
if(str2[2].equals(str)){
String str3=str2[0]+" ";
//将自己的原来密码大小写互换得到新的密码,非字母字符保持不变
for (int j = 0; j < str2[1].length(); j++) {
if('a'<=str2[1].charAt(j)&&str2[1].charAt(j)<='z')
str3+=(char)(str2[1].charAt(j)-32);
else if('A'<=str2[1].charAt(j)&&str2[1].charAt(j)<='Z')
str3+=(char)(str2[1].charAt(j)+32);
else
str3+=str2[1].charAt(j);
}
System.out.println(str3);
}
else
count++;
}
if(count==x)
System.out.println("empty");
}
}
29:合影效果
package 算法;
import java.util.Arrays;
import java.util.Scanner;
/*
*小云和朋友们去爬香山,为美丽的景色所陶醉,想合影留念。如果他们站成一排,男生全部在左(从拍照者的角度),
*并按照从矮到高的顺序从左到右排,女生全部在右,并按照从高到矮的顺序从左到右排,请问他们合影的效果是什么样的(所有人的身高都不同)?
输入
第一行是人数n(2 <= n <= 40,且至少有1个男生和1个女生)。
后面紧跟n行,每行输入一个人的性别(男male或女female)和身高(浮点数,单位米),两个数据之间以空格分隔。
输出
n个浮点数,模拟站好队后,拍照者眼中从左到右每个人的身高。每个浮点数需保留到小数点后2位,相邻两个数之间用单个空格隔开。
样例输入
6
male 1.72
male 1.78
female 1.61
male 1.65
female 1.70
female 1.56
样例输出
1.65 1.72 1.78 1.70 1.61 1.56
*
* */
public class 合影效果 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int num=sc.nextInt();
//定义存放男女生身高的数组
double[] male=new double[num];
double[] female=new double[num];
//定义男生人数计数器
int count=0;
for (int i = 0; i < num; i++) {
//输入性别和年龄,若为男生则将计数器加1,将身高存入male数组,反之存入female数组
String sex=sc.next();
double high=sc.nextDouble();
if(sex.equals("male")){
count++;
male[i]=high;
}else
female[i]=high;
}
//利用数组工具类对男女身高进行排序输出
Arrays.sort(male);
Arrays.sort(female);
for (int i = male.length-count; i < male.length; i++) {
System.out.printf("%.2f"+"%s",male[i]," ");
}
for (int i = female.length-1; i>=count ; i--) {
System.out.printf("%.2f"+"%s",female[i]," ");
}
}
}
30:黑马歌手大赛
package 算法;
import java.util.Arrays;
import java.util.Scanner;
/*
*某年某日,为了庆祝黑马程序员成立一百周年,西安黑马中心进行了一次青年歌手大奖赛,每位评委会给参赛选手打分。
选手得分规则为去掉一个最高分和一个最低分,然后计算平均得分,请编程实现,输出平均分,保留到小数点后两位。
输入
第一个数是n(2 < n < 100),表示评委的人数,然后是n个评委的打分,打分分数均为小于或等于100的正整数。
输出
输出平均得分,保留到小数点后两位
*
*
* */
public class 黑马歌手大赛 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int num=sc.nextInt();
int[] arr=new int[num];
double sum=0;
for(int i=0;i<arr.length;i++){
arr[i]=sc.nextInt();
}
Arrays.sort(arr);
for(int i=1;i<arr.length-1;i++){
sum+=arr[i];
}
System.out.printf("%.2f",sum/(num-2));
}
}
31:黑马歌手大赛1
package 算法;
import java.util.Scanner;
/*
*某年某日,为了庆祝黑马程序员成立一百周年,西安黑马中心进行了一次青年歌手大奖赛,每位评委会给参赛选手打分。
选手得分规则为去掉一个最高分和一个最低分,然后计算平均得分,请编程实现,输出平均分,保留到小数点后两位。
输入
第一个数是n(2 < n < 100),表示评委的人数,然后是n个评委的打分,打分分数均为小于或等于100的正整数。
输出
输出平均得分,保留到小数点后两位
样例输入
10
1 2 3 4 5 6 7 8 9 10
样例输出
5.50
*
* */
public class 黑马歌手大赛1 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int num=sc.nextInt();
int max=1;
int min = 100;
double sum=0;
for (int i = 0; i < num; i++) {
int x=sc.nextInt();
if(max<x)
max=x;
if(min>x)
min=x;
sum+=x;
}
System.out.printf("%.2f",(sum-min-max)/(num-2));
}
}
32:猴子约瑟夫问题
package 算法;
import java.util.ArrayList;
import java.util.List;
/*
* 约瑟夫问题:有n只猴子,按顺时针方向围成一圈选大王(编号从1到n),从第1号开始报数,
* 一直数到m,数到m的猴子退出圈外,剩下的猴子再接着从1开始报数。就这样,直到圈内只剩下
* 一只猴子时,这个猴子就是猴王,编程求输入n,m后,输出最后猴王的编号。
* */
import java.util.Scanner;
public class 猴子约瑟夫问题 {
public static void main(String[] args) {
List list = new ArrayList();
while (true) {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
int y = sc.nextInt();
if (num == 0 && y == 0)
break;
if (y == 1) {
list.add(num);
}
int[] people = new int[num];
int x = 0;
for (int i = 0; i < people.length; i++) {
people[i] = 1;
}
if (y > 1) {
while (true) {
for (int i = 0; i < people.length; i++) {
if (people[i] == 1) {
x++;
if (x % y == 0) {
people[i] = 0;
num--;
}
}
}
if (num == 1) {
break;
}
}
}
for (int i = 0; i < people.length; i++) {
if (people[i] == 1&&y!=1) {
list.add(i + 1);
}
}
}
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
}
33:鸡兔同笼
package 算法;
import java.util.Scanner;
/*
*一个笼子里面关了鸡和兔子(鸡有2只脚,兔子有4只脚,没有例外)。已经知道了笼子里面脚的总数a,问笼子里面至少有多少只动物,至多有多少只动物。
输入
一行,一个正整数a (a < 32768)。
输出
一行,包含两个正整数,第一个是最少的动物数,第二个是最多的动物数,两个正整数用一个空格分开。
如果没有满足要求的答案,则输出两个0,中间用一个空格分开。
* */
public class 鸡兔同笼 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double x = sc.nextDouble();
int y = (int) Math.ceil(x / 4);
int z = (int) Math.ceil(x / 2);
//注意没有一只脚和三只脚的动物
if (x % 4 == 2 && x % 2 == 0)
System.out.println(y + " " + z);
else
System.out.println(0 + " " + 0);
}
}
34:计算2的N次方
package 算法;
import java.math.BigInteger;
import java.util.Scanner;
public class 计算2的N次方 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int x=sc.nextInt();
BigInteger bi=new BigInteger("2");
BigInteger sum=new BigInteger("1");
for (int i = 0; i < x; i++) {
sum=sum.multiply(bi);
}
System.out.println(sum.toString());
}
}
35:计算邮资
package 算法;
import java.util.Scanner;
/*
* 根据邮件的重量和用户是否选择加急计算邮费。计算规则:重量在1000克以内(包括1000克),
* 基本费8元。超过1000克的部分,每500克加收超重费4元,不足500克部分按500克计算;如果用户选择加急,多收5元。
* 输入一行,包含整数和一个字符,以一个空格分开,分别表示重量(单位为克)和是否加急。如果字符是y,
* 说明选择加急;如果字符是n,说明不加急。
* */
public class 计算邮资 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int x=sc.nextInt();
double money=0;
char m=sc.next().charAt(0);
//对重量进行判断
if(x>=1000)
//对于不满500的进行向上取整
money=8+Math.ceil((x-1000)/500.0)*4;
else
money=8;
//对是否加急进行判断,并转换为整型输出
if(m=='y')
System.out.println((int)(money+5));
else
System.out.println((int)money);
}
}
36:简单计算器
package 算法;
import java.util.Scanner;
//输入只有一行,共有三个参数,其中第1、2个参数为整数,第3个参数为操作符(+,-,*,/)。
//输出只有一行,一个整数,为运算结果。然而:
//1. 如果出现除数为0的情况,则输出:Divided by zero!
//2. 如果出现无效的操作符(即不为 +, -, *, / 之一),则输出:Invalid operator!
public class 简单计算器 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
char x = sc.next().charAt(0);
if (b == 0 && x == '/')
System.out.println("Divided by zero!");
else {
switch (x) {
case '+':
System.out.println(a + b);
break;
case '-':
System.out.println(a - b);
break;
case '*':
System.out.println(a * b);
break;
case '/':
System.out.println(a / b);
break;
default:
System.out.println("Invalid operator!");
break;
}
}
}
}
37:简单密码
package 算法;
import java.util.Scanner;
/*
Julius Caesar曾经使用过一种很简单的密码。对于明文中的每个字符,将它用它字母表中后5位对应的字符来代替,
这样就得到了密文。比如字符A用F来代替。如下是密文和明文中字符的对应关系。
密文
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
明文
V W X Y Z A B C D E F G H I J K L M N O P Q R S T U
你的任务是对给定的密文进行解密得到明文。
你需要注意的是,密文中出现的字母都是大写字母。密文中也包括非字母的字符,对这些字符不用进行解码
*/
public class 简单密码 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String str=sc.nextLine();
String str1="";
for (int i = 0; i < str.length(); i++) {
if('A'<=str.charAt(i)&&str.charAt(i)<'F'){
str1+=(char)(91-('A'-str.charAt(i)+5));
}
else if('F'<=str.charAt(i)&&str.charAt(i)<='Z'){
str1+=(char)(str.charAt(i)-5);
}
else
str1+=str.charAt(i);
}
System.out.println(str1);
}
}
38:将字符串中的小写字母转换成大写字母
package 算法;
import java.util.Scanner;
public class 将字符串中的小写字母转换成大写字母 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String str=sc.nextLine();
char[] strr=new char[str.length()];
for (int i = 0; i < str.length(); i++) {
if(str.charAt(i)>='a'&&str.charAt(i)<='z'){
strr[i]=(char)(str.charAt(i)-32);
}
else
strr[i]=str.charAt(i);
}
System.out.println(String.valueOf(strr));
}
}
39:角谷猜想
package 算法;
import java.util.Scanner;
/*
* 所谓角谷猜想,是指对于任意一个正整数,如果是奇数,则乘3加1,如果是偶数,则除以2,
* 得到的结果再按照上述规则重复处理,最终总能够得到1。如,假定初始整数为5,计算过程分别为
* 16、8、4、2、1。程序要求输入一个整数,将经过处理得到1的过程输出来。
*
* */
public class 角谷猜想 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int x=sc.nextInt();
while(x!=1){
if(x%2==1){
System.out.println(x+"*"+3+"+"+1+"="+(3*x+1));
x=3*x+1;
}
else{
System.out.println(x+"/"+2+"="+(x/2));
x=x/2;
}
}
System.out.println("End");
}
}
40:阶乘求和
package 算法;
import java.math.BigInteger;
import java.util.Scanner;
public class 阶乘求和 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int x=sc.nextInt();
BigInteger sum=new BigInteger("0");
for (int i = 1; i <= x; i++) {
BigInteger rid=new BigInteger("1");
for (int j = 1; j <= i; j++) {
BigInteger k=BigInteger.valueOf(j);
rid=rid.multiply(k);
}
sum=sum.add(rid);
}
System.out.println(sum.toString());
}
}
41:金币
package 算法;
import java.util.Scanner;
/*
* 国王将金币作为工资,发放给忠诚的骑士。第一天,骑士收到一枚金币;
* 之后两天(第二天和第三天)里,每天收到两枚金币;之后三天(第四、
* 五、六天)里,每天收到三枚金币;之后四天(第七、八、九、十天)
* 里,每天收到四枚金币……这种工资发放模式会一直这样延续下去:当连
* 续N天每天收到N枚金币后,骑士会在之后的连续N+1天里,每天收到
* N+1枚金币(N为任意正整数)。
你需要编写一个程序,确定从第一天开始的给定天数内,骑士一共获得了多少金币
*
* */
public class 金币 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int time=sc.nextInt(); //输入的是天数
int count=1; //表示第一天,之后两天,之后三天,之后四天......中的天数也代表每天收到的金币数
int sum=0; //虚拟总天数(判断time发工资的区间)
int money=0; //得到金币数
//当虚拟天数大于输入的天数时循环结束
while(true){
sum=sum+count;
if(sum<time)
count++;
else
break;
}
//计算前count天数的金币数(不包含count)1 2*2 3*3 4*4
for(int i=1;i<count;i++){
money=money+i*i;
}
//计算总金币数,即前count天的金币数+最后一天获得的金币数(由于最后一个count的天数不一定干满,所以)
money=money+(count-(sum-time))*count;
System.out.println(money);
}
}
42:奇数单增序列
package 算法;
import java.util.Arrays;
import java.util.Scanner;
/*
*给定一个长度为N(不大于500)的正整数序列,请将其中的所有奇数取出,并按升序输出。
输入
共2行:
第1行为 N;
第2行为 N 个正整数,其间用空格间隔。
输出
增序输出的奇数序列,数据之间以逗号间隔。数据保证至少有一个奇数。
样例输入
10
1 3 2 6 5 4 9 8 7 10
样例输出
1,3,5,7,9
*
* */
public class 奇数单增序列 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
// 定义整型数组用存放数字
int[] arr = new int[num];
// 定义奇数的个数
int y = 0;
// 循环输入整数
for (int i = 0; i < num; i++) {
int x = sc.nextInt();
// 判断输入的整数是否为奇数,如果为奇数则将其存放进数组,并将计数器加1
if (x % 2 == 1) {
arr[i] = x;
y++;
}
}
// 利用数组工具类对数组中的数字进行排序
Arrays.sort(arr);
// 输出数组中的奇数
for (int i = arr.length - y; i < arr.length; i++) {
// 判断加","的条件
if (i != arr.length - 1)
System.out.print(arr[i] + ",");
else
System.out.print(arr[i]);
}
}
}
43:晶晶赴约会
package 算法;
import java.util.Scanner;
//晶晶的朋友贝贝约晶晶下周一起去看展览,但晶晶每周的1、3、5有课必须上课,
//请帮晶晶判断她能否接受贝贝的邀请,如果能输出YES;如果不能则输出NO。
//输入有一行,贝贝邀请晶晶去看展览的日期,用数字1到7表示从星期一到星期日。
//输出有一行,如果晶晶可以接受贝贝的邀请,输出YES,否则,输出NO。注意YES和NO都是大写字母!
public class 晶晶赴约会 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int x=sc.nextInt();
if(x==1||x==3||x==5)
System.out.println("NO");
else
System.out.println("YES");
}
}
44:九九乘法表
package 算法;
import java.util.Scanner;
public class 九九乘法表 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.print("请输入你要打印的乘法表数:");
int x=sc.nextInt();
for (int i = 1; i <=x; i++) {
for (int j = 1; j <=i; j++) {
System.out.print(i+"*"+j+"="+i*j+" ");
}
System.out.println();
}
}
}
45:救援
package 算法;
import java.util.Scanner;
/*
* 救生船从大本营出发,营救若干屋顶上的人回到大本营,屋顶数目以及每个屋顶的坐标和人数都将由输入决定,
* 求出所有人都到达大本营并登陆所用的时间。在直角坐标系的原点是大本营,救生船每次从大本营出发,
* 救了人之后将人送回大本营。坐标系中的点代表屋顶,每个屋顶由其位置坐标和其上的人数表 示。救生船每次从大
* 本营出发,以速度50 米/分钟驶向下一个屋顶,达到一个屋顶后,救下其上的所有人,每人上船1 分钟,船原路
* 返回,达到大本营,每人下船0.5 分钟。假设原点与任意一个屋顶的连线不穿过其它屋顶。
*
* 第一行,一个整数,表示屋顶数n。接下来依次有n 行输入,每一行上包含两个表示屋顶相对于大本营的平面坐标位
* 置的实数(单位是米)、一个表示人数的整数,数之间以一个空格分开。
* 一行,救援需要的总时间,精确到分钟 (向上取整)。
* */
public class 救援 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
double[] num = new double[3 * x];
double times = 0;
for (int j = 0; j < x; j++) {
for (int i = 0; i < 3; i++) {
num[i] = sc.nextDouble();
}
times += 2 * Math.sqrt(num[0] * num[0] + num[1] * num[1]) / 50 + num[2] * 1.5;
}
System.out.println((int) Math.ceil(times));
}
}
46:买房子
package 算法;
import java.util.Scanner;
/*
* 某程序员开始工作,年薪N万,他希望在中关村公馆买一套60平米的房子,现在价格是200万,
* 假设房子价格以每年百分之K增长,并且该程序员未来年薪不变,且不吃不喝,不用交税,每年
* 所得N万全都积攒起来,问第几年能够买下这套房子?(第一年年薪N万,房价200万)
* 一行,包含两个正整数N(10 <= N <= 50), K(1 <= K <= 20),中间用单个空格隔开。
* 如果在第20年或者之前就能买下这套房子,则输出一个整数M,表示最早需要在第M年能买下,否则输出Impossible。
* */
public class 买房子 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = sc.nextInt();
double sum = 200;
int money = n;
int time = 1;
while (sum>money) {
time++;
money += n;
sum+=sum * k / 100;
if (time > 20) {
System.out.println("Impossible");
return;
}
}
System.out.println(time);
}
}
47:密码翻译
package 算法;
import java.util.Scanner;
/*
* 在情报传递过程中,为了防止情报被截获,往往需要对情报用一定的方式加密,简单的加密算法虽然不足以完全避免情报被破译,
* 但仍然能防止情报被轻易的识别。我们给出一种最简的的加密方法,对给定的一个字符串,把其中从a-y,A-Y的字母用其后继字母替代,
* 把z和Z用a和A替代,其他非字母字符不变,则可得到一个简单的加密字符串。
* */
public class 密码翻译 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
String strr = "";
char a = ' ';
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) >= 'a' && str.charAt(i) <= 'y' || str.charAt(i) >= 'A' && str.charAt(i) <= 'Y')
a = (char) (str.charAt(i) + 1);
else if(str.charAt(i)=='z')
a='a';
else if(str.charAt(i)=='Z')
a='A';
else
a=str.charAt(i);
strr = strr + a;
}
System.out.println(strr);
}
}
48:年龄与疾病
package 算法;
import java.util.Scanner;
/*
* 某医院想统计一下某项疾病的获得与否与年龄是否有关,需要对以前的诊断记录进行整理,
* 按照0-18、19-35、36-60、61以上(含61)四个年龄段统计的患病人数占总患病人数的比例。
* 共2行,第一行为过往病人的数目n(0 < n <= 100),第二行为每个病人患病时的年龄。
* 按照0-18、19-35、36-60、61以上(含61)四个年龄段输出该段患病人数占总患病人数的比例,
* 以百分比的形式输出,精确到小数点后两位。每个年龄段占一行,共四行。
*
* */
public class 年龄与疾病 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int x=sc.nextInt();
int y=0;
int a=0;
int b=0;
int c=0;
int d=0;
for(int i=0;i<x;i++){
y=sc.nextInt();
if(y>0&&y<=18)
a++;
if(y>=19&&y<=35)
b++;
if(y>=36&&y<=60)
c++;
if(y>=61)
d++;
}
System.out.printf("%.2f"+"%s"+"%.2f"+"%s"+"%.2f"+"%s"+"%.2f"+"%s",a*1.0*100/x,"%"+"\n",b*1.0*100/x,"%"+"\n",c*1.0*100/x,"%"+"\n",d*1.0*100/x,"%"+"\n");
}
}
49:排序查找一起练
package 算法;
import java.util.Arrays;
import java.util.Scanner;
/*
*对一个指定大小为n的整数序列,先进行排序(从小到大),然后通过二分查找来查找一个元素m,要去打印出排序后数组,
*元素在序列里的位置,并统计处查找的次数。输出格式请参考示例输出
输入
输入包括两行:
第一行输入整数n(n<500)和需要查找的数值m
第二行依次输入n个整数
输出
输出包括两行:
第一行输出 排序后的序列
第二行输出m在序列中位置和查找的次数,如果没有找到,请输出-1,两个数字间用空格隔开。
样例输入
10 5
11 12 91 38 20 5 23 99 12 4
样例输出
[4,5,11,12,12,20,23,38,91,99]
1 2
*
* */
public class 排序查找一起练 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int num=sc.nextInt();
int x=sc.nextInt();
int[] nums=new int[num];
int min = 0;
int max = nums.length - 1;
int mid = (min + max) / 2;
int count=1;
StringBuilder sb=new StringBuilder("[");
for (int i = 0; i < num; i++) {
nums[i]=sc.nextInt();
}
Arrays.sort(nums);
for (int i = 0; i < nums.length; i++) {
if(i==nums.length-1){
sb.append(nums[i]).append("]");
}else{
sb.append(nums[i]).append(",");
}
}
System.out.println(sb.toString());
while(nums[mid] != x) {
if(nums[mid] < x) {
min = mid + 1;
}else if (nums[mid] > x){
max = mid - 1;
}
mid = (min + max) / 2;
count++;
if(min > max) {
System.out.println(-1+" "+(count-1));
return;
}
}
System.out.println(mid+" "+count);
}
}
50:判断闰年
package 算法;
import java.util.Scanner;
public class 判断闰年 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int year=sc.nextInt();
if(year%4==0&&year%100!=0||year%400==0){
System.out.println("Y");
}
else
System.out.println("N");
}
}
51:判断正负
package 算法;
import java.util.Scanner;
//给定一个整数N,判断其正负。
//如果N > 0, 输出positive;
//如果N = 0, 输出zero;
//如果N < 0, 输出negative
public class 判断数正负 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int x=sc.nextInt();
if(x>0)
System.out.println("positive");
if(x==0)
System.out.println("zero");
if(x<0)
System.out.println("negative");
}
}
52:判断一个数能否同时被3和5整除
package 算法;
import java.util.Scanner;
//输入一行,包含一个整数n。( -1,000,000 < n < 1,000,000)
//输出一行,如果能同时被3和5整除输出YES,否则输出NO
public class 判断一个数能否同时被3和5整除 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int x=sc.nextInt();
if(x%3==0&&x%5==0)
System.out.println("YES");
else
System.out.println("NO");
}
}
53:判断字符串是否为回文
package 算法;
import java.util.Scanner;
/*
描述
输入一个字符串,输出该字符串是否回文。回文是指顺读和倒读都一样的字符串。
输入
输入为一行字符串(字符串中没有空白字符,字符串长度不超过100)。
输出
如果字符串是回文,输出yes;否则,输出no。
样例输入
abcdedcba
样例输出
yes
*
* */
public class 判断字符串是否为回文 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
StringBuffer str1 = new StringBuffer(str);
String str2 = str1.reverse().toString();
if (str.equals(str2)) {
System.out.println("yes");
} else {
System.out.println("no");
}
}
}
54:奇偶ASCII值判断
package 算法;
import java.util.Scanner;
//任意输入一个字符,判断其ASCII是否是奇数,若是,输出YES,否则,输出NO
//例如,字符A的ASCII值是65,则输出YES,若输入字符B(ASCII值是66),则输出NO
//如果其ASCII值为奇数,则输出YES,否则,输出NO
public class 奇偶ASCII值判断 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String y=sc.nextLine();
byte x=(byte)y.charAt(0);
if(x%2==1){
System.out.println("YES");
}else{
System.out.println("NO");
}
}
}
55:奇偶数判断
package 算法;
import java.util.Scanner;
//给定一个整数,判断该数是奇数还是偶数
public class 奇偶数判断 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int x=sc.nextInt();
if(x%2==0){
System.out.println("even");
}else{
System.out.println("odd");
}
}
}
56:输出绝对值
package 算法;
import java.util.Scanner;
//输入一个浮点数,输出这个浮点数的绝对值。
//输出这个浮点数的绝对值,保留到小数点后两位。
public class 输出绝对值 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.printf("%.2f",Math.abs(sc.nextDouble()));
}
}
57:奇数求和
package 算法;
import java.util.Scanner;
//两个数 m 和 n,两个数以一个空格分开,其中 0 <= m <= n <= 300 。
//输出一行,包含一个整数,表示m 到 n(包括m 和 n )之间的所有奇数的和
public class 奇数求和 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int m=sc.nextInt();
int n=sc.nextInt();
int sum=0;
for(int i=m;i<=n;i++){
if(i%2==1){
sum+=i;
}
}
System.out.println(sum);
}
}
58:骑车与走路
package 算法;
import java.util.Scanner;
/*
* 在北大校园里,没有自行车,上课办事会很不方便.但实际上,并非去办任何事情都是骑车快,
* 因为骑车总要找车、开锁、停车、锁车等,这要耽误一些时间.假设找到自行车,开锁并车上自
* 行车的时间为27秒;停车锁车的时间为23秒;步行每秒行走1.2米,骑车每秒行走3.0米。
* 请判断走不同的距离去办事,是骑车快还是走路快。
* 输出一行,如果骑车快,输出一行"Bike";如果走路快,输出一行"Walk";如果一样快,输出一行"All"。
* */
public class 骑车与走路 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
double x=sc.nextDouble();
double walk=x/1.2;
double bike=27+23+x/3.0;
if(bike==walk)
System.out.println("All");
else if(bike>walk)
System.out.println("Walk");
else
System.out.println("Bike");
}
}
59:求10000以内n的阶乘
package 算法;
import java.math.BigInteger;
import java.util.Scanner;
public class 求10000以内n的阶乘 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
BigInteger ride=new BigInteger("1");
for (int i = 1; i <=n; i++) {
BigInteger j=BigInteger.valueOf(i);
ride=ride.multiply(j);
}
System.out.println(ride);
}
}
60:求和
package 算法;
import java.util.Scanner;
/*
* 求Sn = a + aa + aaa + … + aa…a 的值(最后一个数中 a 的个数为 n ),其中 a 是一个1~9的数字,例如:
2 + 22 + 222 + 2222 + 22222 (此时 a=2 n=5 )
输入
一行,包括两个整数,第一个为a,第2个为n(1 ≤ a, n ≤ 9),以空格分隔。
*
* */
public class 求和 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
int a = sc.nextInt();
int sum = 0;
int sn = 0;
for(int j = 0;j < a ; j++){
sn = sn * 10 + num; //循环累加
sum=sum+sn;
}
System.out.println(sum);
}
}
61:求阶乘的和
package 算法;
import java.util.Scanner;
//给定正整数n,求不大于n的正整数的阶乘的和(即求1!+2!+3!+...+n!)
public class 求阶乘的和 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int sum=0;
for(int i=1;i<=n;i++){
int a=1;
for(int j=1;j<=i;j++){
a*=j;
}
sum+=a;
}
System.out.println(sum);
}
}
62:求整数的和与均值
package 算法;
import java.util.Scanner;
//输入第一行是一个整数n,表示有n个整数。
//第2~n+1行每行包含1个整数。每个整数的绝对值均不超过10000。
public class 求整数的和与均值 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int num=sc.nextInt();
int sum=0;
for(int i=0;i<num;i++){
sum+=sc.nextInt();
}
System.out.printf(sum+" "+"%.5f",(double)sum/num);
}
}
63:求最大最小值
package 算法;
import java.util.Scanner;
public class 求最大最小值 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] num=new int[3];
for(int i=0;i<3;i++){
num[i]=sc.nextInt();
}
int min=num[0];
for(int i=0;i<num.length;i++){
if(min<num[i]){
min=num[i];
}
}
System.out.println(min);
}
}
64:球弹跳高度的计算
package 算法;
import java.util.Scanner;
/*
* 一球从某一高度落下(整数,单位米),每次落地后反跳回原来高度的一半,再落下。
编程计算气球在第10次落地时,共经过多少米? 第10次反弹多高?
输入一个整数h,表示球的初始高度。
*
* 输出包含两行:
第1行:到球第10次落地时,一共经过的米数。
第2行:第10次弹跳的高度。
注意:结果可能是实数,结果用double类型保存。
提示:输出时不需要对精度特殊控制,用cout << ANSWER,或者printf("%g", ANSWER)即可。
*
*/
public class 球弹跳高度的计算 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
double high = x;
double h = x;
for (int i = 0; i < 10; i++) {
h *= 0.5;
if (i < 9) {
high += 2 * h;
}
}
System.out.printf("%g"+"\n"+"%g", high,h);
}
}
65:人口增长问题
package 算法;
import java.util.Scanner;
/*
*我国现有x亿人口,按照每年0.1%的增长速度,n年后将有多少人?
*一行,包含两个整数x和n,分别是人口基数和年数,以单个空格分隔。
*输出最后的人口数,以亿为单位,保留到小数点后四位。1 <= x <= 100, 1 <= n <= 100。
* */
public class 人口增长问题 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int x=sc.nextInt();
int n=sc.nextInt();
double sum=x;
for(int i=1;i<=n;i++){
sum=sum+sum*0.1/100;
}
System.out.printf("%.4f",sum);
}
}
66:删除单词后缀
package 算法;
import java.util.Scanner;
//给定一个单词,如果该单词以er、ly或者ing后缀结尾, 则删除该后缀(题目保证删除后缀后的单词长度不为0), 否则不进行任何操作
public class 删除单词后缀 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
if (str.endsWith("er") || str.endsWith("ly"))
System.out.println(str.substring(0, str.length() - 2));
else if (str.endsWith("ing"))
System.out.println(str.substring(0, str.length() - 3));
else
System.out.println(str);
}
}
67:石头剪刀布
package 算法;
import java.util.Scanner;
/*输入包含三行。
第一行包含三个整数:N,NA,NB,分别表示比了N轮,小A出拳的周期长度,小B出拳的周期长度。0 < N,NA,NB < 100。
第二行包含NA个整数,表示小A出拳的规律。
第三行包含NB个整数,表示小B出拳的规律。
其中,0表示“石头”,2表示“剪刀”,5表示“布”。相邻两个整数之间用单个空格隔开。
输出
输出一行,如果小A赢的轮数多,输出A;如果小B赢的轮数多,输出B;如果两人打平,输出draw。
对于测试数据,猜拳过程为:
样例输入
10 3 4
0 2 5
0 5 0 2
A:0 2 5 0 2 5 0 2 5 0
B:0 5 0 2 0 5 0 2 0 5
*/
public class 石头剪刀布 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
int[] x = new int[b];
int[] y = new int[c];
int[] x1 = new int[a];
int[] y1 = new int[a];
int m = 0;
int n = 0;
int aa = 0;
int bb = 0;
for (int i = 0; i < b; i++) {
x[i] = sc.nextInt();
}
for (int i = 0; i < c; i++) {
y[i] = sc.nextInt();
}
if (a <= b && a <= c) {
for (int i = 0; i < a; i++) {
if (x[i] - y[i] == -2 || x[i] - y[i] == -3 || x[i] - y[i] == 5)
aa++;
if (x[i] - y[i] == -5 || x[i] - y[i] == 2 || x[i] - y[i] == 3)
bb++;
}
} else {
if (a > b) {
for (int i = 0; i < a; i++) {
if (i % b == 0)
m = 0;
x1[i] = x[m];
m++;
}
}
if (a > c) {
for (int i = 0; i < a; i++) {
if (i % c == 0)
n = 0;
y1[i] = y[n];
n++;
}
}
for (int i = 0; i < a; i++) {
if(x1[i]-y1[i]==-2||x1[i]-y1[i]==-3||x1[i]-y1[i]==5)
aa++;
if(x1[i]-y1[i]==-5||x1[i]-y1[i]==2||x1[i]-y1[i]==3)
bb++;
}
}
if (aa > bb)
System.out.println("A");
else if (aa < bb)
System.out.println("B");
else
System.out.println("draw");
}
}
68:完数
package 算法;
import java.util.Scanner;
/*
* 一个数如果恰好等于它的因子之和,这个数就称为"完数"。例如,6的因子为1、2、3,而6=1+2+3,
* 因此6是"完数"。小明为了梦想,来到黑马程序员学习,点招老师为了测试他的基础水平,让他编程计算出N以内的所有的完数,
* 并按下面格式输出其因子:6 its fastors are 1 2 3
输入
输入一个正整数N,测试数据保证N>6
输出
N以内所有的完数及其因子
*
* */
public class 完数 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
for (int j = 6; j <= num; j++) {
int sum = 0;
String str=" its fastors are";
for (int i = 1; i <= j / 2; i++) {
if (j % i == 0){
sum += i;
str+=" "+i;
}
}
if (j == sum)
System.out.println(j+str);
}
}
}
69:输出最高分数的学生姓名
package 算法;
import java.util.Scanner;
/*
*输入学生的人数,然后再输入每位学生的分数和姓名,求获得最高分数的学生的姓名。
输入
第一行输入一个正整数N(N <= 100),表示学生人数。接着输入N行,每行格式如下:
分数 姓名
分数是一个非负整数,且小于等于100;
姓名为一个连续的字符串,中间没有空格,长度不超过20。
数据保证最高分只有一位同学。
输出
获得最高分数同学的姓名。
样例输入
5
87 lilei
99 hanmeimei
97 lily
96 lucy
77 jim
样例输出
hanmeimei
*
* */
public class 输出最高分数的学生姓名 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int num=sc.nextInt();
//定义存放成绩和姓名的数组
int[] grade=new int[num];
String[] name=new String[num];
int max=grade[0];
int count=0;
for (int i = 0; i < num; i++) {
grade[i]=sc.nextInt();
name[i]=sc.nextLine();
}
for (int i = 0; i < grade.length; i++) {
if(max<grade[i]){
max=grade[i];
count=i;
}
}
System.out.println(name[count].substring(1));
}
}
70:数1的个数
package 算法;
import java.util.Scanner;
/*
* 给定一个十进制正整数n,写下从1到n的所有整数,然后数一下其中出现的数字“1”的个数。
* 例如当n=2时,写下1,2。这样只出现了1个“1”;当n=12时,
* 写下1,2,3,4,5,6,7,8,9,10,11,12。这样出现了5个“1”。
* */
public class 数1的个数 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
int count = 0;
String str="";
String min="1";
for (int i = 1; i <= num; i++) {
str=str+i;
}
String now=str.replace(min, "");
System.out.println(str.length()-now.length());
}
}
71:数字模式的识别
package 算法;
import java.util.Scanner;
/*
*数字的模式是指在一堆给定数字中出现次数最多的数值,如5,5,5,3,3,2,6,4,它的模式就是5。现在你的任务,就是从数字中找到它的模式.
输入
第一行为整数N.从第二行开始为N个整数。对于输入的每个数,有( 0<= input_number <= 2000000 ).
输出
输出这些数字的模式,如果模式个数不为1,选择它们之中较小的
样例输入
8
5 5 5 3 3 2 6 4
样例输出
5
*
* */
public class 数字模式的识别 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
//定义整型数组用于存放数字
int[] arr = new int[num];
//循环控制数字存入数组
for (int i = 0; i < arr.length; i++) {
arr[i] = sc.nextInt();
}
//将x赋值为arr[0]
int x = arr[0];
//y表示相同数字出现的次数
int y = 1;
for (int i = 0; i < arr.length; i++) {
//count表示每轮相同数字出现的次数
int count = 1;
for (int j = i + 1; j < arr.length; j++) {
//出现相等的数字时count++
if (arr[i] == arr[j]) {
count++;
}
//如果后一个数字出现的次数大于前面相同数字的出现次数,则将后面数字出现的次数记录,并将数字记录
if (count > y) {
y = count;
x=arr[i];
}
}
//如果出现相同数字的次数相同则将数字小的记录
if(count==y){
if(x>arr[i])
x=arr[i];
}
}
System.out.println(x);
}
}
72:数字求和
package 算法;
import java.util.Scanner;
public class 数字求和 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int x=sc.nextInt();
int sum=0;
int y=0;
for (int i = 0; i < 5; i++) {
y=sc.nextInt();
if(y<x)
sum+=y;
}
System.out.println(sum);
}
}
73:数字统计
package 算法;
import java.util.Scanner;
/*
*请统计某个给定范围[L, R]的所有整数中,数字2出现的次数。比如给定范围[2, 22]
*数字2在数2中出现了1次,在数12中出现1次,在数20中出现1次,在数21中出现1次,
*在数22中出现2次,所以数字2在该范围内一共出现了6次。
* */
public class 数字统计 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int one=sc.nextInt();
int two=sc.nextInt();
String str="";
String min="2";
for(int i=one;i<=two;i++){
str=str+i;
}
String now=str.replace(min, "");
System.out.println(str.length()-now.length());
}
}
74:数组逆序重放
package 算法;
import java.util.Scanner;
//输入为两行:第一行数组中元素的个数n(1<n<100),第二行是n个整数,每两个整数之间用空格分隔。
public class 数组逆序重放 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
int[] y = new int[x];
for (int i = 0; i < x; i++) {
y[i] = sc.nextInt();
}
for (int i = 0; i < x; i++) {
System.out.print(y[x-i-1] + " ");
}
}
}
75:数组逆序重放1
package 算法;
import java.util.Scanner;
/*
*将一个数组中的值按逆序重新存放。例如,原来的顺序为8,6,5,4,1。要求改为1,4,5,6,8。
输入
输入为两行:第一行数组中元素的个数n(1<n<100),第二行是n个整数,每两个整数之间用空格分隔。
输出
输出为一行:输出逆序后数组的整数,每两个整数之间用空格分隔。
样例输入
5
8 6 5 4 1
样例输出
1 4 5 6 8
*
* */
public class 数组逆序重放1 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int num=sc.nextInt();
int[] arr=new int[num];
int x=0;
for (int i = 0; i < arr.length; i++) {
arr[i]=sc.nextInt();
}
for (int i = 0; i < arr.length/2; i++) {
x=arr[arr.length-1-i];
arr[arr.length-1-i]=arr[i];
arr[i]=x;
}
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]+" ");
}
}
}
76:谁考了第k名
package 算法;
import java.util.Scanner;
/*
*在一次考试中,每个学生的成绩都不相同,现知道了每个学生的学号和成绩,求考第k名学生的学号和成绩。
输入
第一行有两个整数,分别是学生的人数n(1≤n≤100),和求第k名学生的k(1≤k≤n)。
其后有n行数据,每行包括一个学号(整数)和一个成绩(浮点数),中间用一个空格分隔。
输出
输出第k名学生的学号和成绩,中间用空格分隔。(注:请用%g输出成绩)
样例输入
5 3
90788001 67.8
90788002 90.3
90788003 61
90788004 68.4
90788005 73.9
样例输出
90788004 68.4
*
* */
public class 谁考了第k名 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int num=sc.nextInt();
int k=sc.nextInt();
//定义整型数组用于存放学号
int[] id=new int[num];
//定义double类型数组用于存放成绩
double[] grade=new double[num];
for (int i = 0; i <num; i++) {
id[i]=sc.nextInt();
grade[i]=sc.nextDouble();
}
//根据成绩的大小利用冒泡排序法对其进行排序
for (int i = 0; i < grade.length; i++) {
for (int j = i+1; j <= grade.length-1; j++) {
if(grade[i]<grade[j]){
//对成绩进行交换
double temp=grade[i];
grade[i]=grade[j];
grade[j]=temp;
//对姓名进行交换
int st=id[i];
id[i]=id[j];
id[j]=st;
}
}
}
System.out.printf(id[k-1]+" "+"%g",grade[k-1]);
}
}
77:陶陶摘苹果
package 算法;
import java.util.Scanner;
/*
*包括两行数据。第一行包含10个100到200之间(包括100和200)的整数(以厘米为单位)
*分别表示10个苹果到地面的高度,两个相邻的整数之间用一个空格隔开。第二行只包括一个100
*到120之间(包含100和120)的整数(以厘米为单位),表示陶陶把手伸直的时候能够达到的最大高度。
* 包括一行,这一行只包含一个整数,表示陶陶能够摘到的苹果的数目。
*/
public class 陶陶摘苹果 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int[] m=new int[10];
int count=0;
for(int i=0;i<10;i++){
m[i]=sc.nextInt();
}
int n=sc.nextInt()+30;
for(int i=0;i<10;i++){
if(m[i]<=n)
count++;
}
System.out.println(count);
}
}
78:统计数字字符个数
package 算法;
import java.util.Scanner;
//输入一行字符,统计出其中数字字符的个数。
public class 统计数字字符个数 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String str=sc.nextLine();
int count=0;
for (int i = 0; i < str.length(); i++) {
if('0'<=str.charAt(i)&&str.charAt(i)<='9')
count++;
}
System.out.println(count);
}
}
79:统计水仙花数
package 算法;
import java.util.Scanner;
//计算1-100以内的水仙花个数,
public class 统计水仙花数 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int i=sc.nextInt();
for(int count=100;count<=i;count++){
int a=count/100;
int b=count/10%10;
int c=count%10;
if(a*a*a+b*b*b+c*c*c==count)
System.out.println(count);
}
}
}
80:整数序列的元素最大跨度值
package 算法;
import java.util.Arrays;
import java.util.Scanner;
public class 整数序列的元素最大跨度值 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int num=sc.nextInt();
int[] x=new int[num];
for(int i=0;i<num;i++){
x[i]=sc.nextInt();
}
Arrays.sort(x);
System.out.println(x[x.length-1]-x[0]);
}
}
81:校门外的树
package 算法;
import java.util.Scanner;
/*
某校大门外长度为L的马路上有一排树,每两棵相邻的树之间的间隔都是1米。我们可以把马路看成一个数轴
马路的一端在数轴0的位置,另一端在L的位置;数轴上的每个整数点,即0,1,2,……,L,都种有一棵树。
由于马路上有一些区域要用来建地铁。这些区域用它们在数轴上的起始点和终止点表示。已知任一区域的起始点
和终止点的坐标都是整数,区域之间可能有重合的部分。现在要把这些区域中的树(包括区域端点处的两棵树)
移走。你的任务是计算将这些树都移走后,马路上还有多少棵树。
第一行有两个整数L(1 <= L <= 10000)和 M(1 <= M <= 100),
L代表马路的长度,M代表区域的数目,L和M之间用一个空格隔开。接下来的M行每行包含两个不同的整数,
用一个空格隔开,表示一个区域的起始点和终止点的坐标。
对于20%的数据,区域之间没有重合的部分;
对于其它的数据,区域之间有重合的情况。
包括一行,这一行只包含一个整数,表示马路上剩余的树的数目。
* */
public class 校门外的树 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
int y = sc.nextInt();
int[] tree = new int[x+1];
int[] arr = new int[2 * y];
int count=0;
for (int i = 0; i < tree.length; i++) {
tree[i] = 1;
}
for (int i = 0; i < arr.length; i++) {
arr[i] = sc.nextInt();
}
for (int i = 0; i < tree.length; i++) {
for (int j = 0; j < arr.length; j+=2) {
if (arr[j] <= i && i <= arr[j + 1]) {
tree[i] = 0;
}
}
}
for (int i = 0; i < tree.length; i++) {
if(tree[i]==1)
count++;
}
System.out.println(count);
}
}
82:心
package 算法;
public class 心 {
public static void main(String[] args) {
System.out.println(" *** ***");
System.out.println(" ********** **********");
System.out.println("************ ************");
System.out.println("************* *************");
System.out.println("************** **************");
System.out.println("*************** ***************");
System.out.println(" ******* *** *** *******");
System.out.println(" ****** ***** ******");
System.out.println(" ****** *** ******");
System.out.println(" ****** * ******");
System.out.println(" ****** * ******");
System.out.println(" ***********");
System.out.println(" *********");
System.out.println(" *******");
System.out.println(" *****");
System.out.println(" *");
}
}
83:星系炸弹
package 算法;
/*
*
在X星系的广袤空间中漂浮着许多X星人造“炸弹”,用来作为宇宙中的路标。
每个炸弹都可以设定多少天之后爆炸。
比如:阿尔法炸弹2015年1月1日放置,定时为15天,则它在2015年1月16日爆炸。
有一个贝塔炸弹,2014年11月9日放置,定时为1000天,请你计算它爆炸的准确日期。
请填写该日期,格式为 yyyy-mm-dd 即4位年份2位月份2位日期。比如:2015-02-19
*
*/
import java.util.Scanner;
public class 星系炸弹 {
public static void main(String[] args) {
System.out.print("请输入炸弹放置的时间(yyyy-mm-dd):");
// 创建字符输入对象
Scanner sc = new Scanner(System.in);
// 接收用户输入的字符串
String time = sc.nextLine();
System.out.print("请定时(天):");
int num = Integer.parseInt(sc.nextLine());
// 以-为分割线将日期截取
String[] arr = time.split("-");
// 分别提取出年月日并将其转换为int类型
int year = Integer.parseInt(arr[0]);
int month = Integer.parseInt(arr[1]);
int day = Integer.parseInt(arr[2]);
num = num + day;
day = 0;
while (true) {
// 判断年份是闰年
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
if (month == 2) {
if (num > 28) {
month++;
num = num - 28;
}
else{
day=num;
break;
}
}
}
//判断年份是平年
if (!(year % 4 == 0 && year % 100 != 0 || year % 400 == 0)) {
if (month == 2) {
if (num > 27) {
month++;
num = num - 27;
}
else{
day=num;
break;
}
}
}
// 对月份为31天的进行操作
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
if (num > 31) {
month++;
num = num - 31;
if (month == 13) {
year++;
month = 1;
}
}
else{
day=num;
break;
}
}
// 对月份天数为30天的进行操作
if (month == 4 || month == 6 || month == 9 || month == 11) {
if (num > 30) {
month++;
num = num - 30;
}
else{
day=num;
break;
}
}
}
System.out.println("爆炸的时间为:"+year+"-"+month+"-"+day);
}
}
84:熊怪吃核桃
package 算法;
/*
*
森林里有一只熊怪,很爱吃核桃。不过它有个习惯,每次都把找到的核
桃分成相等的两份,吃掉一份,留一份。如果不能等分,熊怪就会扔掉
一个核桃再分。第二天再继续这个过程,直到最后剩一个核桃了,直接
丢掉。*/
public class 熊怪吃核桃 {
public static void main(String[] args) {
int a = 1543;
int b = 0;
while (a!=0) {
if(a%2==1){
a--;
b++;
}
else{
a=a/2;
}
}
System.out.println(b);
}
}
85:有趣的跳跃
package 算法;
import java.util.Scanner;
/*
* 一个长度为n(n>0)的序列中存在“有趣的跳跃”当前仅当相邻元素的差的绝对值经过排序后正好是从1到(n-1)。
* 例如,1 4 2 3存在“有趣的跳跃”,因为差的绝对值分别为3,2,1。当然,任何只包含单个元素的序列一定存在“有趣的跳跃”。
* 你需要写一个程序判定给定序列是否存在“有趣的跳跃”。
* 一行,第一个数是n(0 < n < 3000),为序列长度,接下来有n个整数,依次为序列中各元素,各元素的绝对值均不超过1,000,000,000。
* 一行,若该序列存在“有趣的跳跃”,输出"Jolly",否则输出"Not jolly"
* */
public class 有趣的跳跃 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
int[] jump = new int[x];
int[] z = new int[x - 1];
int count = 0;
for (int i = 0; i < jump.length; i++) {
jump[i] = sc.nextInt();
}
if (x < 3) {
System.out.println("Not jolly");
} else {
for (int i = 1; i < jump.length - 1; i++) {
if (jump[i] == jump[i - 1] && jump[i] == jump[i + 1]) {
System.out.println("Jolly");
System.exit(0);
}
if (Math.abs(jump[i] - jump[i - 1]) - Math.abs(jump[i] - jump[i + 1]) == 1) {
z[i - 1] = Math.abs(jump[i] - jump[i - 1]);
if (i == jump.length - 2)
z[i] = Math.abs(jump[i] - jump[i + 1]);
}
}
for (int i = 0; i < z.length; i++) {
if (z[i] == x - 1 - i) {
count++;
}
}
if (count == x - 1)
System.out.println("Jolly");
else
System.out.println("Not jolly");
}
}
}
86:与指定数字相同的数的个数
package 算法;
import java.util.Scanner;
//输入包含2行:
//第1行为N和m,表示整数序列的长度(N <= 100)和指定的数字, 中间用一个空格分开;
//第2行为N个整数,整数之间以一个空格分开。
public class 与指定数字相同的数的个数 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int[] x=new int[a];
int count=0;
for(int i=0;i<a;i++){
x[i]=sc.nextInt();
}
int b=sc.nextInt();
for (int i = 0; i < x.length; i++) {
if(x[i]==b)
count++;
}
System.out.println(count);
}
}
87:约瑟夫问题
package 算法;
import java.util.Scanner;
/*
*
* 据说著名犹太历史学家 Josephus有过以下的故事:在罗马人占领乔塔帕特后,39 个犹太人与Josephus及他的
* 朋友躲到一个洞中,39个犹太人决定宁愿死也不要被敌人到,于是决定了一个自杀方式,41个人排成一个圆圈,由第1
* 个人开始报数,每报数到第3人该人就必须自杀,然后再由下一个重新报数,直到所有人都自杀身亡为止。
* 问题:编号为那俩个数字的人存活
* */
public class 约瑟夫问题 {
public static void main(String[] args) {
System.out.print("请输入参与游戏的猴数:");
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
System.out.print("请输入报数的大小:");
int y=sc.nextInt();
int[] people = new int[num];
int x = 0;
for (int i = 0; i < people.length; i++) {
people[i] = 1;
}
while (true) {
for (int i = 0; i < people.length; i++) {
if (people[i] == 1) {
x++;
if (x % y == 0) {
people[i] = 0;
num--;
}
}
}
if (num == 1) {
break;
}
}
for (int i = 0; i < people.length; i++) {
if (people[i] == 1) {
System.out.print(i + 1);
}
}
}
}
88:找第一个只出现一次的字符
package 算法;
import java.util.Scanner;
//给定一个只包含小写字母的字符串,请你找到第一个仅出现一次的字符。如果没有,输出no。
public class 找第一个只出现一次的字符 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String str=sc.nextLine();
for (int i = 0; i < str.length(); i++) {
int count=0;
for (int j = 0; j < str.length(); j++) {
if(str.charAt(i)==str.charAt(j)){
count++;
}
}
if(count==1){
System.out.println(str.charAt(i));
break;
}
if(i==str.length()-1){
System.out.println("no");
break;
}
}
}
}
89:整理药名
package 算法;
import java.util.Scanner;
/*
*医生在书写药品名的时候经常不注意大小写,格式比较混乱。现要求你写一个程序将医生书写混乱的药品名整理成
*统一规范的格式,即药品名的第一个字符如果是字母要大写,其他字母小写。如将ASPIRIN、aspirin整理成Aspirin。
输入
第一行一个数字n,表示有n个药品名要整理,n不超过100。
接下来n行,每行一个单词,长度不超过20,表示医生手书的药品名。药品名由字母、数字和-组成。
*
* */
public class 整理药名 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int x = Integer.parseInt(sc.nextLine());
String[] arr = new String[x];
String[] newarr = new String[x];
String y = "";
for (int i = 0; i < x; i++) {
arr[i] = sc.nextLine();
}
for (int i = 0; i < arr.length; i++) {
y = arr[i].toLowerCase();
if (y.charAt(0) >= 'a' && y.charAt(0) <= 'z')
newarr[i] = (char) (y.charAt(0) - 32) + y.substring(1);
else
newarr[i] = y;
}
for (int i = 0; i < newarr.length; i++) {
System.out.println(newarr[i]);
}
}
}
90:整数的个数
package 算法;
import java.util.Scanner;
//输入有两行:第一行包含一个正整数k,第二行包含k个正整数,每两个正整数用一个空格分开。
//输出有三行,第一行为1出现的次数,,第二行为5出现的次数,第三行为10出现的次数
public class 整数的个数 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int x=sc.nextInt();
int a=0;
int b=0;
int c=0;
for(int i=0;i<x;i++){
int y=sc.nextInt();
if(y==1)
a++;
if(y==5)
b++;
if(y==10)
c++;
}
System.out.print(a+"\n"+b+"\n"+c);
}
}
91:整数去重
package 算法;
import java.util.Scanner;
/*
*给定含有n个整数的序列,要求对这个序列进行去重操作。所谓去重,是指对这个序列中每个重复出现的数,只保留该数第一次出现的位置,删除其余位置。
输入
输入包含两行:
第一行包含一个正整数n(1 <= n <= 20000),表示第二行序列中数字的个数;
第二行包含n个整数,整数之间以一个空格分开。每个整数大于等于10、小于等于100。
输出
输出只有一行,按照输入的顺序输出其中不重复的数字,整数之间用一个空格分开。
样例输入
5
10 12 93 12 75
样例输出
10 12 93 75
*
* */
public class 整数去重 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
//定义整型数组用于存放不相等的整数
int[] nums = new int[num];
//定义所用到的数组长度计数器
int count = 1;
//先x为数组中第一个元素的值
int x = sc.nextInt();
nums[0] = x;
//如果整数的数量为1则直接输出x
if (num == 1)
System.out.println(x);
else {
//以数组的长度减1控制循环
for (int i = 1; i < nums.length; i++) {
//z代表不相等的数的个数,每循环一次值变为0
int z=0;
int y=sc.nextInt();
//输入的y值一次和存入数组的每一个数值比较
for (int j = 0; j < count; j++) {
//若果不相等则计数器加1
if(y!=nums[j]){
z++;
}
}
//如果计数器的值等于数组的长度,则代表都不相等
if(z==count){
//将y的值存入数组中
nums[count]=y;
//所用到的数组长度加1
count++;
}
}
//循环输出数组中的元素
for (int i = 0; i < count; i++) {
System.out.print(nums[i]+" ");
}
}
}
}
92:最长最短单词
package 算法;
import java.util.Scanner;
/*
*输入1行句子(不多于200个单词,每个单词长度不超过100),只包含字母、空格和逗号。
*单词由至少一个连续的字母构成,空格和逗号都是单词间的间隔。
试输出第1个最长的单词和第1个最短单词。
输入
一行句子。
输出
两行输出:
第1行,第一个最长的单词。
第2行,第一个最短的单词。
样例输入
I am studying Programming language C in Peking University
样例输出
Programming
I
*
* */
public class 最长最短单词 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// 输入一个字符串,将","全部转换为空格便于分割字符串
String str1 = sc.nextLine().replace(',', ' ');
//将字符串相连的多个空格转换为一个空格
String str=str1.replaceAll(" +", " ");
// 以空格将字符串分割出来
String[] arr = str.split(" ");
// 定义最长最短单词的初始长度,以及存储变量
int max = arr[0].length();
int min = arr[0].length();
String maxStr = arr[0];
String minStr = arr[0];
// 找出长度最长的单词
for (int i = 1; i < arr.length; i++) {
if (max < arr[i].length()) {
max = arr[i].length();
maxStr = arr[i];
}
}
// 找出长度最短的单词
for (int i = 1; i < arr.length; i++) {
if (min > arr[i].length()) {
min = arr[i].length();
minStr = arr[i];
}
}
System.out.print(maxStr+"\r\n"+minStr);
}
}
93:字符串处理
package 算法;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/*1、(黑马点招面试题):有类似这样的字符串:"1.2,3.4,5.6,7.8,5.56,44.55"请按照要求,依次完成以下试题。
(1)以逗号作为分隔符,把已知的字符串分成一个String类型的数组,数组中的每一个元素类似于"1.2","3.4"这样的字符串
(2)把数组中的每一个元素以"."作为分隔符,把"."左边的元素作为key,右边的元素作为value,封装到Map中,
Map中的key和value都是Object类型。
(3)把map中的key封装的Set中,并把Set中的元素输出。
(4)把map中的value封装到Collection中,把Collection中的元素输出。*/
public class 字符串处理 {
public static void main(String[] args) {
String str="1.2,3.4,5.6,7.8,5.56,44.55";
//将字符串以","切割
String[] arr=str.split(",");
//创建Map集合
Map<Object, Object> m=new HashMap<Object, Object>();
for (int i = 0; i < arr.length; i++) {
String[] num=arr[i].split("\\.");
m.put(num[0], num[1]);
}
Set<Object> key = m.keySet();
for (Object o : key) {
System.out.print(o+" ");
}
System.out.println();
Collection<Object> values = m.values();
for (Object o : values) {
System.out.print(o+" ");
}
}
}
94:字符串判等
package 算法;
import java.util.Scanner;
/*
*判断两个由大小写字母和空格组成的字符串在忽略大小写,且忽略空格后是否相等。
输出
若两个字符串相等,输出YES,否则输出NO。
*
* */
public class 字符串判等 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String str=sc.nextLine().replace(" ", "");
String strr=sc.nextLine().replace(" ", "");
if(str.equalsIgnoreCase(strr))
System.out.println("YES");
else
System.out.println("NO");
}
}
95:字符替换
package 算法;
import java.util.Scanner;
/*
只有一行,由一个字符串和两个字符组成,中间用单个空格隔开。字符串是待替换的字符串,字符串长度小于等于30个字符,且不含空格等空白符;
接下来一个字符为需要被替换的特定字符;
接下来一个字符为用于替换的给定字符。
*/
public class 字符替换 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String str=sc.next();
char a=sc.next().charAt(0);
char b=sc.next().charAt(0);
System.out.println(str.replace(a, b));
}
}
96:组合数
package 算法;
import java.util.Scanner;
/**
* 从4个人中选2个人参加活动,一共有6种选法。
* 从n个人中选m个人参加活动,一共有多少种选法?
*/
public class 组合数 {
public static void main(String[] args) {
System.out.println("请输入两个整数,且前一个大于后一个!");
Scanner sc=new Scanner(System.in);
System.out.println(f(sc.nextInt(),sc.nextInt()));
}
public static int f(int m,int n){
if(n>m) return 0;
if(n==0) return 1;
return f(m-1,n-1)+f(m-1,n);
}
}
97:最大值和最小值的差
package 算法;
import java.util.Arrays;
import java.util.Scanner;
/*
*输出一个整数序列中最大的数和最小的数的差。
输入
第一行为M,表示整数个数,整数个数不会大于10000;
第二行为M个整数,以空格隔开,每个整数的绝对值不会大于10000。
输出
输出M个数中最大值和最小值的差。
样例输入
5
2 5 7 4 2
样例输出
5
*
* */
public class 最大值和最小值的差 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
// 定义数组用于存放输入的整数
int[] arr = new int[num];
for (int i = 0; i < arr.length; i++) {
arr[i] = sc.nextInt();
}
if (num == 1)
System.out.println(0);
else {
Arrays.sort(arr);
System.out.println(arr[arr.length - 1] - arr[0]);
}
}
}
98:最高的分数
package 算法;
import java.util.Arrays;
import java.util.Scanner;
//输入两行,第一行为整数n(1 <= n < 100),表示参加这次考试的人数.第二行是这n个学生的成绩,
//相邻两个数之间用单个空格隔开。所有成绩均为0到100之间的整数。
public class 最高的分数 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
int[] great = new int[num];
for (int i = 0; i < num; i++) {
great[i] = sc.nextInt();
}
Arrays.sort(great);
System.out.println(great[great.length - 1]);
}
}
99:用户提醒
package 算法;
import java.util.Scanner;
/*给你写个需求:键盘录入一个数,判断各种可能性,是字符串提示用户输入的是字符串,
是负数提示用户输入的是负数,是小数提示用户为小数,最终把整数打印在控制台上
判断是字符串或者负数小数时,不仅提示用户信息,还要继续录入整数,知道录入整数为止*/
public class String_demo {
public static void main(String[] args) {
// 创建键盘录入对象
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个数:");
while (true) {
String str = sc.nextLine();
// 字符串正则验证式
String regex = "[a-zA-Z]+";
// 整数正则验证式
String regex1 = "[1-9]+[0-9]+";
if (str.length() > 1) {
if (!str.equals(str.replaceAll(regex, ""))) {
if (!(str.charAt(0) > '1' && str.charAt(0) < '9')) {
System.out.println("您录入的是字符串,请再次输入:");
continue;
}else {
System.out.println("输入错误");
}
}
}
}
}
}
100:忘记题目
package 算法;
import java.util.Scanner;
public class ad {
public static void main(String[] args) {
//创建键盘输入对象
Scanner sc = new Scanner(System.in);
//用户提示
System.out.println("请输入字符串:");
//接收用户输入的字符串
String line = sc.nextLine();
//调用字符串格式化方法
format(line);
}
public static void format(String line) {
//先干掉空格,只留一个空格
String regex = "\\s+"; //匹配空格正则
line = line.replaceAll(regex, " ");
line = line.replaceAll("(\\d+)", "_$1_"); //匹配数字,并进行替换
String[] split = line.split(" "); //分割成字符串数组
String s = ""; //定义一个空串
for (int i = 0; i < split.length; i++) { //遍历字符串数组
//字符串数组中的每一个元素首字母大写,其他小写,链式编程
String s1 = split[i].substring(0,1).toUpperCase().concat(split[i].substring(1).toLowerCase());
s += s1 + " "; //将转换后的字符串追加到s中,并加" "
}
System.out.println(s); //打印结果
}
}
101:过河问题
package 算法;
import java.util.Arrays;
/*
*在漆黑的夜里,N位旅行者来到了一座狭窄而且没有护栏的桥边。如果不借助手电筒的话,大家是无论如何也不敢过桥去的。
*不幸的是,N个人一共只带了一只手电筒,而桥窄得只够让两个人同时过。如果各自单独过桥的话,N人所需要的时间已知;
*而如果两人同时过桥,所需要的时间就是走得比较慢的那个人单独行动时所需的时间。问题是,如何设计一个方案,让这N人尽快过桥。
输入
第一行是一个整数T(1<=T<=20)表示测试数据的组数
每组测试数据的第一行是一个整数N(1<=N<=1000)表示共有N个人要过河
每组测试数据的第二行是N个整数Si,表示此人过河所需要花时间。(0 输出
输出所有人都过河需要用的最少时间
样例输入
1
4
1 2 5 10
样例输出
17
*
* */
import java.util.Scanner;
public class Crossing_River {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// num代表测试次数
int num = sc.nextInt();
//定义数组,用于存放每次测试的时间
int[] sum=new int[num];
for (int j = 0; j < num; j++) {
// x代表人数
int x = sc.nextInt();
// 定义数组用于存放每个人过河所用的时间
int[] time = new int[x];
// 定义过河总时间
int times = 0;
for (int i = 0; i < x; i++) {
time[i] = sc.nextInt();
}
// 利用数组工具类对数组进行升序排序
Arrays.sort(time);
// 判断人数,如果小于等于3则字节算出过河时间
if (x == 1)
times = time[0];
else if (x == 2)
times = time[1];
else if (x == 3)
times = time[0] + time[1] + time[2];
else {
// 判断人数是奇数还是偶数,偶数的话最后加1个时间;奇数的话最后加3个时间
times = 0;
int y = 0;
// 用人数除以2减1控制循环次数
for (int i = 0; i < x / 2 - 1; i++) {
times += 2 * time[1] + time[0] + time[time.length - 1 - y];
y += 2;
}
if (x % 2 == 0)
times += time[1];
else
times += time[1] + time[0] + time[time.length - 1 - y];
}
sum[j]=times;
}
//输出时间
for (int i = 0; i < num; i++) {
System.out.println(sum[i]);
}
}
}
102:第n个数能否被3整除
package 算法;
import java.util.Scanner;
// F(0) = 7, F(1) = 11, F(n) = F(n-1) + F(n-2) (n>=2).
//求第n个数能否被3整除,能的话输出yes;不能的话输出no
public class Fibonacci {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int x=sc.nextInt();
if((x-2)%4==0)
System.out.println("yes");
else
System.out.println("no");
}
}