java上机实验
我们使用的教材是《java简明教程(第三版)》- 皮德常
集合的交并集
盲目分析
这个题目主要还是弄清楚集合怎么实现交并集的算法,然后使用以前所学的增删查改来实现。
多的不说了,坐下坐下。老套路
暴力破解
感谢太傅TaiFu指出问题
class intSet{
int array[] = new int[11];
intSet(int _array[]){
for (int i = 0; i < _array.length; i++)
this.array[i] = _array[i];
}
void disArray(){
for(int i = 0; i < this.array.length; i++){
if(this.array[i] != 0)System.out.print(this.array[i] + " ");
}
System.out.println();
}
boolean isEqual(intSet other){//经过太傅指出错误并修改
if(this.array.length != other.array.length)return false;
int tmp[] = new int[other.array.length];
for(int i = 0; i < this.array.length; i++){
for(int j = 0; j < other.array.length; j++){
if(this.array[i] == other.array[j] && tmp[j] != 1){
tmp[j] = 1;
break;
}
}
}
for (int i = 0; i < tmp.length; i++){
if (tmp[i] == 0) return false;
}
return true;
}
void Mixed(intSet other, int a[]){
int _a[] = new int[22];
int k = 0;
for(int i = 0; i < this.array.length; i++){
for(int j = 0; j < other.array.length; j++){
if(this.array[i] == other.array[j])
_a[k++] = this.array[i];
}
}
for(int i = 0; i < _a.length; i++)
a[i] = _a[i];
}
void Combined(intSet other, int a[]) {
int _a[] = new int[22];
int j,k = 0;
// 先把本体不同元素提取
for(int i = 0; i < this.array.length; i++) {
for(j = 0; j < other.array.length; j++) {
if(this.array[i] == other.array[j]) break;
}
if(j == other.array.length){
_a[k++] = this.array[i];
}
}
//后把other的元素全部提取
for(int i = 0; i < other.array.length; i++){
_a[k++] = other.array[i];
}
for(int i = 0; i < _a.length; i++)
a[i] = _a[i];
}
void insertArray(int a){
for(int i = 0; i < this.array.length; i++){
if(this.array[i] == 0){
this.array[i] = a;
break;
}
}
}
void popArray(int a){
for(int i = 0; i < this.array.length; i++){
if(this.array[i] == a){
for(int j = i; j < this.array.length-1; j++){
this.array[j] = this.array[j+1];
}
}
}
}
}
public class chapter4_5 {
public static void main(String[] arg){
int a[] = {30,20,40,50,80,21,10,22,34,55};
int b[] = {30,21,40,80,50,20,10,22,55,34};
int c[] = {31,21,40,50,87,20,16,22,39,55};//不同数组
int d[] = new int[22];
intSet S1 = new intSet(a);
intSet S2 = new intSet(b);
//判断俩对象是否一样
System.out.println((S1.isEqual(S2)));
//求俩对象的 交和并 集
// S1.Mixed(S2, d);
// S1.Combined(S2, d);
//遍历 交或并 集
// for(int i = 0; i < d.length; i++)
// if(d[i] != 0) System.out.print(d[i] + " ");
// System.out.println();
//添加一个元素
// S1.insertArray(60);
// S1.disArray();
//删除指定元素
// S1.popArray(10);
// S1.disArray();
}
}
运行实例:
```java
是否为相同数组:true
交集:20 40 50 21 22 55
并集:30 80 10 34 31 21 40 50 87 20 16 22 39 55
添加一个60元素:30 20 40 50 80 21 10 22 34 55 60
删除一个10元素:30 20 40 50 80 21 22 34 55
身份证的查验
这个实验图片来自太傅OJ
盲目分析
这个题目主要是弄清楚权重怎么算,然后char
类型、String
类型以及int
类型的转换。char
类型转int
类型,只需要减去一个'0'
,即可实现。String
类型转char
类型需要使用String
类的函数toCharArray()
即可转为char
数组。
之后判断前17位是否为数字,那么建一个方法来使代码更具可读性。这里方法我使用了正则表达式(regex),str.matches("^[0-9]*$")
表示匹配0-9
,全匹配则为true
否则为false
。
暴力破解
import java.util.Scanner;
class IDCard{
private String ID;
final private int power[] = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
final private char convert[] = {'1', '0', 'x', '9', '8', '7', '6', '5', '4', '3', '2'};
IDCard(String _id){
this.ID = _id;
}
private boolean isDigital(String str) {
return str == null || "".equals(str) ? false : str.matches("^[0-9]*$");
}
public boolean isValidID(){
if(this.ID.length() != 18) return false;
String pre17ID = this.ID.substring(0,17);
String at18ID = this.ID.substring(17,18);
char pre17C[] = null;
char at18IDC = at18ID.charAt(0);
int sum = 0;
if(isDigital(pre17ID)){
pre17C = pre17ID.toCharArray();
}else{
return false;
}
if(null != pre17C){
int pre17I[] = new int[pre17C.length];
// convertCharToInt
for(int i = 0; i < pre17C.length; i++){
pre17I[i] = pre17C[i] - '0';
}
// deal with power
for(int i = 0; i < pre17I.length; i++){
sum += pre17I[i] * power[i];
}
// compare the 18thIDCode
int Z = sum % 11;
if(convert[Z] == at18IDC){
return true;
}else{
return false;
}
}
return false;
}
}
public class checkoutID {
public static void main(String[] arg){
Scanner input = new Scanner(System.in);
IDCard id = new IDCard(input.nextLine());
input.close();
System.out.println(id.isValidID());
}
}
运行实例:
12010X198901011234
false
320124198808240056
true
110108196711301866
false
叨叨几句... 3 条评论
赶紧改错!
@太傅
大佬大佬,受教受教
@太傅
改好了,可以退下了嘛