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};
#include <iostream>
#include<cstring>

using namespace std;
int t[11], b[11];


int main()
{
  memset(t, -1, sizeof(t));

   string s;
   int flag=0, sign=0;

   cin >> s;


   for(int i=0; s[i]; i++)
   {
       t[s[i]-'0'] = s[i]-'0';
   }
   cout << "int[] arr = new int[]{";

   int j=0;
   for(int i=9; i>=0; i--)
   {
       if(t[i] != i)
           continue;

       if(flag)
           cout << ',';
       if(t[i] == i)
       {
           cout << i;
           flag = 1;
           b[i] = j++;
       }
   }
   cout << "};" << endl;
   cout<<"int[] index = new int[]{";

    for(int i=0; s[i]!='\0'; i++)
    {
        if(sign)
            cout << ",";
        cout << b[s[i]-'0'];
        sign = 1;
    }
    cout << "};" << endl;
return 0;
}


### L1-027 出租 Problem Set Usage or Solution in Programming Context In the context of solving problems like L1-027 出租, using sets efficiently is crucial for managing unique elements and performing operations such as membership testing, union, intersection, difference, and symmetric difference. Sets are particularly useful when dealing with collections where each element should appear only once. For instance, consider a scenario where one needs to track different types of items being rented out without any duplicates. A Python `set` can be used effectively here: ```python rented_items = {"laptop", "projector", "microphone"} new_rental_request = ["speaker", "laptop"] # Adding new rental requests while avoiding duplicates for item in new_rental_request: rented_items.add(item) print(rented_items) # Output will include all unique items requested. ``` When it comes to more complex scenarios involving multiple datasets, set operations become even more powerful. For example, finding common rentals between two customers could involve an intersection operation: ```python customer_a_rentals = {"camera", "tripod", "lighting kit"} customer_b_rentals = {"sound mixer", "camera"} common_rentals = customer_a_rentals.intersection(customer_b_rentals) print(common_rentals) # This would output {'camera'} indicating shared rentals. ``` Regarding specific solutions tailored towards L1-027 出租 problem, understanding how sets interact within algorithms designed around this challenge becomes essential. The use of sets helps streamline data handling by ensuring that every piece of equipment listed under rent appears just once unless explicitly required otherwise through additional logic implemented outside simple set functionality[^1].
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值