27 篇文章 5 订阅

# ＃平方末尾

能够表示为某个整数的平方的数字称为“平方数”



我这么写不知道对不对，反正哈答案是对了。

import java.util.*;

public class Main
{
public static void main(String args[])
{
int count=0;
for (int i =0; i <100; i++) {
int a=i*i;
int c=a%100;
int d=a%10;
if(c==d) {
count++;
}
}
System.out.println(count);
}
}

# ＃七星填数

如图【图1.png】所示。



这个是看的网上大佬的代码。

import java.util.HashSet;

public class Main {
public static int sum = 0;

public void swap(int[] A, int i, int j) {
int temp = A[i];
A[i] = A[j];
A[j] = temp;
}

public void dfs(int[] A, int step) {
if(step == A.length) {
int[] count = new int[7];
count[0] = A[0] + A[1] + A[2] + A[3];
count[1] = A[0] + A[4] + A[6] + A[9];
count[2] = A[1] + A[4] + 6 + 14;
count[3] = A[2] + A[5] + 6 + 11;
count[4] = A[6] + A[8] + A[10] + 14;
count[5] = A[7] + A[8] + A[9] + 11;
count[6] = A[3] + A[5] + A[7] + A[10];
HashSet<Integer> set = new HashSet<Integer>();
for(int i = 0;i < 7;i++)
if(set.size() == 1) {
for(int i = 0;i < A.length;i++)
System.out.print(A[i]+" ");
System.out.println();
}
sum++;
return;
} else {
for(int i = step;i < A.length;i++) {
swap(A, i, step);
dfs(A, step + 1);
swap(A, i, step);
}
}
}

public static void main(String[] args) {
Main test = new Main();
int[] A = {1,2,3,4,5,7,8,9,10,12,13};
test.dfs(A, 0);
System.out.println(sum);
}
}


# ＃打印数字

小明写了一个有趣的程序，给定一串数字。

22222   00000       1   6666
2     2 0     0    1 1  6
2 0     0      1  666666
2  0     0      1  6     6
2    0     0      1  6     6
2    2 0     0      1  6     6
2222222  00000     1111  66666

public class Main
{
static void f(int n)
{
String[][] di =
{{" 00000 ",
"0     0",
"0     0",
"0     0",
"0     0",
"0     0",
" 00000 "},
{"     1 ",
"   1 1 ",
"     1 ",
"     1 ",
"     1 ",
"     1 ",
"   1111"},
{" 22222 ",
"2     2",
"      2",
"     2 ",
"   2   ",
" 2    2",
"2222222"},
{" 33333 ",
"3     3",
"      3",
"  3333 ",
"      3",
"3     3",
" 33333 "},
{"   44  ",
"  4 4  ",
" 4  4  ",
"4   4  ",
"4   4  ",
"4444444",
"    4  "},
{" 55555 ",
" 5     ",
"555555 ",
"      5",
"      5",
"5     5",
" 55555 "},
{" 6666  ",
"6      ",
"666666 ",
"6     6",
"6     6",
"6     6",
" 66666 "},
{"7777777",
"7    7 ",
"    7  ",
"   7   ",
"  7    ",
" 7     ",
" 7     "},
{" 88888 ",
"8     8",
"8     8",
" 88888 ",
"8     8",
"8     8",
" 88888 "},
{" 99999 ",
"9     9",
"9     9",
" 999999",
"      9",
"9     9",
" 99999 "}};

char[] cc = (""+n).toCharArray();

for(int i=0; i<di[0].length; i++){
for(int j=0; j<cc.length; j++){
System.out.print( ____________________ + " ");  //填空位置
}
System.out.println();
}
}

public static void main(String[] args)
{
f(2016);
}
}

# ＃赢球票

某机构举办球票大奖赛。获奖选手有机会赢得若干张球票。

3
1 2 3

1

3
2 1 3

6

CPU消耗  < 1000ms

代码：

借鉴网上的代码。

import java.util.Scanner;

public class Test4 {
public static int n;
public static int max = 0;
public static int[] num;
public static void f() {
for (int i = 0; i < n; i++) {//从第i个卡片开始
int[] temp = new int[n];//临时数组
for (int k = 0; k < n; k++)
temp[k] = num[k];
int sum = 0;//每次数到卡片的和
int count = 1;//从第一张开始数
int start = i;//第i张卡片
while (true) {
boolean judge = false;
for (int k = 0; k < n; k++)
if (temp[k] >= count) {//判断大于count的，否者就直接跳出
judge = true;
break;
}
if (!judge)//如果不为真就直接跳出
break;
int j = start % n;
if (temp[j] == count) {
sum = sum + count;//每次数到卡片的和加起来
temp[j] = -1;//读到过了
count = 1;//重新从1开始读
} else if (temp[j] != -1)
count++;
start++;
}
max = Math.max(max, sum);//每次比出最大的值
}
System.out.println(max);
}

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
n = in.nextInt();
num = new int[n];
for (int i = 0; i < n; i++)
num[i] = in.nextInt();
f();
}
}

