ZZULIOJ 1114-1130 数组专题 参考代码

一共17道题
下面代码供参考

每题前面标的有题号
1114 逆序
1115 数组最小值
1116 删除元素
1117 查找数组元素
1118 数列有序
1119 一维数组排序
1120 最值交换
1121 电梯
1122 小明的调查作业
1123 最佳校友
1124 两个有序数组合并
1125 上三角矩阵的判断
1126 布尔矩阵的奇偶性
1127 矩阵乘积
1128 课程平均分
1129 第几天
1130 杨辉三角

1114
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		int n;
		int[]a = new int[11];
		n = cin.nextInt();
		for (int i = 0; i < n; i++)
			a[i] = cin.nextInt();
		for (int i = n - 1; i >= 0; i--)
			System.out.printf("%4d", a[i]);
	}

}
1115
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		int n, num, k, ans;
		n = cin.nextInt();
		num = cin.nextInt();
		ans = num;
		k = 0;
		for (int i = 1; i < n; i++)
		{
			num = cin.nextInt();
			if (num < ans)
			{
				ans = num;
				k = i;
			}
		}
		System.out.println(ans + " " + k);
	}
}
1116
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		int n, num, k;
		int[] a = new int[11];
		n = cin.nextInt();
		for (int i = 0; i < n; i++)
		{
			a[i] = cin.nextInt();
		}
		k = cin.nextInt();
		for (int i = 0; i < n; i++)
		{
			if (i == k)
				continue;
			else
			{
				if (i != n - 1)
					System.out.print(a[i] + " ");
				else
					System.out.print(a[i]);
			}
		}
	}
1117
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		int n, k = -1, need;
		int[] a = new int[11];
		n = cin.nextInt();
		for (int i = 0; i < n; i++)
		{
			a[i] = cin.nextInt();
		}
		need = cin.nextInt();
		for (int i = 0; i < n; i++)
		{
			if (a[i] == need)
			{
				k = i;
				break;
			}
		}
		if (k == -1)
			System.out.println("Not Found");
		else
		{
			for (int i = 0; i < n; i++)
			{
				if (i == k)
					continue;
				else
					System.out.printf("%4d", a[i]);
			}
		}
	}

}
1118
import java.util.Arrays;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		int n;
		n = cin.nextInt();
		int[] a = new int[n + 1];
		for (int i = 0; i <= n; i++)
			a[i] = cin.nextInt();
		Arrays.sort(a);
		for (int i = 0; i <= n; i++)
			if (i != n)
				System.out.print(a[i] + " ");
			else
				System.out.print(a[i]);
	}

}
1119
import java.util.Arrays;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		int n;
		n = cin.nextInt();
		int[] a = new int[n];
		for (int i = 0; i < n; i++)
			a[i] = cin.nextInt();
		Arrays.sort(a);
		for (int i = 0; i < n; i++)
			if (i != n - 1)
				System.out.print(a[i] + " ");
			else
				System.out.print(a[i]);
	}

}
1120
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		int n, MAX, MIN, k = 0;
		n = cin.nextInt();
		int[]a = new int[11];
		for (int i = 0; i < n; i++)
			a[i] = cin.nextInt();
		MAX = MIN = a[0];
		for (int i = 0; i < n; i++)
		{
			if (a[i] < MIN)
			{
				MIN = a[i];
				k = i;
			}
		}
		int t;
		t = a[0];
		a[0] = a[k];
		a[k] = t;
		for (int i = 0; i < n; i++)
		{
			if (a[i] > MAX)
			{
				MAX = a[i];
				k = i;
			}
		}
		t = a[n - 1];
		a[n - 1] = a[k];
		a[k] = t;
		for (int i = 0; i < n; i++)
			System.out.print(a[i] + " ");
	}

}
1121
import java.util.Scanner;

public class Main {


	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		int[] a = new int[1010];
		int n, ans = 0;
		n = cin.nextInt();
		for (int i = 0; i < n; i++)
			a[i] = cin.nextInt();
		ans += a[0] * 6;
		for (int i = 1; i < n; i++)
		{
			if (a[i] > a[i - 1])
				ans += (a[i] - a[i - 1]) * 6;
			else
				ans += (a[i - 1] - a[i]) * 4;
		}
		System.out.println(ans + n * 5);
	}

}
1122
import java.util.Arrays;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		int n, cnt = 0, num;
		int[] vis = new int[1010];
		int[] a = new int[1010];
		n = cin.nextInt();
		for (int i = 0; i<n; i++)
		{
			num = cin.nextInt();
			if (vis[num] == 0)
			{
				vis[num] = 1;
				a[cnt++] = num;
			}
		}
		Arrays.sort(a, 0, cnt);
		System.out.println(cnt);
		for (int i = 0; i<cnt; i++)
			System.out.print(a[i] + " ");
	}

}
1123
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		int[] a = new int[100];
		int num, MAX = 0;
		while (cin.hasNext())
		{
			num = cin.nextInt();
			if (num == -1)
				break;
			a[num]++;
			MAX = Math.max(a[num], MAX);
		}
		for (int i = 0; i <= 99; i++)
			if (a[i] == MAX)
				System.out.print(i + " ");
	}

}
1124
import java.util.Arrays;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		int[] a = new int[2000010];
		int[] b = new int[1000010];
		int n, m;
		n = cin.nextInt();
		for (int i = 0; i < n; i++)
			a[i] = cin.nextInt();
		m = cin.nextInt();
		for (int i = 0; i < m; i++)
			b[i] = cin.nextInt();
		for (int i = n; i - n < m; i++)
		{
			a[i] = b[i - n];
		}
		Arrays.sort(a, 0, n + m);
		for (int i = n + m - 1; i >= 0; i--)
			System.out.print(a[i] + " ");
	}
}
1125
import java.util.Scanner;

