P1428 小鱼比可爱
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = input.nextInt();
}
int count = 0;int ans = 0;
for (int i = 0; i < n; i++) {
for (int j = i; j >= 0; j--) {
if (arr[i] > arr[j]) {
count++;
}
}
System.out.print(count + " ");
count = 0;
}
}
}
P1427 小鱼的数字游戏
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int i;int[] a = new int[100];
for (i = 0; i < a.length; i++) {
a[i] = input.nextInt();
if (a[i] == 0) {
break;
}
}
for (int j = i - 1; j >= 0; j--) {
System.out.print(a[j] + " ");
}
}
}
P5727 【深基5.例3】冰雹猜想
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int[] a = new int[100];int i = 1;
int n = input.nextInt();
func(n);
System.out.print(n);
}
public static void func(int n){
if (n == 1) return;
if (n % 2 == 1) n = n * 3 + 1;
else n /= 2;
func(n);
System.out.print(n + " ");
}
}
//递归和数组
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int[] a = new int[100];int i = 1;
int n = input.nextInt();int m = n;
while (n != 1){
if (n % 2 == 0){
n = n / 2;
}else{
n = 3 * n + 1;
}
a[i] = n;
i++;
}
for (int j = i - 1; j > 0; j--) {
System.out.print(a[j] + " ");
}
System.out.println(m);
}
}
P1047 [NOIP2005 普及组] 校门外的树
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input =new Scanner(System.in);
int L=input.nextInt();
int a[]=new int[L+1];
int count=0;
for(int i=0;i<=L;i++) //将路上的所有树打上标记,表示这个点没有被访问过
{
a[i]=0;
}
int M=input.nextInt();
for(int i=1;i<=M;i++) //循环M次
{
int start=input.nextInt(); //区间的头
int end=input.nextInt(); //区间的尾
for(int j=start;j<=end;j++)
{
if(a[j]==0)
{
a[j]=1;
}
}
}
for(int i=0;i<=L;i++)
{
if(a[i]==0)
{
count++;
}
}
System.out.println(count);
}
}
P5728 【深基5.例5】旗鼓相当的对手
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();int i;int[] sum = new int[n + 1];int ans = 0;
int[] ch = new int[n + 1];int[] ma = new int[n + 1];int[] en = new int[n + 1];
for (i = 1; i <= n; i++) {
ch[i] = input.nextInt();
ma[i] = input.nextInt();
en[i] = input.nextInt();
sum[i] = ch[i] + ma[i] + en[i];
}
for (int j = 1; j <= n; j++) {
for (int k = j + 1; k <= n; k++) {
if ((Math.abs(ch[j] - ch[k]) <= 5) && (Math.abs(ma[j] - ma[k]) <= 5) && (Math.abs(en[j] - en[k]) <= 5) && (Math.abs(sum[j] - sum[k]) <= 10)){
ans++;
}
}
}
System.out.println(ans);
}
}
P5729 【深基5.例7】工艺品制作
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws Exception {
Scanner input = new Scanner(System.in);
final int max = 21;
int[][][] cube = new int[max][max][max];
int x1, y1, z1, x2, y2, z2;int count = 0;
int a = input.nextInt();
int b = input.nextInt();
int c = input.nextInt();
int n = input.nextInt();
while (n > 0){
x1 = input.nextInt();
y1 = input.nextInt();
z1 = input.nextInt();
x2 = input.nextInt();
y2 = input.nextInt();
z2 = input.nextInt();
for (int i = x1; i <= x2; i++) {
for (int j = y1; j <= y2; j++) {
for (int k = z1; k <= z2; k++) {
cube[i][j][k] = 1;
}
}
}
n--;
}
for (int i = 1; i <= a; i++) {
for (int j = 1; j <= b; j++) {
for (int k = 1; k <= c; k++) {
if (cube[i][j][k] == 0) count++;
}
}
}
System.out.println(count);
}
}
P2550 [AHOI2001] 彩票摇奖
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();int k = 0;
int[] a = new int[1000];int[] b = new int[1000];
for (int l = 1; l <= 7; l++) {
int x = input.nextInt();
a[x] = 1;
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= 7; j++) {
int x = input.nextInt();
if (a[x] == 1) {
k++;
}
}
b[k]++;
k = 0;
}
for (int i = 7; i >= 1; i--) {
System.out.print(b[i] + " ");
}
}
}
P2615 [NOIP2015 提高组] 神奇的幻方
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws Exception {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int[][] a = new int[40][40];int x = 1;
int y = (n + 1) / 2;
for (int i = 1; i <= n * n; i++) {
//确定第一个数为1,且位置在第一行,中间列,然后循环赋值
a[x][y] = i;
//确定下一个数的横纵坐标。
if (a[(x - 2 + n) % n + 1][y % n + 1] == 0){
x = (x - 2 + n) % n + 1;
y = y % n + 1;
}else {
x = x % n + 1;
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
System.out.print(a[i][j] + " ");
}
System.out.println();
}
}
}
P5730 【深基5.例10】显示屏
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws Exception {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
String s = input.next();
String[] ans = new String[5];
ans[0] = "";
ans[1] = "";
ans[2] = "";
ans[3] = "";
ans[4] = "";
for (int i = 0; i < s.length(); i++) {
if(s.charAt(i) == '0')
{
ans[0] += "XXX";ans[4] += "XXX";
ans[1] += "X.X";ans[2] += "X.X";ans[3] += "X.X";
}
if(s.charAt(i) == '1')
{
ans[0] += "..X";ans[4] += "..X";
ans[1] += "..X";ans[2] += "..X";ans[3] += "..X";
}
if(s.charAt(i) == '2')
{
ans[0] += "XXX";ans[4] += "XXX";
ans[1] += "..X";ans[2] += "XXX";ans[3] += "X..";
}
if(s.charAt(i) == '3')
{
ans[0] += "XXX";ans[4] += "XXX";
ans[1] += "..X";ans[2] += "XXX";ans[3] += "..X";
}
if(s.charAt(i) == '4')
{
ans[0] += "X.X";ans[4] += "..X";
ans[1] += "X.X";ans[2] += "XXX";ans[3] += "..X";
}
if(s.charAt(i) == '5')
{
ans[0] += "XXX";ans[4] += "XXX";
ans[1] += "X..";ans[2] += "XXX";ans[3] += "..X";
}
if(s.charAt(i) == '6')
{
ans[0] += "XXX";ans[4] += "XXX";
ans[1] += "X..";ans[2] += "XXX";ans[3] += "X.X";
}
if(s.charAt(i) == '7')
{
ans[0] += "XXX";ans[4] += "..X";
ans[1] += "..X";ans[2] += "..X";ans[3] += "..X";
}
if(s.charAt(i) == '8')
{
ans[0] += "XXX";ans[4] += "XXX";
ans[1] += "X.X";ans[2] += "XXX";ans[3] += "X.X";
}
if(s.charAt(i) == '9')
{
ans[0] += "XXX";ans[4] += "XXX";
ans[1] += "X.X";ans[2] += "XXX";ans[3] += "..X";
}
if(i != s.length() - 1) //最后一个数字不用空列
{
ans[0] += ".";ans[4] += ".";
ans[1] += ".";ans[2] += ".";ans[3] += ".";
}
}
System.out.println(ans[0]);
System.out.println(ans[1]);
System.out.println(ans[2]);
System.out.println(ans[3]);
System.out.println(ans[4]);
}
}
P1554 梦中的统计
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws Exception {
Scanner input = new Scanner(System.in);
int m = input.nextInt();
int n = input.nextInt();
int[] a = new int[10];int j = 0;
for (int i = m; i <= n; i++) {
for (j = i;; j /= 10) {
if (j == 0) break;
a[j % 10]++;
}
}
for (int i = 0; i <= 9; i++) {
System.out.print(a[i] + " ");
}
}
}
P2141 [NOIP2014 普及组] 珠心算测验
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws Exception {
Scanner input = new Scanner(System.in);
int n = input.nextInt();int count = 0;
int[] a = new int[101];
boolean[] barrel = new boolean[20001];
for (int i = 0; i < n; i++) {
a[i] = input.nextInt();
}
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
//布尔数组记录每两个数之和
barrel[a[i] + a[j]] = true;
}
}
for (int i = 0; i < n; i++) {
if (barrel[a[i]]){
//如果数组a中的数和记录下的数相等就累加
count++;
}
}
System.out.println(count);
}
}
P1614 爱与愁的心痛
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws Exception {
Scanner input = new Scanner(System.in);
int n = input.nextInt();int min = 1000000000;
int m = input.nextInt();int[] a = new int[3001];
int sum = 0;
for (int i = 1; i <= n; i++) {
a[i] = input.nextInt();
}
for (int i = 1; i <= n - m + 1; i++) {
for (int j = 1; j <= m; j++) {
sum += a[i + j - 1];
}
if (sum < min) {
min = sum;
}
sum = 0;
}
System.out.println(min);
}
}
P2911 [USACO08OCT] Bovine Bones G
暴力枚举法
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws Exception {
Scanner input = new Scanner(System.in);
int[] a = new int[16001];int max = 0;
int i, j ,k;
int s1 = input.nextInt();
int s2 = input.nextInt();
int s3 = input.nextInt();
for (i = 1; i <= s1; i++) {
for (j = 1; j <= s2; j++) {
for (k = 1; k <= s3; k++) {
a[i + j + k]++;
max = Math.max(max, i + j + k);
}
}
}
int barrel = 0;int sum = 0;
for (i = 0; i < max; i++) {
if (a[i] > barrel){
barrel = a[i];
sum = i;
}
}
System.out.println(sum);
}
}
归纳法
这是一个概率问题,背后存在一定的规律,通过模拟每一次的投掷情况可以总结出一些规律。以三个正常的六面骰子为例:面数最小值为3,面数最大值为18,剩下出现的值在3-18之间,并且向内递减两端概率相同:
和为3或18:每个共1种,每个概率为1/216。
和为4或17:每个共3种,每个概率为1/72。
和为5或16:每个共6种,每个概率为2/72。
和为6或15:每个共10种,每个概率为5/108。
和为7或14:每个共15种,每个概率为5/72。
和为8或13:每个共21种,每个概率为7/72。
和为9或12:每个共25种,每个概率为25/216。
和为10或11:每个共27种,每个概率为1/8。
如图,可以看出整体分布是呈现一种弧线分布。在三个骰子面数相同时呈现一种类正态分布的曲线。
从一般情况来看,如果三个骰子面数不一致,曲线分布的规律将会倾向较大值,
三个骰子可能时三个面数一样、两个较大面数和两个较小面数这三种情况,如上,三个面数一样最终的概率图是类正态分布曲线,而当出现其余两种情况,这个曲线的概率偏值将会分别向左右移动。
两个较大面数:曲线转折点将会向右偏移,也就是向可能出现的大的值偏移
两个较小面数:曲线转折点将会向左偏移,也就是向可能出现的小的值偏移
于是可以推出,可能出现点数的概率最大值与三个骰子的面数相关
下面这个链接是更加详实的解析。
https://p31prshut.blog.luogu.org/ti-xie-p2911-usaco08oct-niu-gu-tou-bovine-bones
public class Main {
public static void main(String[] args) throws Exception {
Scanner input = new Scanner(System.in);
int s1 = input.nextInt();
int s2 = input.nextInt();
int s3 = input.nextInt();
//排序
if (s1 < s2) exchange(s1, s2);
if (s2 < s3) exchange(s2, s3);
if (s1 < s2) exchange(s1, s2);
//最小值减去最大值+1和中间值比较
if (s2 <= s1 - s3 + 1) System.out.println(s2 + s3 + 1);
else System.out.println(2 + s1 + (s2 - s1 + s3 - 1) / 2);
}
public static void exchange(int a, int b){
int temp = a;
a = b;
b = temp;
}
}
P1161 开灯
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws Exception {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int[] arr = new int[5000001];
int num;
for (int i = 1; i <= n; i++) {
double a = input.nextDouble();
int t = input.nextInt();
for (int j = 1; j <= t; j++) {
num = (int) (j * a);
if (arr[num] == 0) arr[num] = 1;
else arr[num] = 0;
}
}
for (int i = 1; i < 5000001; i++) {
if (arr[i] == 1) System.out.println(i);
}
}
}
P5731 【深基5.习6】蛇形方阵
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws Exception {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int[][] a = new int[11][11];
int num = 1;int t = (int) Math.ceil(1.0 * n / 2);
for(int i=0; i<t; i++) {
for (int j = i; j < n - i; j++) {
a[i][j] = num++;//每一圈判断第一行
}
for (int j = i + 1; j < n - i - 1; j++) {
a[j][n - i - 1] = num++;//每一圈判断最后一列
}
for (int j = n - i - 1; j > i; j--) {
a[n - i - 1][j] = num++;//判断每一圈最后一行
}
for (int j = n - i - 1; j > i; j--) {
a[j][i] = num++;//判断每一圈第一列
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.printf("%3d", a[i][j]);
}
System.out.println();
}
}
}
P5732 【深基5.习7】杨辉三角
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws Exception {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int[][] a = new int[21][21];
a[0][0] = 1;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
a[i][j] +=a[i - 1][j] + a[i - 1][j - 1];
System.out.print(a[i][j] + " ");
}
System.out.println();
}
}
}
P1789 【Mc生存】插火把
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws Exception {
Scanner input = new Scanner(System.in);
int[][] a = new int[105][105];
int n = input.nextInt();
int m = input.nextInt();
int k = input.nextInt();
int count = 0;
//数组下标会越界,每次读入下,y时都向后移2位,保证不越界
for (int i = 1; i <= m; i++) {
int x = input.nextInt();
int y = input.nextInt();
x += 2;y += 2;
for (int j = x - 2; j <= x + 2; j++) {
a[j][y] = 1;
}
for (int j = y - 2; j <= y + 2; j++) {
a[x][j] = 1;
}
a[x + 1][y + 1] = 1;
a[x - 1][y - 1] = 1;
a[x + 1][y - 1] = 1;
a[x - 1][y + 1] = 1;
}
for (int i = 1; i <= k; i++) {
int x = input.nextInt();
int y = input.nextInt();
x += 2;y += 2;
for (int j = x - 2; j <= x + 2; j++) {
for (int l = y - 2; l <= y + 2; l++) {
a[j][l] = 1;
}
}
}
for (int i = 3; i <= n + 2; i++) {
for (int j = 3; j <= n + 2; j++) {
if (a[i][j] == 0) count++;
}
}
System.out.println(count);
}
}
P1319 压缩技术
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws Exception {
Scanner input = new Scanner(System.in);
int n = input.nextInt();int b;
int t = 0;int i = 0;int s = 0;
while (s < n * n){
int a = input.nextInt();
i++;
for (b = a; b >= 1; b--) {
if (t == n) {
System.out.println();
t = 0;
}
if (i % 2 == 1) {
System.out.print(0);
}else {
System.out.print(1);
}
t++;
s++;
}
}
System.out.println();
}
}
P1320 压缩技术(续集版)
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws Exception {
Scanner input = new Scanner(System.in);
String[] a = new String[205];
char c = '0';
int sum = 0;
a[0] = input.next();
for (int i = 1; i < a[0].length(); i++) {
a[i] = input.next();
}
System.out.print(a[0].length() + " ");
for (int i = 0; i < a[0].length(); i++) {
for (int j = 0; j < a[0].length(); j++) {
if (a[i].charAt(j) == c) {
sum++;
} else {
c = a[i].charAt(j);
System.out.print(sum + " ");
sum = 1;
}
}
}
System.out.print(sum);
}
}
P1205 [USACO1.2] 方块转换 Transformations
import java.util.Scanner;
public class Main {
static int N;
static String r90(String rect) {
char[] ret = new char[rect.length()];
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
ret[j * N + N - 1 - i] = rect.charAt(i * N + j);
}
}
return new String(ret);
}
static String r180(String rect) {
return r90(r90(rect));
}
static String r270(String rect) {
return r90(r90(r90(rect)));
}
static String fz(String rect) {
char[] ret = new char[rect.length()];
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
ret[i * N + j] = rect.charAt(i * N + (N - j - 1));
}
}
return new String(ret);
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
N = scanner.nextInt();
String str = "";
String end = "";
for (int i = 0; i < N; i++) {
str += scanner.next();
}
for (int i = 0; i < N; i++) {
end += scanner.next();
}
if (r90(str).equals(end)) {
System.out.print(1);
} else if (r180(str).equals(end)) {
System.out.print(2);
} else if (r270(str).equals(end)) {
System.out.print(3);
} else {
String tmp = fz(str);
if (tmp.equals(end)) {
System.out.print(4);
} else if (r90(tmp).equals(end) || r180(tmp).equals(end) || r270(tmp).equals(end)) {
System.out.print(5);
} else if (str.equals(end)) {
System.out.print(6);
} else {
System.out.print(7);
}
}
}
}