研发岗上机题:环状字母结构,按规则移除节点,输出到文件。

使用你最擅长的编程语言进行编程
从d:\input.txt 中读入全部的字母(空格除外),将全部的字母构成环状结构, 从键盘读入整数n1,n2,

请做如下操作:

以A 节点后第n1 个节点为起点,将步长为n2 节点移除环,作为输出的第一个节点,

并将该点的下一个节点作为起点,再移除步长为n2 的节点,直至环中所有节点全部移除,

将移除的节点序列,输出到d:\output.txt 文件中。

举例:
设n1 = 3

设n2 =5


A –>B–> C–> D–> E–> F–> G–> H–> I–> J–> K–> L–> M–> N–> O–> P–> Q–> R–> S–>T–> U–> V–> W–> X–> Y–> Z‐>A

操作
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
G 移除
A B C D E F H I J KL M N O P Q R S T U V W X Y Z
L 移除
A B C D E F H I J K M N O P Q R S T U V W X Y Z
Q 移除

输出序列:
d:\output.txt 的内容如下

G L Q V A …

要求:在任何输入情况下,程序不能崩溃!

 

package com.company.java;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

import static javax.swing.UIManager.get;

public class DealCircle {
    public static void main(String[] args) throws IOException {
        /**
         * 使用你最擅长的编程语言进行编程
         * 从d:\input.txt 中读入全部的字母(空格除外),将全部的字母构成环状结构,
         * 从键盘读入整数n1,n2,请做如下操作:
         * 以A 节点后第n1 个节点为起点,将步长为n2 节点移除环,作为输出的第一个节点,
         * 并将该点的下一个节点作为起点,再移除步长为n2 的节点,直至环中所有节点全部移除,
         * 将移除的节点序列,输出到d:\output.txt 文件中。
         * 举例:
         * 设n1 = 3, 设n2 =5
         * 环
         * A –>B–> C–> D–> E–> F–> G–> H–> I–> J–> K–> L–> M–> N–> O–> P–> Q–> R–> S–> T–> U–> V–> W–> X–> Y–> Z‐>A
         *
         * 操作
         * A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
         * G 移除
         * A B C D E F H I J KL M N O P Q R S T U V W X Y Z
         * L 移除
         * A B C D E F H I J K M N O P Q R S T U V W X Y Z
         * Q 移除
         * …
         *
         * 输出序列:
         * d:\output.txt 的内容如下:
         * G L Q V A …
         *
         * 要求:在任何输入情况下,程序不能崩溃!
         **/
        /**
         * 递归函数
         * 自己调用自己
         * 什么时候结束
         */
        //初始化
//        String  circle = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

        String circle = readTxtFile("D://char.txt");

        int startpoint = 3;
        int interval = 5;
        startpoint = startpoint - 1;
        interval = interval - 1;


        //读取文件,获取字符串
        String fileaddress ="";

        createFile("D://dealChar.txt");

        dealCircle(circle,startpoint,interval);


    }


