校验和与打印金字塔(JAVA)

2021.03.29上机课
题目描述:
在这里插入图片描述
代码:
3.9 检查ISBN-10

package Practice;

import java.util.Scanner;

public class Ch03Q09 {
    public static void main(String[] args) {
        while(true)
        {
            Scanner input = new Scanner(System.in);

            System.out.print("Enter the first 9 digits of an ISBN as integer: ");
            int number = input.nextInt();
            int checksum = 0;

            // 校验和计算
            for(int i = 1; i <= 9; i ++ ) checksum += ( (number / (int) Math.pow(10, 9 - i) ) % 10) * i;
            checksum %= 11;

            System.out.print("The ISBN-10 number is ");

            // 处理前导0与该数
            for(int i = 8; i >= 0; i -- )
            {
                if(number / (int) Math.pow(10, i) == 0) System.out.print("0");
            }
            System.out.print(number);
            // 输出最后一位
            if (checksum == 10)
                System.out.print("X");
            else
                System.out.print(checksum);
            System.out.println();
        }
    }
}
// 013601267
// 013031997

5.19 打印金字塔

package Practice;

import java.util.Scanner;

public class Ch05Q19 {

    public static void main(String[] args) {
        while(true){
            long number = 0;

            Scanner scanner = new Scanner(System.in);
            System.out.print("Enter the number of lines:");
            int counts = scanner.nextInt();
            long d = (long)Math.pow(2, counts);
            int t = 0;
            while(d != 0)
            {
                d /= 10;
                t ++;
            }
            t += 2;

            String fmt = "%" + t + "s";
            String fmt1 = "%" + t + "d";
            // System.out.println(fmt + fmt1);

            for (int row = 0; row <= counts - 1; row ++ ) {
                // 打印空白
                for (int col = 1; col <= counts - 1 - row; col ++ )
                    System.out.printf(fmt, " ");

                // 打印左半部分
                for(int col = 0; col <= row; col ++ )
                {
                    number = (long)Math.pow(2, col);
                    System.out.printf(fmt1, number);
                }

                // 打印右半部分
                for(int col = row - 1; col >= 0; col --)
                {
                    number = (long)Math.pow(2, col);
                    System.out.printf(fmt1, number);
                }

                System.out.print('\n');
            }
        }

    }
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值