第一题:编码数目
题目:输入N,M,求N+N^2+N^3+...+N^M的结果(取余1000000007),1
输出格式:每行输出对应的结果
解题思路:快速幂(实质对幂指数进行二进制转换,将累乘拆分),不了解的可以查找一下
import java.util.*;
public class Main {
static Scanner in = new Scanner(System.in);
static int mod = 1000000007;
static int q_pow(int a, int b) {
a %= mod;
int ans = 1;
while (b != 0) {
if ((b & 1) == 1)
ans = (ans * a) % mod;
a = (a * a) % mod;
b >>= 1;
}
return ans;
}
public static void main(String[] args) {
int n = in.nextInt();
int m = in.nextInt();
long sum = 0;
for (int i = 1; i <= m; i++) {
sum+=q_pow(n, i);//累加即可
}
System.out.println(sum);
}
}
其实还有一种思路是利用Java提供的大数类BigInterger,这个类支持大数计算
题目:允许对二进制数进行两种操作:00->10,10->01,求可能的最大数(两种操作可以进行任意次)
输入格式:先一行输出样例数,然后每两行输入二进制长度与二进制数本体,1<=长度<=10000
例如:
2
4
0001
10
1010111000
输出格式:每行输出每个样例的答案
例如:
1101
1111101111
思路:操作:①00->10②10->01,
为了使得到的数最大,尽可能使高位为1,那么②其实是为①服务的,利用②将0往高位移动,将1往低位移动,然后将高位的0利用①转换,通过模拟可以得到结论:第一个0后面的所有1(设个数为a)都会别挪到最后(通过②),此时经过①会有新的0101情况出现,继续重复上述步骤,最后得到:(总长度-a-1)个1+0+a个1 的字符串
总结思路:(1)通过②将第一个0后面的所有1移到最后
(2)通过①处理此时的字符串
(3)重复(1)(2)直到不能进行①②操作
import java.util.*;
public class Main {
static Scanner in = new Scanner(System.in);
public static void main(String[] args) {
int t = in.nextInt();
while(t-->0) {
int n = in.nextInt();
String s = in.next();
int pos = 0;
int cnt = 0;
for (int i = 0; i < s.length(); i++) {
if(s.charAt(i)=='0') {pos = i;break;};
}
for (int i = pos; i < s.length(); i++) {
if(s.charAt(i)=='1') cnt++;
}
String ans = "";
for (int i = 0; i < n-cnt-1; i++) {
ans+="1";
}
ans+="0";
for (int i = 0; i < cnt; i++) {
ans+="1";
}
if(pos==0)//注意字符串全为1的情况
System.out.println(s);
else
System.out.println(ans);
}
}
}
题目:即leetcode 37题
输入格式:
{5,3,0,0,7,0,0,0,0}
{6,0,0,1,9,5,0,0,0}
{0,9,8,0,0,0,0,6,0}
{8,0,0,0,6,0,0,0,3}
{4,0,0,8,0,3,0,0,1}
{7,0,0,0,2,0,0,0,6}
{0,6,0,0,0,0,2,8,0}
{0,0,0,4,1,9,0,0,5}
{0,0,0,0,8,0,0,7,9}
输出格式:
{5,3,4,6,7,8,9,1,2}
{6,7,2,1,9,5,3,4,8}
{1,9,8,3,4,2,5,6,7}
{8,5,9,7,6,1,4,2,3}
{4,2,6,8,5,3,7,9,1}
{7,1,3,9,2,4,8,5,6}
{9,6,1,5,3,7,2,8,4}
{2,8,7,4,1,9,6,3,5}
{3,4,5,2,8,6,1,7,9}
思路:标志数组+回溯
import java.util.Scanner;
public class Main {
static Scanner in = new Scanner(System.in);
static boolean[][] row = new boolean[9][10];
static boolean[][] col = new boolean[9][10];
static boolean[][] box = new boolean[9][10];
static int[][] num = new int[9][9];
static boolean solve(int r, int c) {
if (c == 9) {
c = 0;
r++;
if (r == 9)
return true;
}
if (num[r][c] == 0) {
int id = 0;
for (int i = 1; i <= 9; i++) {
id = (r / 3) * 3 + c / 3;
//不能在同一行同一列或者同一个方框
if (row[r][i] == false && col[c][i] == false && box[id][i] == false) {
row[r][i] = col[c][i] = box[id][i] = true;
num[r][c] = i;
if (solve(r, c + 1))
return true;
//位置不合适,回溯
num[r][c] = 0;
row[r][i] = col[c][i] = box[id][i] = false;
}
}
}else
return solve(r, c + 1);
return false;
}
public static void main(String[] args) {
String str;
int bid = 0;
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 10; j++) {
row[i][j] = col[i][j] = box[i][j] = false;
}
}
for (int i = 0; i < 9; i++) {
str = in.next();
for (int j = 0; j < 9; j++) {
num[i][j] = str.charAt(j * 2 + 1) - '0';
if (num[i][j] >= 1 && num[i][j] <= 9) {
bid = (i / 3) * 3 + j / 3;//每个3X3方块的索引
row[i][num[i][j]] = true;
col[j][num[i][j]] = true;
box[bid][num[i][j]] = true;
}
}
}
solve(0, 0);
for (int i = 0; i < 9; i++) {
System.out.print("{");
for (int j = 0; j < 9; j++) {
if (j != 8)
System.out.print(num[i][j] + ",");
else
System.out.println(num[i][j] + "}");
}
}
}
}