L1-027. Rental

 Here is a picture that was once very popular on Sina Weibo:

For a while, there was a cry for help on the Internet, asking how to break this. In fact, this code is very simple, the index array is the subscript of the arr array, index[0]=2 corresponds to arr[2]=1, index[1]=0 corresponds to arr[0]=8, index[2]=3 Corresponding to arr[3]=0, and so on... It is easy to get the phone number 18013820100.

This question asks you to write a program that generates this code for any phone number -- in fact, just generate the first two lines, and the rest will be unchanged.

Input format:

Enter an 11-digit mobile number given in one line.

Output format:

Generate the first two lines of code for the entered number, where the numbers in arr must be given in descending order.

Input sample:
18013820100
Sample output:
int[] arr = new int[]{8,3,2,1,0};
int[] index = new int[]{3,0,4,3,1,0,2,4,3,4,4};

Code:

#include<stdio.h>
intmain()
{
    int i,j,n,m,k=0,t;
    int a[20],b[20];
    char str[20];
    scanf("%s",str);
    for(i=0;str[i]!='\0';i++)
    {
        a[str[i]-'0']=1;
    }
    printf("int[] arr = new int[]{");
    int flag=0;
    for(i=9;i>=0;i--)
    {
        if(a[i]==1)
        {
            b[i]=k;
            k++;
            if(flag==0)
            {
                printf("%d",i);
                flag=1;
            }
            else
            {
                printf(",%d",i);
            }
        }
    }
    printf("};\n");
    printf("int[] index = new int[]{");
    for(i=0;str[i]!='\0';i++)
    {
        if(i==0)
        {
            printf("%d",b[str[i]-'0']);
        }
        else
        {
            printf(",%d",b[str[i]-'0']);
        }
    }
    printf("};");
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325517550&siteId=291194637