day03练习题代码

29 篇文章 0 订阅

7-1

import java.util.Scanner;

public class S7_01 {
	static int n, ans = 0, tem = 1;
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		n = sc.nextInt();
		for (int i = 1; i <= n; i++) {
			tem =tem * 2; //记录 2的i 次方
			ans += tem;
		}
		System.out.println("result = " + ans);
	}
}

7-2

import java.util.Scanner;

public class S7_02 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String str = sc.nextLine();    // Long.parseLong("10010", 2)  将2进制转化为 一个long型数
		System.out.println(Long.parseLong(str.substring(0,8),2) +"." + Long.parseLong(str.substring(8,16),2) + "."
				+ Long.parseLong(str.substring(16,24),2) + "." + Long.parseLong(str.substring(24,32),2));
	}
}

7-3

import java.util.Scanner;

public class S7_03 {
	static char[] rec; 
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		rec = sc.nextLine().toCharArray();
		boolean f = true; //判断-号前面是数 为true 是操作符为false
		for (int i = 0; i < rec.length; i++) {
			if (isNum(rec[i])) {
				while (i < rec.length && isNum(rec[i]))System.out.print(rec[i++]);
				i--; //多加了一次
				System.out.println(); //换行
				
			} else if (rec[i] == '-' || rec[i] == '+') {
				
				f = true; //默认是数
				if (i - 1 < 0 || (!isNum(rec[i - 1]) && rec[i - 1] != ')')){ // ) 是例外,虽然是操作符   i - 1 < 0 直接当做正负号处理
					// + - 号前面是操作符
					f = false;
				}
				if (f) {
					System.out.println( rec[i]);
				} else {
					//是操作符当做数的正负号处理
					System.out.print(rec[i]);
				}
			} else {
				System.out.println(rec[i]);
			}
		}
	}
	static boolean isNum(char c) {
		return c >= '0' && c <= '9' || c == '.';
	}
}

7-4

import java.util.Scanner;

public class S7_04 {
	static long N;
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		N = sc.nextLong();
		if (N == 1) {
			System.out.println(N + "=1");
			return;
		}
		System.out.print(N + "=");
		for (int i = 2; N != 1; i++) { //从2开始枚举 能被除就加上
			int ans = 0;
			while (N  % i == 0) {
				N /= i; 
				ans++; //记录能被当前数 除以多少次
			}
			if (ans == 1) {
				System.out.print(i);
				System.out.print(N == 1 ? "" : "*");  
			} else if (ans > 1) {
				System.out.print(i + "^" + ans);  //若大于1 就输出多少次方
				System.out.print(N == 1 ? "" : "*");
			}
		} 
	}	
}

7-5

import java.util.Scanner;

public class S7_05 {
	static long[] left = new long[93];
	static long[] right = new long[93];
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt(); 
		//初始化    left[1]代表1阶的时候左脚到达终点的方法数  right[2]代表2阶的时候右脚到达终点的方法数
		left[1] = left[2] = right[2] = 1;
		for (int i = 3; i <= 92; i++) {
			left[i] = right[i - 1] + right[i - 2]; //左脚到达终点   那么说明在i-1阶 或者 i-2阶 的时候踏上去的是 右脚
			right[i] = left[i - 1] + left[i - 2];
		}
		System.out.println(left[n]);
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值