Polycarp has just invented a new binary protocol for data transmission. He is encoding positive integer decimal number to binary string using following algorithm:
Though Polycarp learnt how to encode the numbers, he has no idea how to decode them back. Help him calculate the decoded number.
The first line contains one integer number n (1 ≤ n ≤ 89) — length of the string s.
The second line contains string s — sequence of '0' and '1' characters, number in its encoded format. It is guaranteed that the number corresponding to the string is positive and doesn't exceed 109. The string always starts with '1'.
Print the decoded number.
3
111
3
9
110011101
2031
题意:一个1代表1个价值,每个数字之间用0隔开。给你结果,叫你反转求原来的值
解:模拟
import java.util.Scanner;
/**
*
* 作者:张宇翔
* 创建日期:2017年7月16日 下午11:04:25
* 描述:
*/
public class Main {
private static final int Max = (int) (1e5 + 10);
private static int n;
private static String s;
public static void main(String[] args) {
InitData();
GetAns();
}
private static void InitData() {
Scanner cin = new Scanner(System.in);
n=cin.nextInt();
s=cin.next();
};
private static void GetAns() {
int ans=0;
int index=0;
for(int i=0;i
Alice and Bob play 5-in-a-row game. They have a playing field of size 10 × 10. In turns they put either crosses or noughts, one at a time. Alice puts crosses and Bob puts noughts.
In current match they have made some turns and now it's Alice's turn. She wonders if she can put cross in such empty cell that she wins immediately.
Alice wins if some crosses in the field form line of length not smaller than 5. This line can be horizontal, vertical and diagonal.
You are given matrix 10 × 10 (10 lines of 10 characters each) with capital Latin letters 'X' being a cross, letters 'O' being a nought and '.' being an empty cell. The number of 'X' cells is equal to the number of 'O' cells and there is at least one of each type. There is at least one empty cell.
It is guaranteed that in the current arrangement nobody has still won.
Print 'YES' if it's possible for Alice to win in one turn by putting cross in some empty cell. Otherwise print 'NO'.
XX.XX.....
.....OOOO.
..........
..........
..........
..........
..........
..........
..........
..........
YES
XXOXX.....
OO.O......
..........
..........
..........
..........
..........
..........
..........
..........
NO
解:循环给i,j为点的位置赋值成X,然后判断是否可以
import java.util.Scanner;
/**
*
* 作者:张宇翔
* 创建日期:2017年7月16日 下午11:04:25
* 描述:
*/
public class Main {
private static final int Max = (int) (1e3 + 10);
private static String []s;
private static char [][]a;
public static void main(String[] args) {
InitData();
GetAns();
}
private static void InitData() {
Scanner cin = new Scanner(System.in);
s=new String[Max];
a=new char[Max][Max];
for(int i=0;i<10;i++){
s[i]=cin.next();
a[i]=s[i].toCharArray();
}
};
private static void GetAns() {
for(int i=0;i<10;i++){
for(int j=0;j<10;j++){
if(a[i][j]=='.'){
a[i][j]='X';
if(judge()){
System.out.println("YES");
return;
}
a[i][j]='.';
}
}
}
System.out.println("NO");
return;
};
private static boolean judge(){
for(int i=0;i<10;i++){//判断横
for(int j=0;j<=10-5;j++){
if(a[i][j]=='X'&&a[i][j+1]=='X'&&a[i][j+2]=='X'
&&a[i][j+3]=='X'&&a[i][j+4]=='X'){
return true;
}
}
}
for(int i=0;i<10;i++){//判断竖
for(int j=0;j<=10-5;j++){
if(a[j][i]=='X'&&a[j+1][i]=='X'&&a[j+2][i]=='X'
&&a[j+3][i]=='X'&&a[j+4][i]=='X'){
return true;
}
}
}
for(int i=0;i<10;i++){
for(int j=0;j<10;j++){
int cnt=0;
if(Check(i, j)){
cnt++;
}
if(Check(i+1, j+1)){
cnt++;
}
if(Check(i+2, j+2)){
cnt++;
}
if(Check(i+3, j+3)){
cnt++;
}
if(Check(i+4, j+4)){
cnt++;
}
if(cnt==5){
return true;
}
}
}
for(int i=0;i<10;i++){
for(int j=0;j<10;j++){
int cnt=0;
if(Check(i, j)){
cnt++;
}
if(Check(i+1, j-1)){
cnt++;
}
if(Check(i+2, j-2)){
cnt++;
}
if(Check(i+3, j-3)){
cnt++;
}
if(Check(i+4, j-4)){
cnt++;
}
if(cnt==5){
return true;
}
}
}
return false;
}
private static boolean Check(int x,int y){
if(x>=0&&x<10&&y>=0&&y<10&&a[x][y]=='X'){
return true;
}
return false;
}
}
Makes solves problems on Decoforces and lots of other different online judges. Each problem is denoted by its difficulty — a positive integer number. Difficulties are measured the same across all the judges (the problem with difficulty d on Decoforces is as hard as the problem with difficulty d on any other judge).
Makes has chosen n problems to solve on Decoforces with difficulties a1, a2, ..., an. He can solve these problems in arbitrary order. Though he can solve problem i with difficulty ai only if he had already solved some problem with difficulty (no matter on what online judge was it).
Before starting this chosen list of problems, Makes has already solved problems with maximum difficulty k.
With given conditions it's easy to see that Makes sometimes can't solve all the chosen problems, no matter what order he chooses. So he wants to solve some problems on other judges to finish solving problems from his list.
For every positive integer y there exist some problem with difficulty y on at least one judge besides Decoforces.
Makes can solve problems on any judge at any time, it isn't necessary to do problems from the chosen list one right after another.
Makes doesn't have too much free time, so he asked you to calculate the minimum number of problems he should solve on other judges in order to solve all the chosen problems from Decoforces.
The first line contains two integer numbers n, k (1 ≤ n ≤ 103, 1 ≤ k ≤ 109).
The second line contains n space-separated integer numbers a1, a2, ..., an (1 ≤ ai ≤ 109).
Print minimum number of problems Makes should solve on other judges in order to solve all chosen problems on Decoforces.
3 3
2 1 9
1
4 20
10 3 6 3
0
In the first example Makes at first solves problems 1 and 2. Then in order to solve the problem with difficulty 9, he should solve problem with difficulty no less than 5. The only available are difficulties 5 and 6 on some other judge. Solving any of these will give Makes opportunity to solve problem 3.
In the second example he can solve every problem right from the start.
解:对于每个不能完成的,求最少需要完成多少。然后再把当前难度调整为a[i];
import java.util.Arrays;
import java.util.Scanner;
/**
*
* 作者:张宇翔
* 创建日期:2017年7月16日 下午11:04:25
* 描述:
*/
public class Main {
private static final int Max = (int) (1e4 + 10);
private static int n,k;
private static int []a;
public static void main(String[] args) {
InitData();
GetAns();
}
private static void InitData() {
Scanner cin = new Scanner(System.in);
a=new int[Max];
n=cin.nextInt();
k=cin.nextInt();
for(int i=0;i=x){
MAX=Math.max(MAX, a[i]);
}else{
int temp=x;
while(MAX