两个机试题:对称密码;最大递减数

里面一些细节问题值得思考和注意:

1.标准输入

<span style="font-size:14px;">Scanner sc =new Scanner(System.in);
	String str=sc.next();</span>

2.str.length()是字符串长度,下标获取最大为str.length()-1

3.注意循环结束的条件,和状态,在对称密码中,是

当匹配错误的情况发生在cabbad的指针情况,所以是rear-front-1, 不是abba,rear-front+1

4.代码的逻辑性要清晰,注意对称密码2种代码格式:

for循环下注意条件写入while下嵌套if语句,逻辑清晰,并且,在代码1中for循环

<pre name="code" class="java">int (i=1;i<str.length()-1;i++)	不利于应用到别的场景,要写i<str.length(),在下面的嵌套中在进行条件赛选
 


5其实对称代码2是有错误的:用54123321111111,测试下。只需要交换两个if语句的顺序就

6.在最大递减数的代码中:List为接口,不能直接定义对象,要ArrayList实现。

Integer.parseInt(str)的用法;

字符串比对:98>9754


对称密码代码1:
<pre name="code" class="java">public class Main {
	public static void main(String[] args){
	Scanner sc =new Scanner(System.in);
	String str=sc.next();
	int max=1;
	int front=0;
	int rear=0;
	
	//i从1开始,到倒数第二位,因为在下面rear++时会charAt(rear)会超界,最后一项在最后if判断
	for(int i=1;i<str.length()-1;i++){
		
		//判断是不是abba的对称
		if(str.charAt(i)==str.charAt(i-1)){
			front=i-1;
			rear=i;
			while(front>=0&&rear<=str.length()-1&&str.charAt(front)==str.charAt(rear)){
				front--;
				rear++;
			}
			//判断是不是aba的对称
		}else if(str.charAt(i+1)==str.charAt(i-1)){
			front=i-1;
			rear=i+1;
			while(front>=0&&rear<=str.length()-1&&str.charAt(front)==str.charAt(rear)){
				front--;
				rear++;
			}
		}else{
			continue;
		}
		//当匹配错误的情况发生在cabbad的指针情况,所以是rear-front-1, 不是abba,rear-front+1
		max=max>rear-front-1?max:rear-front-1;
	}
	//做最后两项的匹配
	if(str.charAt(str.length()-1)==str.charAt(str.length()-2)){
		max=max>2?max:2;
	}
		
	System.out.println(max);	
	}		
}


 
<pre name="code" class="java">对称密码2:

 
<pre name="code" class="java">import java.util.Scanner;
//对称加密的最终版

public class Main3 {
	public static void main(String[] args){
	Scanner sc =new Scanner(System.in);
	String str=sc.next();
	int max=1;
	int front=0;
	int rear=0;
	
	//i从1开始,到倒数第二位,因为在下面rear++时会charAt(rear)会超界,最后一项在最后if判断
	for(int i=1;i<str.length();i++){
		
		//判断是不是abba的对称
		if(i<str.length()-1&&str.charAt(i+1)==str.charAt(i-1)){
			front=i-2;
			rear=i+2;
			while(front>=0&&rear<=str.length()-1){
				if(str.charAt(front)==str.charAt(rear)){
					front--;
					rear++;
				}else
					break;
				
			}
		}else if(str.charAt(i)==str.charAt(i-1)){
			front=i-2;
			rear=i+1;
			while(front>=0&&rear<=str.length()-1){
//				(front>=0&&rear<=str.length()-1&&str.charAt(front)==str.charAt(rear)){
				if(str.charAt(front)==str.charAt(rear)){
					front--;
					rear++;
				}else
					break;
				
			}
			//判断是不是aba的对称
		} else{
			continue;
		}
		//当匹配错误的情况发生在cabbad的指针情况,所以是rear-front-1, 不是abba,rear-front+1
		max=max>rear-front-1?max:rear-front-1;
	}
	
		
	System.out.println(max);	
	}		
}


 
最大递减数:
/最大递减数
public class Main2 {


<span style="white-space:pre">	</span>@SuppressWarnings("null")
<span style="white-space:pre">	</span>public static void main(String[] args){
<span style="white-space:pre">		</span>Scanner sc =new Scanner(System.in);
<span style="white-space:pre">		</span>String str = sc.next();
<span style="white-space:pre">		</span>List<String> list=new ArrayList<String>();
<span style="white-space:pre">		</span>
<span style="white-space:pre">		</span>
<span style="white-space:pre">		</span>
<span style="white-space:pre">		</span>//求递减字串,存入list
<span style="white-space:pre">		</span>for(int i=0;i<str.length()-1;){
<span style="white-space:pre">			</span>
<span style="white-space:pre">			</span>int j=i;
<span style="white-space:pre">			</span>while(str.charAt(j)>str.charAt(j+1)){
<span style="white-space:pre">				</span>if(j==str.length()-2){
<span style="white-space:pre">					</span>j++;
<span style="white-space:pre">					</span>break;
<span style="white-space:pre">				</span>}else{
<span style="white-space:pre">					</span>j++;
<span style="white-space:pre">				</span>}<span style="white-space:pre">				</span>
<span style="white-space:pre">			</span>}<span style="white-space:pre">			</span>
<span style="white-space:pre">			</span>list.add(str.substring(i, j+1));
<span style="white-space:pre">			</span>i=j+1;
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>
<span style="white-space:pre">		</span>String aMax=list.get(0);
<span style="white-space:pre">		</span>String bMax=list.get(0);
<span style="white-space:pre">		</span>int max=0;
<span style="white-space:pre">		</span>
<span style="white-space:pre">		</span>//求最大递减数
<span style="white-space:pre">		</span>for(int j=1;j<list.size();j++){
<span style="white-space:pre">			</span>
//<span style="white-space:pre">			</span>if(list.get(j).length()>aMax.length()){
//<span style="white-space:pre">				</span>aMax=list.get(j);
//<span style="white-space:pre">			</span>}else if(list.get(j).length()==aMax.length()){
//<span style="white-space:pre">			</span>//对于98和9677,不能这么单纯的比较,这样会认为98大
//<span style="white-space:pre">			</span>aMax=aMax.compareTo(list.get(j))>0?aMax:list.get(j);
//<span style="white-space:pre">			</span>}
<span style="white-space:pre">			</span>
<span style="white-space:pre">			</span>
<span style="white-space:pre">			</span>//直接转换成int类型
<span style="white-space:pre">			</span>aMax = Integer.parseInt(aMax)>Integer.parseInt(list.get(j))?aMax:list.get(j);
<span style="white-space:pre">			</span>
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>
<span style="white-space:pre">		</span>//求最大递减和
<span style="white-space:pre">		</span>for(int k=0;k<list.size();k++){
<span style="white-space:pre">			</span>int add=0;
<span style="white-space:pre">			</span>for(int m=0;m<list.get(k).length();m++){
<span style="white-space:pre">				</span>add=add+list.get(k).charAt(m)-48;
<span style="white-space:pre">			</span>}
<span style="white-space:pre">			</span>max=max>add?max:add;
<span style="white-space:pre">			</span>bMax=(max==add)?list.get(k):bMax   ;
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>
<span style="white-space:pre">		</span>
<span style="white-space:pre">		</span>System.out.print(aMax+" "+bMax);
<span style="white-space:pre">	</span>}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值