一道有效的IPv7地址转换成十进制数据的题
题目描述:
在这里插入图片描述
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Scanner;
//1.0.0.0.0.0.2转换成二进制
public class test111 {
public static void main(String[] args) throws IOException {
//BufferedReader reader =new BufferedReader(new InputStreamReader(System.in));
// int n=Integer.parseInt(reader.readLine());
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
Long res = -1L;
String str=in.nextLine();
String strs[] = str.split("\\.");
String tempstr = "";
for (int i = 0; i < strs.length; i++) {
if (isValidIP(strs[i]) == true) {
//int temp = StringUtils.leftPad(Integer.toBinaryString(Integer.parseInt(strs[i])), 8, '0');
int number=Integer.parseInt(strs[i]);
StringBuilder sBuilder = new StringBuilder();
for (int j = 0; j < 8; j++){
sBuilder.append(number & 1);
number = number >>> 1;
}
tempstr += sBuilder.reverse().toString();
} else
System.out.println(res);
}
res=0L;
for (int i = tempstr.length() - 1, j = 0; i >= 0; i--, j++) {
int temp = tempstr.charAt(i) - '0';
res+= temp * (long) Math.pow(2, j);
}
System.out.println(res);
}
}
public static boolean isValidIP (String s){
int begin = 0;
int end = s.length();
int strlrn = end - begin;
if (strlrn > 1 && s.charAt(begin) == '0') return false;
int strRes = 0;
while (begin < end) {
strRes = strRes * 10 + s.charAt(begin) - '0';
begin++;
}
return strRes >= 0 && strRes <= 255;
}
}
小K的睡眠时长
import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* 小 K 的睡眠时长
* @param timeToBed int整型一维数组 小 K 上床睡觉时间,格式为 [H, M]
* @param alarms int整型二维数组 闹钟列表,每一项为 [h, m]
* @return int整型一维数组
*/
public int[] sleepTime (int[] timeToBed, int[][] alarms) {
Arrays.sort(alarms, (a, b) -> {
if (a[0] == b[0]) return a[1] - b[1];
else return a[0] - b[0];
});
for (int i = 0; i < alarms.length; i ++) {
if ((alarms[i][0] > timeToBed[0]) || ((alarms[i][0] == timeToBed[0]) && (alarms[i][1] >= timeToBed[1]))) {
int n = (alarms[i][0] - timeToBed[0]) * 60 + alarms[i][1] - timeToBed[1];
return new int[]{n / 60, n % 60};
}
}
int n = (alarms[0][0] + 24 - timeToBed[0]) * 60 + alarms[0][1] - timeToBed[1];
return new int[]{n / 60, n % 60};
}
}
you获得2分,oo获得1分,最多可以获得多少分?
奇数变偶数
只能互换一次或0次,如果能变成偶数,则输出。若不能,输出-1
子矩阵之和
import java.util.*;
public class Main {
public static int get_gcd(int n1, int n2) {
int gcd = 0;
if (n1 < n2) {// 交换n1、n2的值
n1 = n1 + n2;
n2 = n1 - n2;
n1 = n1 - n2;
}
if (n1 % n2 == 0) {
gcd = n2;
}
while (n1 % n2 > 0) {
n1 = n1 % n2;
if (n1 < n2) {
n1 = n1 + n2;
n2 = n1 - n2;
n1 = n1 - n2;
}
if (n1 % n2 == 0) {
gcd = n2;
}
}
return gcd;
}
public static void main(String[] args) {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int n=Integer.parseInt(strs[0]);
int m=Integer.parseInt(strs[1]);
int k=Integer.parseInt(strs[2]);
int[][] temp=new int[n][m];
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
temp[i][j] = get_gcd(i + 1, j + 1);
}
}
int res = 0;
int i = 0;
int j = 0;
while(i <= m - k){
while(j <= n - k){
for(int x = i; x < i + k; x++){
for(int y = j; y < j + k; y++){
res += temp[x][y];
}
}
j += 1;
}
i += 1;
j = 0;
}
System.out.println(res);
}
}
粒子实验
import java.util.Scanner;
public class guanglianda {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int n=in.nextInt();
boolean[] chu=new boolean[n+1];
int []a=new int[n];
int []b=new int[n];
for(int i=0;i<n;i++){
a[i]=in.nextInt();
}
for(int i=0;i<n;i++){
b[i]=in.nextInt();
}
int res=0;
int i=0;
int j=0;
while (i<n){
if(chu[a[i]]==false){
if(a[i]==b[j]){
chu[a[i]]=true;
i++;
j++;
}else{
chu[b[j]]=true;//不是期盼的
j++;
res++;
}
}else{
i++;
}
}
System.out.println(res);
}
}
最多的人
lst1 = list(map(int, input().split()))
lst2 = []
for _ in range(lst1[1]):
lst2.append(list(map(int, input().split())))
res = 0
dp = [0] * (lst1[0] + 1)
for i in range(len(lst2)):
m = lst2[i][2]
for j in range(lst2[i][0], lst2[i][1] + 1):
if m > 0:
if dp[j] == 0:
dp[j] = 1
m -= 1
else:
continue
else:
if dp[j] == 0:
dp[j] = -1
else:
continue
print(dp.count(1) + dp.count(0) - 1)