    //处理字符串,递归函数
    private static void dealCircle(String circle,int sp,int interval){
        System.out.println("===================================");
        //环长度
        int cl = circle.length();

        //处理的前半部分,新环的后半部分
        StringBuffer dealLetter = new StringBuffer();
        //处理的后半部分,新环的前半部分
        StringBuffer leftLetter = new StringBuffer();

        //取出的字符集合
//        List letterList = new ArrayList();
        StringBuffer pickLetters = new StringBuffer();


        //环长度大于等于步长
//        if(cl>=interval){

            for(int i=0;i<cl;){
                //当前起始点
//                System.out.println("当前起始点为 :" + circle.charAt(sp));

                if(i < sp + interval) {


                    char getLetter = circle.charAt(i);
//                    System.out.println(getLetter);
                    //拼装新环后面
                    dealLetter.append(getLetter);


                }else if (i == sp + interval){


                    //相等的时候取出字符
                    char getLetter = circle.charAt(i);
                    //System.out.println("下标=起始点=步长大的情况,取出字符 : "+getLetter);
                    //放进新list里面
//                    letterList.add(getLetter);
                    pickLetters.append(getLetter);

                    //下一个起点位置
                    int nextsp = sp + interval + 1;
//                    System.out.println("下一个起点 : "+circle.charAt(sp)+"."+sp);
                    //判断当前起点+步数与环长的大小关系。
                    if(nextsp>cl){//开始拼装新环,并重置初始关系
                        sp = sp;
                    }else {
                        sp = nextsp;
                    }

                }
                i++;
            }

//        }else{
//            System.out.println("stop=============");
//        }

        String newCircle = null;

        //旧环
        System.out.println("旧环 :" + circle);
        //拿出的字符
//        String pickLetter = letterList.toString();
        String pickLetter = pickLetters.toString();
        System.out.println("拿出的字符  :" + pickLetter);

        writeTxtFile(pickLetter, "D://dealChar.txt", true);

//如果旧环长度大于起始点位置
        if(circle.length()>sp){
            //        System.out.println("sp = "+sp);
            Character splitChar = circle.charAt(sp);
//      System.out.println("splitChar = "+splitChar);

            //旧环后半部分
            String beforeLetter = circle.substring(circle.indexOf(splitChar));
//        System.out.println("旧环后半部分 :" + beforeLetter);

            //未处理新环
//        System.out.println("未处理新环 :" + dealLetter);
            //新环前半部分
            String behindLetter= dealLetter.substring(0,dealLetter.toString().indexOf(splitChar));
//        System.out.println("新环前半部分 :" + behindLetter);

            //新环
            newCircle= beforeLetter+behindLetter;
            System.out.println("新环       :" + newCircle);
        }else{
            //处理旧环

            //处理新环
//            for(int i=0;i<letterList.size();i++){
//
//                String rpc = letterList.get(i).toString();
                System.out.println("rpc : "+rpc);
//                circle = circle.replace(rpc,"");
//
//            }
            for(int i=0;i<pickLetters.length();i++){

                String rpc = pickLetters.charAt(i)+"";
//                System.out.println("rpc : "+rpc);
                circle = circle.replace(rpc,"");

            }

            //新环
            newCircle= circle;
            System.out.println("新环       :" + newCircle);

        }


        if(newCircle.length()>interval){
            sp=0;
            dealCircle(newCircle,sp,interval);
        }

    }

    /**
     * 读取txt
     */

    public static String  readTxtFile(String filepath) throws IOException {
        String result="";//存读取的内容
        String thisline=null;//存读取的行值

        File file =  new File(filepath);

        if(file.exists()&&file.isFile()){//判定文件存在,并且是正确的文件格式

            FileInputStream fileInputStream = new FileInputStream(filepath);
            InputStreamReader inputStreamReader =new InputStreamReader(fileInputStream);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

            try {

                while ((thisline = bufferedReader.readLine())!=null){

                    result+=thisline;

                }
                bufferedReader.close();
            }catch (FileNotFoundException e){
                e.printStackTrace();
            }

        }

        return result;
    }

    /**
     * 创建txt
     */
    public static Boolean createFile(String filePath) {
        Boolean flag = false;
        File newF = new File(filePath);//给一个文件初始化-个地址 if(!newF.exists())//判断这个文件是否存在,避免建的文件重复
        try {
            newF.createNewFile();//如果这个文件不存在,那么创建它
        } catch (IOException e) {
            e.printStackTrace();
        }
        flag = true;//给标记值赋值}

        return flag;
    }

    /**
     * 向TXT中写入内容,并且可以选择在原来的基础上追加新内容,也可以覆盖旧内容
     * @param content
     * @param filePath
     * @param append
     * @return
     */

    public static boolean writeTxtFile(String content,String filePath,boolean append) {
        boolean flag = false;
        File thisFile = new File(filePath);
        try {
            if (!thisFile.exists()) {
                thisFile.createNewFile();//如果不存在创建这个文件
            }
                FileWriter fw = new FileWriter(filePath,append);//append表示可追加
                fw.write(content);
                fw.close();
//                BufferedWriter bw = new BufferedWriter(new FileWriter(filePath));
//                bw.write(content);
//                bw.close();
            } catch(IOException e){
                e.printStackTrace();
            }
            flag = true;
            return flag;
        }


}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值