7-3 出租 (20分)

下面是新浪微博上曾经很火的一张图:

在这里插入图片描述

一时间网上一片求救声,急问这个怎么破。其实这段代码很简单,index数组就是arr数组的下标,index[0]=2 对应 arr[2]=1,index[1]=0 对应 arr[0]=8,index[2]=3 对应 arr[3]=0,以此类推…… 很容易得到电话号码是18013820100。

本题要求你编写一个程序,为任何一个电话号码生成这段代码 —— 事实上,只要生成最前面两行就可以了,后面内容是不变的。

输入格式:
输入在一行中给出一个由11位数字组成的手机号码。

输出格式:
为输入的号码生成代码的前两行,其中arr中的数字必须按递减顺序给出。

输入样例:
18013820100

输出样例:
int[] arr = new int[]{8,3,2,1,0};
int[] index = new int[]{3,0,4,3,1,0,2,4,3,4,4};

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

//** Class for buffered reading int and double values *//*
class Reader {
	static BufferedReader reader;
	static StringTokenizer tokenizer;

	// ** call this method to initialize reader for InputStream *//*
	static void init(InputStream input) {
		reader = new BufferedReader(new InputStreamReader(input));
		tokenizer = new StringTokenizer("");
	}

	// ** get next word *//*
	static String next() throws IOException {
		while (!tokenizer.hasMoreTokens()) {
			// TODO add check for eof if necessary
			tokenizer = new StringTokenizer(reader.readLine());
		}
		return tokenizer.nextToken();
	}
	static boolean hasNext()throws IOException {
		return tokenizer.hasMoreTokens();
	}
	static String nextLine() throws IOException{
		return reader.readLine();
	}
	static char nextChar() throws IOException{
		return next().charAt(0);
	}
	static int nextInt() throws IOException {
		return Integer.parseInt(next());
	}

	static float nextFloat() throws IOException {
		return Float.parseFloat(next());
	}
}
public class Main {
	public static void main(String[] args) throws IOException {
		Reader.init(System.in);
		String a = Reader.nextLine();
		int []array = new int[10];
		for (int i = 0; i < a.length(); i++) {
			array[Integer.parseInt(a.charAt(i)+"")]++;
		}
		System.out.print("int[] arr = new int[]{");
		String temp = "";
		int [] result = new int[10];
		int index = 0;
		for (int i = 9; i >=0; i--) {
			if (array[i]>=1) {
				temp+=i+",";
				result[i] = index++;
			}
		}
		if (temp.charAt(temp.length()-1)==',') {
			temp = temp.substring(0, temp.length()-1);
		}
		System.out.println(temp+"};");
		
		
		System.out.print("int[] index = new int[]{");
		for (int i = 0; i < a.length()-1; i++) {
			System.out.print(result[Integer.parseInt(a.charAt(i)+"")]+",");
		}
		System.out.print(result[Integer.parseInt(a.charAt(a.length()-1)+"")]+"};");
	}
	

}



发布了45 篇原创文章 · 获赞 8 · 访问量 1784

猜你喜欢

转载自blog.csdn.net/weixin_43888039/article/details/104008589