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

一时间网上一片求救声,急问这个怎么破。其实这段代码很简单,index数组就是arr数组的下标,index[0]=2 对应 arr[2]=1index[1]=0 对应 arr[0]=8index[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};

这道题怎么说,就是把数据放到set,然后简单的排序和搜索,。。。

 1 import java.io.BufferedReader;
2 import java.io.InputStreamReader;
3 import java.util.HashSet;
4 import java.util.Iterator;
5 import java.util.Set;
6
7 public class Main {
8 public static void main(String args[]) throws Exception{
9 BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
10 String str=br.readLine();
11 Set<Integer> set = new HashSet<>();
12 for (int i=0;i<str.length();i++)set.add(Integer.parseInt(str.charAt(i)+""));
13 int arr[]=new int[set.size()];
14 int indexs[]=new int[11];
15 Iterator it=set.iterator();
16 int index=0;
17 while (it.hasNext()){
18 arr[index]=(int)it.next();
19 index++;
20 }
21 sort(arr);
22 System.out.print("int[] arr = new int[]{");
23 for (int i=0;i<arr.length;i++){
24 System.out.print(arr[i]);
25 if (i<arr.length-1)System.out.print(",");
26 }
27 System.out.print("};\n");
28 SearchSort(arr,indexs,str);
29 System.out.print("int[] index = new int[]{");
30 for (int i=0;i<indexs.length;i++){
31 System.out.print(indexs[i]);
32 if (i<indexs.length-1)System.out.print(",");
33 }
34 System.out.print("};");
35 }
36 public static void sort(int arr[]){
37 int max;
38 for (int i=0;i<arr.length;i++){
39 max=i;
40 for (int j=i;j<arr.length;j++){
41 if (arr[j]>arr[max]){
42 max=j;
43 }
44 }
45 if (max!=i){
46 int temp=arr[i];
47 arr[i]=arr[max];
48 arr[max]=temp;
49 }
50 }
51 }
52 public static void SearchSort(int arr[],int index[],String str){
53 for (int i=0;i<index.length;i++){
54 for (int j=0;j<arr.length;j++){
55 int a=Integer.parseInt(str.charAt(i)+"");
56 if (a==arr[j]){
57 index[i]=j;
58 break;
59 }
60 }
61 }
62 }
63 }

 

L1-027 出租 (20 分) java题解的更多相关文章

  1. L1-023 输出GPLT (20 分) java题解 GPLT天梯赛防坑技巧

    上题目先 给定一个长度不超过10000的.仅由英文字母构成的字符串.请将字符重新调整顺序,按GPLTGPLT....这样的顺序输出,并忽略其它字符.当然,四种字符(不区分大小写)的个数不一定是一样多的 ...

  2. 顶点的度 (20 分) Java解法

    顶点的度 顶点的图.给定一个有向图,输出各顶点的出度和入度. 输入格式: 输入文件中包含多个测试数据,每个测试数据描述了一个无权有向图.每个测试数据的第一行为两个正整数n 和m,1 ≤ n ≤ 100 ...

  3. 1002 写出这个数 (20 分) java解题

    读入一个正整数 n,计算其各位数字之和,用汉语拼音写出和的每一位数字. 输入格式: 每个测试输入包含 1 个测试用例,即给出自然数 n 的值.这里保证 n 小于 10^100. 输出格式: 在一行内输 ...

  4. pat 1065 A+B and C (64bit)(20 分)(大数, Java)

    1065 A+B and C (64bit)(20 分) Given three integers A, B and C in [−2​63​​,2​63​​], you are supposed t ...

  5. PAT(B) 1018 锤子剪刀布(C:20分,Java:18分)

    题目链接:1018 锤子剪刀布 分析 用一个二维数组保存两人所有回合的手势 甲乙的胜,平,负的次数刚好相反,用3个变量表示就可以 手势单独保存在signs[3]中,注意顺序.题目原文:如果解不唯一,则 ...

  6. [PAT] 1144 The Missing Number(20 分)

    1144 The Missing Number(20 分) Given N integers, you are supposed to find the smallest positive integ ...

  7. [PAT] 1140 Look-and-say Sequence(20 分)

    1140 Look-and-say Sequence(20 分)Look-and-say sequence is a sequence of integers as the following: D, ...

  8. 【剑指offer】(第 2 版)Java 题解

    [剑指offer](第 2 版)Java 题解 第一章 面试的流程 略... 第二章 面试需要的基础知识 面试题 1. 赋值运算符函数 面试题 2. 实现 Singleton 模式 Solution ...

  9. L1-002 打印沙漏 (20 分)

    L1-002 打印沙漏 (20 分) 方法:清晰思路,纸上写出实例,注意循环使用 本题要求你写个程序把给定的符号打印成沙漏的形状.例如给定17个“*”,要求按下列格式打印 ***** *** * ** ...

随机推荐

  1. vue 之 后端返回空字符串用 null 和 “”以及 undefind 判断不到的问题

    原文: <!-- <span v-if="scope.row.buyer_credit_score != '' || scope.row.buyer_credit_score ! ...

  2. 查看所有日志命令:journalctl

    journalctl命令作用:实时查看所有日志(内核日志和应用日志) 语法格式: journalctl [参数] 常用参数:-k 查看内核日志-b 查看系统本次启动的日志-u 查看指定服务的日志-n ...

  3. 前端路由原理之 hash 模式和 history 模式

    什么是路由? 个人理解路由就是浏览器 URL 和页面内容的一种映射关系. 比如你看到我这篇博客,博客的链接是一个 URL,而 URL 对应的就是我这篇博客的网页内容,这二者之间的映射关系就是路由. 其 ...

  4. Python - 如何将 list 列表作为数据结构使用

    列表作为栈使用 栈的特点 先进后出,后进先出 如何模拟栈? 先在堆栈尾部添加元素,使用 append() 然后从堆栈顶部取出一个元素,使用 pop() # 模拟栈 stack = [1, 2, 3, ...

  5. Python - repr()、str() 的区别

    总的来说 str():将传入的值转换为适合人阅读的字符串形式 repr():将传入的值转换为 Python 解释器可读取的字符串形式 传入整型 # number resp = str(1) print ...

  6. AOP联盟通知类型和Spring编写代理半自动

    一.cglib功能更强大 二.Spring核心jar包 三.AOP联盟通知 三.代码实现Spring半自动代理 1.环绕通知的切面 2.bean.xml配置 3.创建bean容器,获取bean,即已经 ...

  7. 【Git】给不同目录配置不同的用户名和邮箱

    场景 使用 git 时,对于公司项目和个人项目想用不同的用户名和邮箱提交,简单的解决方式就是对 git 仓库单独配置 user.name 和 user.email: 直接修改当前仓库的 .git/co ...

  8. systemctl添加自定义系统服务

    [Service] Type=forking ExecStart=绝对路径 ExecStop=绝对路径 ExecReload=绝对路径 以上最精简版,文件/usr/lib/systemd/system ...

  9. Windows 11抢先体验

    SHA1值: 3B6DA9194BA303AC7DBBF2E521716C809500919C 谷歌云:https://drive.google.com/file/d/1sH0cBI9hwh8EdlV ...

  10. 使用PHP获取图像文件的EXIF信息

    在我们拍的照片以及各类图像文件中,其实还保存着一些信息是无法直观看到的,比如手机拍照时会有的位置信息,图片的类型.大小等,这些信息就称为 EXIF 信息.一般 JPG . TIFF 这类的图片文件都会 ...