L1-027. 出租,l1-027出租

來源:互聯網
上載者:User

L1-027. 出租,l1-027出租

下面是新浪微博上曾經很火的一張圖:

一時間網上一片求救聲,急問這個怎麼破。其實這段代碼很簡單,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};

 

時間限制400 ms記憶體限制65536 kB代碼長度限制8000 B判題程式Standard作者陳越 
import java.util.*;public class main {    public static void main(String[] args)    {        int[] tel = new int[11];        int[] arr = new int[10];        int[] tel1 = new int[11];        int[] index = new int[11];                Scanner in = new Scanner(System.in);                String l = in.next();                    for(int i = 0; i < 11; i++)        {            tel[i]=(int)(l.charAt(i) - '0');            tel1[i]= tel[i];        }                        for(int i = 0; i < 11; i++)        {            int pos = i;            for(int j = i+1; j < 11; j++)            {                if(tel1[j] > tel1[pos])                    pos = j;            }            if(pos != i)            {                int temp = tel1[pos];                tel1[pos] = tel1[i];                tel1[i] = temp;            }        }                int k = 0;        arr[k]= tel1[0];        for(int i = 1; i < 11; i++)        {            if(arr[k] != tel1[i])            {                k++;                arr[k] = tel1[i];            }        }                for(int i = 0; i < 11; i++)        {            for(int j = 0; j <= k; j++)            {                if(tel[i] == arr[j])                {                    index[i] = j;                }            }        }                System.out.print("int[] arr = new int[]{");        System.out.print(arr[0]);        for(int i = 1; i <= k; i++)        {            System.out.print("," + arr[i]);        }        System.out.println("};");                System.out.print("int[] index = new int[]{");        System.out.print(index[0]);        for(int i = 1; i < 11; i++)        {            System.out.print("," + index[i]);        }        System.out.println("};");    }}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.