L1-027 出租 (20 分) java题解
下面是新浪微博上曾经很火的一张图:

一时间网上一片求救声,急问这个怎么破。其实这段代码很简单,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};
这道题怎么说,就是把数据放到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题解的更多相关文章
- L1-023 输出GPLT (20 分) java题解 GPLT天梯赛防坑技巧
上题目先 给定一个长度不超过10000的.仅由英文字母构成的字符串.请将字符重新调整顺序,按GPLTGPLT....这样的顺序输出,并忽略其它字符.当然,四种字符(不区分大小写)的个数不一定是一样多的 ...
- 顶点的度 (20 分) Java解法
顶点的度 顶点的图.给定一个有向图,输出各顶点的出度和入度. 输入格式: 输入文件中包含多个测试数据,每个测试数据描述了一个无权有向图.每个测试数据的第一行为两个正整数n 和m,1 ≤ n ≤ 100 ...
- 1002 写出这个数 (20 分) java解题
读入一个正整数 n,计算其各位数字之和,用汉语拼音写出和的每一位数字. 输入格式: 每个测试输入包含 1 个测试用例,即给出自然数 n 的值.这里保证 n 小于 10^100. 输出格式: 在一行内输 ...
- pat 1065 A+B and C (64bit)(20 分)(大数, Java)
1065 A+B and C (64bit)(20 分) Given three integers A, B and C in [−263,263], you are supposed t ...
- PAT(B) 1018 锤子剪刀布(C:20分,Java:18分)
题目链接:1018 锤子剪刀布 分析 用一个二维数组保存两人所有回合的手势 甲乙的胜,平,负的次数刚好相反,用3个变量表示就可以 手势单独保存在signs[3]中,注意顺序.题目原文:如果解不唯一,则 ...
- [PAT] 1144 The Missing Number(20 分)
1144 The Missing Number(20 分) Given N integers, you are supposed to find the smallest positive integ ...
- [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, ...
- 【剑指offer】(第 2 版)Java 题解
[剑指offer](第 2 版)Java 题解 第一章 面试的流程 略... 第二章 面试需要的基础知识 面试题 1. 赋值运算符函数 面试题 2. 实现 Singleton 模式 Solution ...
- L1-002 打印沙漏 (20 分)
L1-002 打印沙漏 (20 分) 方法:清晰思路,纸上写出实例,注意循环使用 本题要求你写个程序把给定的符号打印成沙漏的形状.例如给定17个“*”,要求按下列格式打印 ***** *** * ** ...
随机推荐
- tf.app.run() 运行结束时,报错:SystemExit exception: no description
环境:Python3.6.6 + tensorflow-gpu 源码如下: import tensorflow as tf def main(): print("hello tf.app.r ...
- 去除所有js,html,css代码
<?php$search = array ("'<script[^>]*?>.*?</script>'si", // 去掉 javascript ...
- 安装redis 6.0.6
1.规划目录:下载目录.安装目录.redis数据目录mkdir -p /data/appmkdir -p /opt/redis_cluster/redis_6379/{conf,logs,pid}mk ...
- 性能测试工具JMeter 基础(二)—— 主界面介绍
主界面介绍 JMeter的主界面主要分为菜单导航栏.工具栏.计划树标签栏.内容栏 菜单导航栏:全部的功能的都包含在菜单栏中 工具栏:相当于菜单栏常用功能的快捷按钮 计划树标签栏:显示测试用例(计划)相 ...
- Linux环境搭建及项目部署
一. VMWare安装图解 1.点击下一步 2.接受条款,下一步 3.选择安装目录,不建议有中文目录和空格目录.下一步 4.下一步 5.这两个选项根据可以爱好习惯选择,下一步 6.安装 7.完成 9. ...
- Appium问题解决方案(5)- selenium.common.exceptions.InvalidSelectorException: Message: Locator Strategy 'name' is not supported for this session
背景 使用Appium Server 1.15.1版本 执行了以下脚本 test = driver.find_element_by_name("自动化测试") print(test ...
- c# List集合类常用操作:二、增加
所有操作基于以下类 class Employees { public int Id { get; set; } public string Name { get; set; } public stri ...
- Android使用百度语音识别api代码实现。
第一步 ① 创建平台应用 点击百度智能云进入,没有账号的可以先注册账号,这里默认都有账号了,然后登录. 然后左侧导航栏点击找到语音技术 然后会进入一个应用总览页面, 然后点击创建应用 立即创建 点击查 ...
- C语言学习笔记---3.字符串格式化输入输出
1.C语言字符串 字符串(character string)是一个或多个字符的序列,例如:"Zing went the strings of my heart!" C语言没有专门用 ...
- JS预编译过程
GO和AO 变量的预编译 实例1 console.log(a); var a=1; console.log(a); 实际编译过程: 将a存入预编译对象中,赋值为undefined: 真正的赋值语句当程 ...