public class Main {

	public static int j(int k)
	{
		int sum = 0;
		for (int i = 1; i <= k; i++)
			sum += i;
		return sum;
	}
	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		int[][] a = new int[11][11];
		int n, cnt = 0;
		n = cin.nextInt();
		for (int i = 0; i < n; i++)
			for (int j = 0; j < n; j++)
			{
				int num = cin.nextInt();
				if (i != 0 && j != n - 1)
				{
					if (j < i&&num == 0)
						cnt++;
				}
			}
		if (n*n - j(n) == cnt)
			System.out.print("YES");
		else
			System.out.print("NO");
	}
}
1126
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		int n;
		int[][] a = new int[110][110];
		n = cin.nextInt();
		int flag1 = 0, flag2 = 0, u = 0, v = 0;
		for (int i = 0; i<n; i++)
			for (int j = 0; j<n; j++)
			{
				a[i][j] = cin.nextInt();
			}
		for (int i = 0; i<n; i++)
		{
			int sum = 0;
			for (int j = 0; j<n; j++)
			{
				sum += a[i][j];
			}
			if (sum % 2 != 0)
			{
				flag1 = 1;
				u = i;
			}
		}
		for (int i = 0; i<n; i++)
		{
			int sum = 0;
			for (int j = 0; j<n; j++)
			{
				sum += a[j][i];
			}
			if (sum % 2 != 0)
			{
				flag2++;
				v = i;
			}
		}
		if (flag1 == 0 && flag2 == 0)
			System.out.println("OK");
		else
			if (flag1 == 1 && flag2 == 1)
				System.out.printf("Change bit(%d,%d)", u, v);
			else
				System.out.println("Corrupt");
	}

}
1127
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		int m, n, k;
		int[][] a = new int[11][11];
		int[][] b = new int[11][11];
		int[][] c = new int[11][11];
		m = cin.nextInt();
		n = cin.nextInt();
		k = cin.nextInt();
		for (int i = 0; i < m; i++)
			for (int j = 0; j < n; j++)
				a[i][j] = cin.nextInt();
		for (int i = 0; i < n; i++)
			for (int j = 0; j < k; j++)
				b[i][j] = cin.nextInt();
		for (int i = 0; i < m; i++)
			for (int j = 0; j < k; j++)
			{
				int sum = 0;
				for (int u = 0; u < n; u++)
					sum += a[i][u] * b[u][j];
				c[i][j] = sum;
			}
		for (int i = 0; i < m; i++)
		{
			for (int j = 0; j < k; j++)
				System.out.print(c[i][j] + " ");
			System.out.println();
		}

	}
}
1128

import java.util.Scanner;

public class Main {
	public static void main(String args[])
	{
		Scanner cin = new Scanner(System.in);
		int m, n;
		double[][] b = new double[1010][1010];
		double[]a = new double[1010];
		m = cin.nextInt();
		n = cin.nextInt();
		for (int i = 0; i < m; i++)
			for (int j = 0; j < n; j++)
				b[i][j] = cin.nextDouble();
		for (int i = 0; i < n; i++)
		{
			double sum = 0;
			for (int j = 0; j < m; j++)
				sum += b[j][i];
			a[i] = sum / m;
		}
		for (int i = 0; i < n; i++)
			System.out.printf("%.2f ", a[i]);
	}
}
1129
import java.util.Scanner;

public class Main {
	public static int is_leap_year(int year)
	{
		if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
			return 1;
		return 0;
	}
	public static void main(String[] args)
	{
		int a[][] = { { 0, 0 }, { 31, 31 }, { 28, 29 }, { 31, 31 }, { 30, 30 }, { 31, 31 }, { 30, 30 }, { 31, 31 },
		{ 31, 31 }, { 30, 30 }, { 31, 31 }, { 30, 30 }, { 31, 31 } };
		Scanner cin = new Scanner(System.in);
		String date = cin.nextLine();
		String[] s = date.split("-");
		int cnt = 0, year = 0, month = 0, day = 0;
		for (String i : s)
		{
			//System.out.println(i+" "+cnt);
			if (cnt == 0)
				year = Integer.parseInt(i);
			if (cnt == 1)
				month = Integer.parseInt(i);
			if (cnt == 2)
				day = Integer.parseInt(i);
			cnt++;
		}
		// System.out.println(year+" "+month+" "+day);
		int flag = 0;
		if (is_leap_year(year) == 1)
			flag = 1;
		int sum = day;
		for (int i = 1; i < month; i++)
			sum += a[i][flag];
		System.out.println(sum);
	}
}
1130
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		int[][] a = new int[40][40];
		Scanner cin = new Scanner(System.in);
		int n = cin.nextInt();
		for (int i = 0; i < n; i++)
		{
			a[i][0] = a[i][i] = 1;
			for (int j = 1; j < i; j++)
				a[i][j] = a[i - 1][j - 1] + a[i - 1][j];
		}
		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j <= i; j++)
				System.out.print(a[i][j] + " ");
			System.out.println();
		}
	}
}
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这是一道经典的位运算题目,考察对二进制的理解和位运算的熟练程度。 题目描述: 给定一个长度为 $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、付费专栏及课程。

余额充值