- 问题 1115: DNA
首先做出一个完整的DNA片段,用行数控制每行前面和中间的空格
然后先完整输出一个DNA,再输出重复度-1个去掉第一行的DNA
处理完一组数据之后换行
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
for (int n = 0; n < N; n++) {
int a = sc.nextInt();// 行数
int b = sc.nextInt();// 重复度
oneDNA(0, a);
for(int i=1;i<b;i++) {
oneDNA(1, a);
}
System.out.println();
}
}
static void oneDNA(int start, int n) {
int x = n - 2;
for (int i = start; i < n; i++) {
if (i < n / 2) {
x -= i*2;
PKG(x, i);
}
if (i == n / 2) {
for (int j = 0; j < i; j++)
System.out.print(" ");
System.out.println("X");
}
if (i > n / 2) {
PKG(x, n - i - 1);
x += 2;
}
}
}
static void PKG(int x, int y) {
// x:两个X之间的空格 y:开头的空格
for (int i = 0; i < y; i++)
System.out.print(" ");
System.out.print("X");
for (int i = 0; i < x; i++)
System.out.print(" ");
System.out.println("X");
}
}
- 问题 1116: IP判断
使用正则挺简单的…关于正则的使用
每部分取【0~255】,共三种情况
一位:0~9 \\d
两位:10~99 [1-9]\\d
三位:第一位1后两位都可,第一位2第二位只能0到4 (1\\d{2})|(2[0-4]\\d)|(25[0-5])
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNextLine()) {
String s = sc.nextLine();
boolean ox = s.matches("((\\d)|([1-9]\\d)|(1\\d{2})|(2[0-4]\\d)|(25[0-5]))\\."
+ "((\\d)|([1-9]\\d)|(1\\d{2})|(2[0-4]\\d)|(25[0-5]))\\."
+ "((\\d)|([1-9]\\d)|(1\\d{2})|(2[0-4]\\d)|(25[0-5]))\\."
+ "((\\d)|([1-9]\\d)|(1\\d{2})|(2[0-4]\\d)|(25[0-5]))");
if (s.equals("End of file")) {
return;
}
if (ox)
System.out.println("Y");
else
System.out.println("N");
}
}
}
不使用正则也可以做,按.分割开,判断够不够4段,开头是不是0,再将字符转化为整型值查看是否在范围内即可,一但不符合,直接中止判断循环
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNextLine()) {
String s = sc.nextLine();
if (s.equals("End of file")) {
return;
}
boolean ox = false;
String[] ss = s.split("\\.");
if (ss.length == 4) {
for (int i = 0; i < 4; i++) {
if (ss[i].charAt(0) == '0') {
ox = false;
break;
}
try {//出现不是数的情况,比如abc之类,执行cath
if (Integer.valueOf(ss[i]) <= 255 &&
Integer.valueOf(ss[i]) >= 0) {
ox = true;
} else {
ox = false;
break;
}
} catch (Exception e) {
ox = false;
break;
}
}
}
if (ox)
System.out.println("Y");
else
System.out.println("N");
}
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNextLine()) {
String s=sc.nextLine();
int num=0;
for(int i=0;i<s.length();i++) {
num+=(int)(s.charAt(i)-'0');
}
System.out.println(num);
}
}
}