题解 | #统计回文#

统计回文

https://www.nowcoder.com/practice/9d1559511b3849deaa71b576fa7009dc

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
       String A=in.nextLine();
       String B=in.nextLine();
       int count=0;//统计正确的插入方法
       for(int i=0;i<=A.length();i++){  //插入0...length下标
        StringBuffer str=new StringBuffer(A);
        str.insert(i,B);//插入到i下标
        StringBuffer tmp=new StringBuffer(str);//作为交换的媒介
        tmp.reverse();//翻转,判断
        if(str.toString().equals(tmp.toString())){//相等,则为正确的插入方法
        count++;
        }

       }
        System.out.println(count);
       
    }
}

全部评论

相关推荐

03-31 16:42
已编辑
郑州西亚斯学院 后端
Java抽象带篮子:你简历少了几个模块看上去就感觉信息很少,简历怎么写可以看看我发的帖子
点赞 评论 收藏
分享
04-03 12:09
東京大学 C++
求求求求暑期offer:留第一行,剩下的不要
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务