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

一时间网上一片求救声,急问这个怎么破。其实这段代码很简单,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. VMware ESXi 7.0 U2 SLIC & Unlocker Intel NUC 专用镜像 202109 更新

    2021.08.31 更新:集成 "vmkusb-nic-fling" 和 "nvme-community",现在只有一个镜像. 2021.06.16 更新:集 ...

  2. 4.React生命周期

    4.React生命周期 4.1引出生命周期 class Life extends React.Component { state = { opacity:0.5 } death = () => ...

  3. python代码检查工具(静态代码审查)

    python静态代码检查 我们知道python是一门脚本语言,不像C#/Java等编译型语言可以在编译阶段就报出代码错误,脚本语言往往需要在运行期执行到这段代码时才会抛出代码错误. 那么在实际商业项目 ...

  4. CSS导航菜单(二级菜单)

    index.html <div class="nav"> <ul> <li> <a href="#">Java& ...

  5. table头部固定,内容滚动,类似新闻一下向上滚动

    html: <div class="ul_box"> <table class="table1"> <thead> < ...

  6. CTFd+ubuntu service搭建等待更新

    CTFd是一款基于Apache2.0的协议的开源CTF平台,最新版本目前为1.20.该平台功能强大,基本上能够满足目前的CTF竞赛需求,同时,该平台提供了强大的插件功能,可以自己进行插件开发实现自己的 ...

  7. 搭建GIT仓库

  8. VBox 虚拟机安装 Openwrt 做旁路由

    VBox 虚拟机安装 Openwrt 做旁路由 需求:开个虚拟机做旁路由,电脑把网关设置成旁路由地址,用它跑个上网或其他什么东西. 安装及配置过程简述 这件事流程很简单,总结起来主要有以下几点: 安装 ...

  9. 【Linux】LNMP1.6 环境报500错误解决方法

    vi /usr/local/nginx/conf/fastcgi.conf 进入编辑fastcgi.conf 找到  fastcgi_param  PHP_VALUE  "open_base ...

  10. FreeRTOS-06-信号量

    说明 本文仅作为学习FreeRTOS的记录文档,作为初学者肯定很多理解不对甚至错误的地方,望网友指正. FreeRTOS是一个RTOS(实时操作系统)系统,支持抢占式.合作式和时间片调度.适用于微处理 ...