ZZULIOJ JAVA T1050-T1059

本文集展示了多个Java编程示例,涉及阶乘求和、幂次方运算、数列计算、最大值查找等数学与逻辑问题。通过具体代码实现,加深了对算法的理解与实践。

ZZULIOJ JAVA T1050-T1059

T1050

import java.util.Scanner;

public class T1050 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner in  = new Scanner(System.in);
		int x = in.nextInt();
		in.close();
		long a=1,sum=0;
		for(int i=1;i<=x;i++ ) {
			a*=i;
			sum+=a;
		}
		System.out.println(sum);

	}

}

T1051

import java.util.Scanner;

public class T1051 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner in  = new Scanner(System.in);
		double x = in.nextDouble();
		int y = in.nextInt();
		in.close();
		double sum = x;
		for(int i=0;i<y-1;i++) {
			sum +=Math.pow(x, 1.0/2);
			x=Math.pow(x, 1.0/2);
		}
		
		System.out.printf("%.2f",sum);
			

	}

}

T1052

import java.util.Scanner;

public class T1052 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner in = new Scanner(System.in);
		int n= in.nextInt();
		int a= in.nextInt();
		in.close();
		int sum=0;
		int t = a;
		for(int i=1;i<=n;i++) {
			
			sum+=a;
			a=a*10+t;
			
		}
		System.out.println(sum);

	}

}```

## T1053

```java
import java.util.Scanner;
  
public class T1053 {
  
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner in = new Scanner(System.in);
        double x = in.nextDouble();
        in.close();
        double sum = 0.0;
        double a=1.0,b=1.0;
        double x0=x;
        for(int i =1;i<11;i++) {
             
            sum += x*a/b;
            a = -a;
            x = x*x0*x0;
            b = b*(i*2)*(i*2+1);
        }
          
        System.out.printf("%.3f", sum);
  
    }
  
}

T1054

import java.util.Scanner;

public class T1054 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner in  = new Scanner(System.in);
		int x = in.nextInt();
		in.close();
		int sum =1;
		
		for(int i=1;i<x;i++) {
			sum =(sum+1)*2; 
		
		}
		System.out.println(sum);
			

	}

}

T1055

import java.util.Scanner;

public class T1055 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner in = new Scanner(System.in);
		int n  =in.nextInt();
		in.close();
		int a[] = new int[45];
		a[1] =1;
		a[2] =1;
		if(n<3)
			System.out.println(a[n]);
		else {
			for(int i =3;i<=n;i++) {
				a[i] = a[i-1] +a[i-2];
			}
			System.out.println(a[n]);
		}
	}

}

T1056

import java.util.Scanner;

public class T1056 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner in  = new Scanner(System.in);
		int x = in.nextInt();
		int y = in.nextInt();
		in.close();
		int max =0;
		int f =1;
		
		for(int i=x;i<=y;i++) {
			if(i%7==0&&i%4!=0) {
				max =i;
				f=-1;
			}
		
		}
		if(f==1)
			System.out.println("no");
		else
			System.out.println(max);

	}

}

T1057

import java.util.Scanner;

public class T1057 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner in  = new Scanner(System.in);
		int x = in.nextInt();
		in.close();
		int f =1;
		if(x==1)
			f=-1;
		
		for(int i=2;i<=Math.sqrt(x);i++) {
			 if(x%i==0)
				 f=-1;
		
		}
		if(f==1)
			System.out.println("Yes");
		else
			System.out.println("No");

	}

}

T1058

import java.util.Scanner;

public class T1058 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner in  = new Scanner(System.in);
		long x = in.nextInt();
		in.close();
		long a=1,sum=0;
		for(int i=1;i<=100;i++ ) {
			a*=i;
			sum+=a;
			if(sum>=x) {
				System.out.println("m<="+(i-1));
			    break;
			}
				

		}
	
	}

}

T1059

import java.util.Scanner;

public class T1059 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int a;
		int b=0;
		
		Scanner in = new Scanner(System.in);
		do {
			a =in.nextInt();
			b =Math.max(a, b);
		}while(a>=0);
		System.out.println(b);
		in.close();
		
		
		

	}

}
这是一道经典的位运算题目,考察对二进制的理解和位运算的熟练程度。 题目描述: 给定一个长度为 $n$ 的数组 $a$,初始时每个数的值都为 $0$。现在有 $m$ 个操作,每个操作为一次询问或修改。 对于询问,给出两个整数 $l,r$,求 $a_l \oplus a_{l+1} \oplus \cdots \oplus a_r$ 的值。 对于修改,给出一个整数 $x$,表示将 $a_x$ 的值加 $1$。 输入格式: 第一行两个整数 $n,m$。 接下来 $m$ 行,每行描述一次操作,格式如下: 1 l r:表示询问区间 $[l,r]$ 的异或和。 2 x:表示将 $a_x$ 的值加 $1$。 输出格式: 对于每个询问操作,输出一个整数表示答案,每个答案占一行。 数据范围: $1 \leq n,m \leq 10^5$,$0 \leq a_i \leq 2^{30}$,$1 \leq l \leq r \leq n$,$1 \leq x \leq n$ 输入样例: 5 5 2 1 2 3 1 2 4 2 2 1 1 5 输出样例: 0 2 解题思路: 对于询问操作,可以利用异或的性质,即 $a \oplus b \oplus a = b$,将 $a_l \oplus a_{l+1} \oplus \cdots \oplus a_r$ 转化为 $(a_1 \oplus \cdots \oplus a_{l-1}) \oplus (a_1 \oplus \cdots \oplus a_r)$,因为两个前缀异或后的结果可以相互抵消,最后的结果即为 $a_1 \oplus \cdots \oplus a_{l-1} \oplus a_1 \oplus \cdots \oplus a_r = a_l \oplus \cdots \oplus a_r$。 对于修改操作,可以将 $a_x$ 对应的二进制数的每一位都分离出来,然后对应位置进行修改即可。由于只有加 $1$ 操作,所以只需将最后一位加 $1$ 即可,其余位不变。 参考代码:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值