循环算法题*3

 输出n行*组成的等腰三角形。

public class IsoscelesTriangleDomo {
    public static void main(string[] args){
        //这里假设输入为5
        printTriangle(5);
    }
    publie static void printTriangle(int n){
        for(int i = 1; i <= n; i++){
            // 打印前导空格
            for(int j = 1; j <= n - 1; j++){
                System.out.print(" ");
            }
            // 打印*
            for (int k = 1:k <= 2 * i - 1; k++){
                System.out.print("*");
            }
            // 换行
            System.out.println();
        }
    }
}    

九九乘法表

public class MultiplicationTableDemo{
    public static void main(string[] args){
        int m = 9;
        int n = 9;
        String str = m + "*" + n + "=" + m*n;
        int maxLength = str.length();

        for(int i=1; i<=m; i++){
            for(int j=1; j<=i; j++){
                str = String.format("%-"+ maxLength + "s\t",i + "*" + j + "=" + i*j);
                System.out.print(str);
            }
            System.out.println();
        }
    }
}

百钱买百鸡

有一人去买鸡,公鸡每只5元,母鸡每只3元,小鸡每3只1元,用100元买100只鸡,要求公鸡、母鸡、小鸡都要有,各买多少只?

public class ChickenDemo {
    public static void main(String[]args){
        int totalMoney = 100;//总金额
        int totalChickens=100://总鸡数
        for(int roosters = 1; roosters <= totalMoney / 5; roosters++){
            for(int hens = 1; hens <= totalMoney / 3; hens++){
                int chicks = totalChickens - roosters - hens;
                int cost = roosters * 5 + hens * 3 + chicks / 3;

                // 检查是否满足条件
                if(chicks >= 1 && chicks % 3 == 0 && cost == totalMoney){
                    System.out.println("公鸡:"+ roosters +"只,母鸡:"+ hens +"只,小鸡:"+ chicks +"只");
                }
            }
        }
    }
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值