一、井字棋
题目描述:
给定一个二维数组board,代表棋盘,其中元素为1的代表是当前玩家的棋子,0表示没有棋子,-1代表是对方玩家的棋子。当一方棋子在横竖斜方向上有连成排的及获胜(及井字棋规则),返回当前玩家是否胜出。
测试样例:
[[1,0,1],[1,-1,-1],[1,-1,0]]
返回:true
题目详解:
直接记录所有赢的可能返回true,否则返回false。
import java.util.*;
public class Board {
public boolean checkWon(int[][] board) {
// write code here
for(int i = 0;i < 3;i++){
int count1 = 0;
int count2 = 0;
int count3 = 0;
int count4 = 0;
for(int j = 0;j < 3;j++){
//横行
if(board[i][j] == 1){
count1++;
}
//竖行
if(board[j][i] == 1){
count2++;
}
}
if(count1 == 3||count2 == 3){
return true;
}
}
if(board[0][0] == 1&&board[1][1] == 1&&board[2][2] == 1){
return true;
}else if(board[0][2] == 1&&board[1][1] == 1&&board[2][0] == 1){
return true;
}
return false;
}
}
二、密码强度等级
题目描述:
密码按如下规则进行计分,并根据不同的得分为密码进行安全等级划分。
一、密码长度:
5 分: 小于等于4 个字符
10 分: 5 到7 字符
25 分: 大于等于8 个字符
二、字母:
0 分: 没有字母
10 分: 密码里的字母全都是小(大)写字母
20 分: 密码里的字母符合”大小写混合“
三、数字:
0 分: 没有数字
10 分: 1 个数字
20 分: 大于1 个数字
四、符号:
0 分: 没有符号
10 分: 1 个符号
25 分: 大于1 个符号
五、奖励(只能选符合最多的那一种奖励):
2 分: 字母和数字
3 分: 字母、数字和符号
5 分: 大小写字母、数字和符号
最后的评分标准:
>= 90: 非常安全
>= 80: 安全(Secure)
>= 70: 非常强
>= 60: 强(Strong)
>= 50: 一般(Average)
>= 25: 弱(Weak)
>= 0: 非常弱(Very_Weak)
对应输出为:
VERY_SECURE
SECURE
VERY_STRONG
STRONG
AVERAGE
WEAK
VERY_WEAK
请根据输入的密码字符串,进行安全评定。
注:
字母:a-z, A-Z
数字:0-9
符号包含如下: (ASCII码表可以在UltraEdit的菜单view->ASCII Table查看)
!"#$%&'()*+,-./ (ASCII码:0x21~0x2F)
:;<=>?@ (ASCII码:0x3A~0x40)
[\]^_` (ASCII码:0x5B~0x60)
{|}~ (ASCII码:0x7B~0x7E)
提示:
1 <= 字符串的长度<= 300
输入描述:输入一个string的密码
输出描述:输出密码等级
补充说明:
示例1
输入:
38$@NoNoN
输出:
VERY_SECURE
说明:
样例的密码长度大于等于8个字符,得25分;大小写字母都有所以得20分;有两个数字,所以得20分;包含大于1符号,所以得25分;由于该密码包含大小写字母、数字和符号,所以奖励部分得5分,经统计得该密码的密码强度为25+20+20+25+5=95分。
示例2
输入:
Jl)M:+
输出:
AVERAGE
说明:
示例2的密码强度为10+20+0+25+0=55分。
题目详解:
将每个种类的得分分别利用一个方法统计,最后相加。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String s = in.next();
if(len(s) == 0){
System.out.println("VERY_WEAK");
}else{
int sum = len(s) + fun(s);
if(sum >= 90){
System.out.println("VERY_SECURE");
}else if(sum >= 80){
System.out.println("SECURE");
}else if(sum >= 70){
System.out.println("VERY_STRONG");
}else if(sum >= 60){
System.out.println("STRONG");
}else if(sum >= 50){
System.out.println("AVERAGE");
}else if(sum >= 25){
System.out.println("WEAK");
}else{
System.out.println("VERY_WEAK");
}
}
}
public static int len(String s){
if(s.length() <= 4){
return 5;
}else if(s.length() >= 5 && s.length() <= 7){
return 10;
}else if(s.length() >= 8){
return 25;
}
return 0;
}
public static int fun(String s){
int sum = 0;
int l = 0;
int u = 0;
int n = 0;
int sym = 0;
for(int i = 0;i < s.length();i++){
char ch = s.charAt(i);
if(ch >= 'a' && ch <= 'z'){
l++;
}else if(ch >= 'A' && ch <= 'Z'){
u++;
}else if(ch >= '0' && ch <= '9'){
n++;
}else if((ch >= 0x21 && ch <= 0x2F)||
(ch >= 0x3A && ch <= 0x40)||
(ch >= 0x5B && ch <= 0x60)||
(ch >= 0x7B && ch <= 0x7E)){
sym++;
}
}
int letter = funl(l,u);
int num = funn(n);
int symbel = funs(sym);
int extra = 0;
if(letter == 20 && num >=10 && symbel >=10){
extra = 5;
}else if(letter >= 10 && num >=10 && symbel >=10){
extra = 3;
}else if(letter >= 10 && num >=10){
extra = 2;
}
return letter + num + symbel + extra;
}
public static int funl(int l,int u){
if((l == 0 && u == 0)){
return 0;
}else if(l == 0 || u == 0){
return 10;
}else{
return 20;
}
}
public static int funn(int n){
if(n == 0){
return 0;
}else if(n == 1){
return 10;
}else{
return 20;
}
}
public static int funs(int n){
if(n == 0){
return 0;
}else if(n == 1){
return 10;
}else{
return 25;
}
}
}
如有建议或想法,欢迎一起讨论学习~