A、 B 、 C 是 3 个字符串。把 A 中包含的所有 B 都替换为 C ,如果替换以后还有 B 就继续替换,直到 A 不包含 B 为止。
1、请编写程序实现以上功能。不允许使用系统提供的字符串比较、查找和替换函数。
public static void main(String[] args) {
String a ="aaasacaascabasniagabaasgaha";
String b ="as";
String c ="s";
System.out.println(replace(a,b,c));
}
public static String replace(String a,String b,String c){
char[] as = a.toCharArray();
char[] bs = b.toCharArray();
char[] cs = c.toCharArray();
//上次循环是否进行替换
boolean status = true;
go:while (true){
for (int i =0;i<as.length+1-bs.length;i++){
if (i==0) {
if (status) {
status = false;
} else {
break go;
}
}
//比较字符串
if (as[i]==bs[0]){
//是否含有需要替换的字符串
boolean status2 =true;
for (int j = 1; j <bs.length ; j++) {
if (as[i+j]==bs[j]){
}else {
status2=false;
}
}
//替换字符串
if (status2){
char[] newas =new char[as.length-bs.length+c.length()];
System.arraycopy(as,0,newas,0,i);
System.arraycopy(cs,0,newas,i,cs.length);
System.arraycopy(as,i+bs.length,newas,i+cs.length,as.length-(i+bs.length));
as=newas;
status=true;
}
}
}
}
return new String(as);
}
X-Y-Z是三维坐标系。线段AB保持A点不动,围绕Z轴方向(红色虚线示意)逆时针旋转(B点移动到C点位置),旋转角度相当于在XY平面投影的30度。求C点坐标。不用编程。
实在不会,从网络上搜的两个答案