码迷,mamicode.com
首页 > 其他好文 > 详细

L1-027 出租

时间:2019-01-21 13:53:23      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:https   std   微博   输入格式   mes   编写   count   names   class   

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

技术分享图片

一时间网上一片求救声,急问这个怎么破。其实这段代码很简单,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};
 
思路:用book数组标记实现数组去重再从大到小输出arr数组,再在这个数组中寻找下标输出index数组,注意格式问题......
 
 1 #include<iostream>
 2 #include<cstring>
 3 using namespace std;
 4 int main()
 5 {
 6     int book[10];
 7     memset(book,0,sizeof(book));
 8     char number[12];
 9     cin>>number;
10     for(int i=0;i<11;i++)
11         book[number[i]-0]=1;
12     cout<<"int[] arr = new int[]{";
13     int count;
14     int t=0;
15     int num[11];
16     for(int i=0;i<=9;i++)
17     {
18     if(book[i])
19     {
20         count=i;
21         break;
22     }
23     }
24     for(int i=9;i>=0;i--)
25     {
26         if(book[i])
27         {
28             num[t++]=i;
29         count==i?cout<<i:cout<<i<<",";
30         }
31     }
32     cout<<"};"<<endl<<"int[] index = new int[]{";
33     for(int i=0;i<10;i++)
34     {
35         for(int j=0;j<t;j++)
36         {
37             if((number[i]-0)==num[j])
38             {
39             cout<<j<<",";
40             continue;
41             }
42         }
43     }
44     for(int j=0;j<t;j++)
45     {
46         if((number[10]-0)==num[j])
47         {
48             cout<<j<<"};";
49             return 0;
50         }
51     }
52     return 0;
53 }

 

 

L1-027 出租

标签:https   std   微博   输入格式   mes   编写   count   names   class   

原文地址:https://www.cnblogs.com/xwl3109377858/p/10298106.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!