# ＃路径之谜

小明冒充X星球的骑士，进入了一个奇怪的城堡。

（城堡的西墙和北墙内各有 n 个靶子）

0  1  2  3
4  5  6  7
8  9  10 11
12 13 14 15

4
2 4 3 4
4 3 3 3

0 4 5 1 2 3 7 11 10 9 13 14 15

CPU消耗  < 1000ms

这个更是借鉴网上的代码，我都看不懂，不李姐。

// 常规的深搜+剪枝
import java.util.ArrayList;
import java.util.Scanner;

public class Main {
// 下一步的方向
static int[][] dir = { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } };
// 结果
static ArrayList<Integer> list = new ArrayList<Integer>();

static int N;
// 记录北边的箭靶
static int[] north;
// 记录西边的箭靶
static int[] west;
// 记录走过的方格
static byte[] visit;

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
north = new int[N];
west = new int[N];

for (int i = 0; i < N; i++) {
north[i] = sc.nextInt();
}
for (int i = 0; i < N; i++) {
west[i] = sc.nextInt();
}

// visit[i]为0，表示未被走过；visit[i]为1，表示已经走过。
visit = new byte[N * N];

dfs(0, 0);

sc.close();
}

static void dfs(int x, int y) {
int index = x * N + y;
visit[index] = 1;
north[y]--;
west[x]--;

if (index == N * N - 1) {
for (int i = 0; i < N; i++) {
if (north[i] > 0 || west[i] > 0)
return;
}
for (int i = 0; i < list.size(); i++) {
System.out.print(list.get(i) + " ");
}
return;
}

for (int i = 0; i < dir.length; i++) {
int nextx = x + dir[i][0];
int nexty = y + dir[i][1];
if (nextx < 0 || nextx >= N || nexty < 0 || nexty >= N)
continue;
if (north[nexty] <= 0 || west[nextx] <= 0)
continue;
dfs(nextx, nexty);
list.remove(list.size() - 1);
north[nexty]++;
west[nextx]++;
visit[nextx * N + y] = 0;
}
}
}


# ＃碱基

生物学家正在对n个物种进行研究。

1<=i1<i2<....<im<=n;

【输入格式】

DNA序列从1至n编号，每个序列中的碱基从1开始依次编号，不同的生物的DNA序列长度可能不同。

【输出格式】

【样例输入】
3 2 2
ATC
TCG
ACG

【样例输出】
2

【样例输入】
4 3 3
AAA
AAAA
AAA
AAA

【样例输出】
7

【数据规模与约定】

CPU消耗  < 1000ms



这个更别说了，还难......

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;

public class Main {
public static PrintWriter out = new PrintWriter(System.out);
public static int n, m, k;
public static long ans, tmp, mod = 1000000007;
public static String str;
public static String[] s = new String[10];
public static int[] a = new int[10];

public static void main(String[] args) {
n = in.nextInt();
m = in.nextInt();
k = in.nextInt();
for (int i = 1; i <= n; i++)
s[i] = in.nextLine();
ans = 0;
dfs(1, 1);
out.println(ans%mod);
out.flush();
out.close();
}

static void dfs(int kk, int p) {
if (kk > m) {
int len = s[a[1]].length();
for (int i = 0; i < len-k+1; i++) {
str = s[a[1]].substring(i, i+k);
tmp = 1;
for (int j = 2; j <= m; j++) {
tmp = (tmp * getStrCount(s[a[j]], str)) % mod;
if (tmp == 0) break;//这些DNA序列里遇到有不包含str子序列的，后面的DNA序列就不需要继续查找了，直接break
}
ans = (ans + tmp) % mod;
}
return;
}
for (int i = p; i <= n; i++) {
a[kk] = i;
dfs(kk+1, i+1);
}
}

static long getStrCount(String s1, String s2) {
long sum = 0;
String tmps = s1;
int index = tmps.indexOf(s2);
while (index != -1) {
sum++;
tmps = tmps.substring(index+1);
index = tmps.indexOf(s2);
}
return sum;
}

public StringTokenizer tokenizer;

tokenizer = null;
}

public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

public String nextLine() {
String str = null;
try {
} catch (IOException e) {
e.printStackTrace();
}
return str;
}

public int nextInt() {
return Integer.parseInt(next());
}

public long nextLong() {
return Long.parseLong(next());
}

public Double nextDouble() {
return Double.parseDouble(next());
}

public BigInteger nextBigInteger() {
return new BigInteger(next());
}

public BigDecimal nextBigDecimal() {
return new BigDecimal(next());
}

}
}

• 4
点赞
• 1
收藏
觉得还不错? 一键收藏
• 打赏
• 0
评论
02-23 398
05-22 1133
08-16
06-19
07-15
05-20

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

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

¥2 ¥4 ¥6 ¥10 ¥20

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