练习

1.请实现一个算法
确定一个字符串的所有字符串是否全都不同。这里我们要求不允许使用额外的存储结构。
给定一个string iniString,请返回一个boolean值,True代表所有的字符全都不同,False代表存在相同的字符。

package cn.lrf.practice;

import java.util.Scanner;

public class StringIssame {
    public static void main(String[] args) {
        System.out.println("请输出一个字符串:");
        Scanner sc = new Scanner(System.in);
        String iniString = sc.next();
        boolean b = stringIssame(iniString);
    }

    public static boolean stringIssame(String iniString) {
        for (int i = 0; i < iniString.length(); i++) {
            char ch = iniString.charAt(i);
            int index = iniString.lastIndexOf(ch);
            if (i != index) {
                System.out.println("存在相同的字符");
                return false;
            }
        }
        System.out.println("所有的字符都不同");
        return true;
    }
}

在这里插入图片描述
2.给定一个string iniString,请返回一个string ,为翻转后的字符串。

package cn.lrf.practice;

import java.util.Scanner;

public class TurnString {
    public static void main(String[] args) {
        System.out.println("请输出一个需要翻转的字符串");
        Scanner sc = new Scanner(System.in);
        String iniString = sc.next();
        System.out.println("翻转的字符串:");
        for (int i = iniString.length()-1; i >=0; i--) {
            System.out.print(iniString.charAt(i));
        }
    }
}

在这里插入图片描述
3.有数量不限的硬币,币值为25分,10分,5分和1分,请编写代码计算给定一个int数字,返回各种硬币各有几枚(最少枚数)。

package cn.lrf.practice;

import java.util.Scanner;

public class CountMoney {
    public static void main(String[] args) {
        System.out.println("设定有25分,10分,5分,1分硬币规格,请给一个分值(int),给出各硬币数量(最少枚数)");
        Scanner sc = new Scanner(System.in);
        int input = sc.nextInt();
        int num = 25;
        int num1 = 10;
        int num2 = 5;
        int num3 = 1;
        int numCount = input / num;
        int num1Count = input % num / num1;
        int num2Count = input % num % num1 / num2;
        int num3Count = input % num % num1 % num2 / num3;
        System.out.println("币值为25分:" + numCount + "个,10分:" + num1Count + "个,5分:" + num2Count + "个和1分:" + num3Count + "个。");
    }
}

在这里插入图片描述

4.for循环案例——计算水仙花数的个数

在这里插入图片描述

package practice;

/*
    水仙花数是指一个 3 位数,
    它的每个位上的数字的 3次幂之和等于它本身(
    例如:1^3 + 5^3+ 3^3 = 153)。
 */
public class NarcissisticNumber {
    public static void main(String[] args) {
        //需求————打印所有的水仙花数的个数
        //1.定义一个计数器,用来记录水仙花数的个数
        int count = 0;
        //2.获取到所有的三位数
        for (int i = 100; i < 1000; i++) {
            //i记录的就是所有的三位数
            //3.获取到该数字的个位,十位,百位数字
            int ge = i % 10;
            int shi =i/10%10;
            int bai = i/10/10%10;
            //4.判断该数字是否是水仙花数,如果是,计数器自增1
            if(ge*ge*ge+shi*shi*shi+bai*bai*bai==i){
                //能走到这里,说明i是一个水仙花数
//                count=count+1;
//                count++;
                ++count;
            }
        }
        //5.打印计数器的结果即可
        System.out.println("水仙花数的个数是:"+count);
    }
}

在这里插入图片描述

5.用do while循环模拟 练习知识点的过程

在这里插入图片描述

package practice;

/*需求:
 * 用do while循环模拟 练习知识点的过程
 * 要求:
 * 至少练习一次,并且练习次数不小于3次就表示这个知识点你学会了*/
public class DoWhileDemo {
    public static void main(String[] args) {
//1.定义一个变量,记录练习次数
        int count = 1;
        //2.定义一个变量,用来标记是否学会这个知识点。true:学会了,false:没学会
        boolean isOK = false;
        //3.在do while 循环中模拟这个动作
        do {
            //输出练习次数
            System.out.println("正在进行第" + count + "此练习");
            //判断练习次数,看是否不小于三,如果条件成立,表示学会了
            if (count >= 3) {
                //能进来,表示学会了
                //将boolean类型变量的值改为:true
                isOK = true;
            }

            //不管是否学会,每练习一次,次数要+1
            count++;
        } while (!isOK);
    }
}

在这里插入图片描述

6.数组练习

在这里插入图片描述

package practice;

/*需求:给定一个int数组,找出它的最大元素
 * 思路
 * 从第一个元素开始,一次于后面的元素比较,
 * 每次都将最大值存储在临时变量中,比较完成
 * 后临时变量即为最大值*/
public class ArrayDemo {
    public static void main(String[] args) {
        //1.定义int[]
        int[] arr = {1, 3, 5, 2, 4, 6};
        //2.定义一个临时变量temp,用来表示最大值
        int temp = arr[0];
        //3.通过for循环遍历数组,获取到每一个数据
        for (int i = 0; i < arr.length; i++) {
            //4.把获取到的数据依此和 temp进行比较,并将最大的值赋值给temp
            if (arr[i] > temp) {
                temp = arr[i];
            }
        }
        //5.for循环执行结束后,temp记录的就是最大值,打印即可
        System.out.println("数组中的最大元素为:"+temp);
    }
}

在这里插入图片描述

7.创建矩阵A

1 2 4
3 4 5
7 5 3
创建矩阵B
3 4 6
1 2 8
1 2 5

1、导入jama.jar包。
2、实现A+B。并打印。
求出A的转置。并打印。
求矩阵A的秩,并解释矩阵的秩。并打印。

package practice;

import Jama.Matrix;

public class JuZhen {
    public static void main(String[] args) {
        double[][] arry1 = {
                {1, 2, 4},
                {3, 4, 5},
                {7, 5, 3}};
        double[][] arry2 = {
                {3, 4, 6},
                {1, 2, 8},
                {1, 2, 5}};
        Matrix A = new Matrix(arry1);
        Matrix B = new Matrix(arry2);
        Matrix D = A.plus(B);//矩阵相加
        System.out.println("矩阵A加B:");
        D.print(4, 0);
        Matrix m1 = A.transpose();
        System.out.println("A的转置:");
        m1.print(4, 0);
        int n = A.rank();
        System.out.println("矩阵A的秩:");
        System.out.println(n);
    }

}

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值