1.完全平方数AABB
package xiaoti;
public class AABB_PerfectSquare {
public static void main(String[] args) {
/*T:满足AABB的四位数称为完全平方数,
* 即百位和千位相等、个位和十位相等,请输出所有的完全平方数*/
int a,b,c,d;
for(int i=10;i<=99;i++) {
int x=i*i;
a=x%10;//个位
b=x/10%10;//10
c=x/100%10;//100
d=x/1000%10;//1000
//&&x==d*1000+c*100+b*10+a 不用i*i=dcba=AABB
if(a==b&&c==d) {
System.out.print(x+" "+i);
}
}
}
}
2.小球的无限反弹
package xiaoti;
import java.security.PublicKey;
import java.util.Iterator;
import java.util.Scanner;
public class Ball_rebound {
//背记版本
/*一球从某一高度h落下(单位米),每次落地后反跳回原来高度的一半,再落下。
*
* 编程计算气球在第10次落地时,共经过多少米? 第10次反弹多高?
* 输出:包含两行,第1行:到球第10次落地时,一共经过的米数。
* 第2行:第10次弹跳的高度 */
public static void main(String[] args) {
System.out.print("请输入球的初始高度:");
Scanner in=new Scanner(System.in);
double h=in.nextDouble();
int n=10;
sum(h, n);
}
public static void sum(double h1,int n1) {
double s=h1; //注意类型范围 int = double 高转低
double y=h1;
double x=h1;
for(int i=1;i<=n1;i++) {
y=y/2.0;
s+=y*2;
x/=2.0;
}
System.out.println("第"+n1+"次落地时一共进过"+s+"米");
System.out.print("第"+n1+"次反弹"+x+"米");
}
/*
public static void main(String[] args) {
double s = 100;//总距离
double high = 100;//第一次高度
int n = 10; //次数
double d = 0;//每次落地之间间隔的距离
for(int i = 1;i <= n;i++) {
s += d;
high /= 2.0;
d = high*2;
}
System.out.println("共走过:"+s+"米");
System.out.println("第"+n+"次反弹的高度是:"+high);
}
*/
/*
public static void main(String[] args) {
/*一球从某一高度h落下(单位米),每次落地后反跳回原来高度的一半,再落下。
*
* 编程计算气球在第10次落地时,共经过多少米? 第10次反弹多高?
* 输出:包含两行,第1行:到球第10次落地时,一共经过的米数。
* 第2行:第10次弹跳的高度 *
System.out.print("请输入球的初始高度:");
Scanner in=new Scanner(System.in);
double h=in.nextDouble();
int n=10;
sum(h, n);
//sum_h(h,n);
//rebound_h(h,n);
}
public static void sum(double h1,int n1) {
double s=h1; //注意类型范围 int = double 高转低
double y=h1;
double x=h1;
for(int i=1;i<=n1;i++) {
y=y/2.0;
s=s+y*2;
x/=2.0;
}
System.out.println("第"+n1+"次落地时一共进过"+s+"米");
System.out.print("第"+n1+"次反弹"+x+"米");
}
*/
//
/*
//第n次落地时一共进过的米数
//public static ???? 没有static 报错
public static void sum_h(double h1,int n1) {
double s=h1; //注意类型范围 int = double 高转低
double y=h1;
for(int i=1;i<=n1;i++) {
y/=2.0;
s=s+y*2;
}
System.out.println("第"+n1+"次落地时一共进过"+s+"米");
}
//第n次反弹多高
public static void rebound_h(double h1,int n1) {
double x=h1;
for(int i=1;i<=n1;i++) {
x=x/2.0;
}
System.out.print("第"+n1+"次反弹"+x+"米");
} */
}
3.费用恰好用完
package xiaoti;
import java.sql.Blob;
import java.util.Scanner;
public class Buy_something {
public static void main(String[] args) {
//求最佳方案 正好把钱花完
/*
System.out.print("请输入班费有多少钱:");
Scanner in=new Scanner(System.in);
int x=in.nextInt();
int count=0;
Buy_somethingpen Buy=new Buy_somethingpen();
for(int R=1;R<=x/5;R++)
for(int Blu=1;Blu<=(x-5*R)/3;Blu++)
for(int Bla=1;Bla<=(x-5*R-3*Bla)/2;Bla++) {
if(x==Buy.getRedpen()*R+Buy.getBluepen()*Blu
+Buy.getBlackpen()*Bla) {
System.out.println("班费:"+x+"正好可以买redpen"+R+"支"
+"bluepen"+Blu+"支"+"blackpen"+Bla+"支");
count++;
}
}
if(count>0) System.out.println("共有套"+count+"方案");
else System.out.println("无合适方案"); */
//不分类写
System.out.print("请输入班费有多少钱:");
Scanner in=new Scanner(System.in);
int x=in.nextInt();
int count=0;
for(int R=1;R<=x/5;R++)
for(int Blu=1;Blu<=(x-5*R)/3;Blu++)
for(int Bla=1;Bla<=(x-5*R-3*Bla)/2;Bla++) {
if(x==5*R+3*Blu+2*Bla) {
System.out.println("班费:"+x+"5元笔"+R+"支"
+"3元笔"+Blu+"支"+"3元笔"+Bla+"支");
count++;
}
}
if(count>0) System.out.println("共有套"+count+"方案");
else System.out.println("无合适方案");
}
}
4.求MAX MIN
package xiaoti;
import java.util.Scanner;
public class Max_Min {
public static void main(String[] args) {
int max,min;
/*Scanner in=new Scanner(System.in);
System.out.print("请输入数组元素的个数:");
int n=in.nextInt();
int []a=new int[n];
for(int i=0;i<a.length;i++) {
//[i]a=in. 错误!!!!!
a[i]=in.nextInt();
}*/
//int []a=new int[]{1,2,3,451,564};
int []a={1,2,3,451,564};
min=a[0];
max=a[0];
for(int i=1;i<a.length;i++) {
if(max<a[i]) max=a[i];
if(min>a[i]) min=a[i];
}
System.out.println("max="+max);
System.out.println("min="+min);
}
}
5.水仙花数
package Library_ManagementSystem;
public class Books {
private int serNum;
private String title;
private String author;
private double price;
public Books() {
}
public Books(int serNum, String title, String author, double price) {
this.serNum = serNum;
this.title = title;
this.author = author;
this.price = price;
}
public int getSerNum() {
return serNum;
}
public void setSerNum(int serNum) {
this.serNum = serNum;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public void show(){
System.out.println("编号:"+serNum+" 书名:《" + title + "》 作者:"
+ author + " "+ "价格:" + price);
}
}
package Library_ManagementSystem;
//Library
import java.util.ArrayList;
import java.util.Scanner;
/*
* 1、提供操作菜单,可以选择要进行的操作。
2、可以添加图书,添加图书时,编号需要唯一,添加成功,返回到菜单。
3、可以查询图书,显示所有图书信息,然后返回到菜单。
4、可以根据书名,查询单本图书信息,显示信息后,返回到菜单。
5、可以删除图书,通过编号删除,删除成功后,返回到菜单。
6、可以修改图书的信息,但编号不可以修改,修改成功后,返回到菜单。
7、可以退出系统,结束程序运行。
*
*
* */
public class Library {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ArrayList<Books> list = new ArrayList<>();
while (true) {
System.out.println("===========图书馆里系统============");
System.out.println("1.添加图书");
System.out.println("2.查询图书");
System.out.println("3.查询单本");
System.out.println("4.删除图书");
System.out.println("5.修改图书");
System.out.println("6.退出系统");
System.out.println("请输入序号:(1-6)");
int choice = sc.nextInt();
boolean aNull = isNull(list);
switch (choice) {
case 1:
// System.out.println("添加图书");
addBoo(sc,list);
break;
case 2:
// System.out.println("查询所有图书");
if(aNull){
lookBooks(list);
}else {
System.out.println("请先添加图书!");
}
break;
case 3:
// System.out.println("查询单本图书");
if(aNull){
lookDanBook(list,sc);
}else {
System.out.println("请先添加图书!");
}
break;
case 4:
// System.out.println("删除图书");
if(aNull){
reBook(list,sc);
}else {
System.out.println("请先添加图书!");
}
break;
case 5:
// System.out.println("修改图书");
if(aNull){
changBook(list,sc);
}else {
System.out.println("请先添加图书!");
}
break;
case 6:
System.out.println("欢迎使用");
System.exit(0);
default:
System.out.println("输入有误,请输入(1-6)");
break;
}
}
}
// 判断集合是否 有值
public static boolean isNull(ArrayList<Books> list){
if(list.size() == 0 || list == null){
// 没有值
return false;
}
return true;
}
// 判断是否存在
public static int isHave(ArrayList<Books> list,int id){
for (int i = 0; i < list.size(); i++) {
// 如果存在
if (id == list.get(i).getSerNum()){
return i;
}
}
// 不存在返回 -1
return -1;
}
// 添加图书
public static void addBoo(Scanner sc, ArrayList<Books> list) {
int id = 0;
while (true) {
System.out.println("请输入图书编号:");
id = sc.nextInt();
int index = isHave(list, id);
if(index == -1){
break;
}else {
System.out.println("该图书编号已经存在,请重新输入!");
}
}
System.out.println("请输入图书的书名:");
String name = sc.next();
System.out.println("请输入图书的作者:");
String author = sc.next();
System.out.println("请输入图书的价格:");
double price = sc.nextDouble();
Books b1 = new Books(id,name,author,price);
list.add(b1);
System.out.println("添加成功!");
}
// 查看所有图书
public static void lookBooks(ArrayList<Books> list){
// System.out.println("================================");
// System.out.println(" 编号 书名 作者 价格");
for (int i = 0; i < list.size(); i++) {
list.get(i).show();
}
}
// 查看单个图书 根据编号查找
public static void lookDanBook(ArrayList<Books> list,Scanner sc){
System.out.println("请输入要查看的图书编号:");
int lookBookId = sc.nextInt();
// 查找是否存在 存在 返回索引 不存在 -1
int index = isHave(list, lookBookId);
if(index != -1){
// System.out.println(" 编号 书名 作者 价格");
list.get(index).show();
}else {
System.out.println("您要查找的图书不存在!");
}
}
// 删除图书
public static void reBook(ArrayList<Books> list,Scanner sc){
System.out.println("请输入要删除的图书编号:");
int reId = sc.nextInt();
// 进行判断是否存在 存在 返回但前值 不存在 -1
int index = isHave(list, reId);
if(index != -1){
Books b1 = list.get(index);
list.remove(b1);
System.out.println("删除成功!!");
}else {
System.out.println("您要删除的图书不存在!");
}
}
// 修改图书
public static void changBook(ArrayList<Books> list, Scanner sc){
System.out.println("请输入要修改的图书编号:");
int changeId = sc.nextInt();
// 进行判断是否存在 存在 返回但前值 不存在 -1
int index = isHave(list, changeId);
if(index != -1){
System.out.println("请输入该图书新的图书名:");
String newName = sc.next();
System.out.println("请输入改图书新的作者:");
String newAuthor = sc.next();
System.out.println("请输入该图书新的价格:");
double newPrice = sc.nextDouble();
Books b1 = new Books(changeId,newName,newAuthor,newPrice);
list.set(index,b1);
System.out.println("修改成功!");
}else {
System.out.println("您输入的图书不存在!!");
}
}
}