CSUFTACM2017寒假习题解析(第1—3题)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_39360985/article/details/79124250

第一题:

Julius Caesar lived in a time of danger and intrigue. The hardest situation Caesar ever faced was keeping himself alive. In order for him to survive, he decided to create one of the first ciphers. This cipher was so incredibly sound, that no one could figure it out without knowing how it worked.

You are a sub captain of Caesar’s army. It is your job to decipher the messages sent by Caesar and provide to your general. The code is simple. For each letter in a plaintext message, you shift it five places to the right to create the secure message (i.e., if the letter is ‘A’, the cipher text would be ‘F’). Since you are creating plain text out of Caesar’s messages, you will do the opposite:

Cipher text
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

Plain text
V W X Y Z A B C D E F G H I J K L M N O P Q R S T U

Only letters are shifted in this cipher. Any non-alphabetical character should remain the same, and all alphabetical characters will be upper case.

input:

Input to this problem will consist of a (non-empty) series of up to 100 data sets. Each data set will be formatted according to the following description, and there will be no blank lines separating data sets. All characters will be uppercase.

A single data set has 3 components:

Start line - A single line, “START”
Cipher message - A single line containing from one to two hundred characters, inclusive, comprising a single message from Caesar
End line - A single line, “END”

Following the final data set will be a single line, “ENDOFINPUT”.

output:

For each data set, there will be exactly one line of output. This is the original message by Caesar.

Sample Input:

START
NS BFW, JAJSYX TK NRUTWYFSHJ FWJ YMJ WJXZQY TK YWNANFQ HFZXJX
END
START
N BTZQI WFYMJW GJ KNWXY NS F QNYYQJ NGJWNFS ANQQFLJ YMFS XJHTSI NS WTRJ
END
START
IFSLJW PSTBX KZQQ BJQQ YMFY HFJXFW NX RTWJ IFSLJWTZX YMFS MJ
END
ENDOFINPUT

Sample Output:

IN WAR, EVENTS OF IMPORTANCE ARE THE RESULT OF TRIVIAL CAUSES
I WOULD RATHER BE FIRST IN A LITTLE IBERIAN VILLAGE THAN SECOND IN ROME
DANGER KNOWS FULL WELL THAT CAESAR IS MORE DANGEROUS THAN HE

具体代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;

char a[205],str[205];
char s[27]= {'V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U'};
int main()
{
    while(gets(a))
    {
        if(strcmp(a,"ENDOFINPUT")==0)
        {
            break;
        }
        if(strcmp(a,"START")==0)
        {
            gets(str);
            int len=strlen(str);
            for(int i=0;i<len;i++)
            {
                if(str[i]>='A'&&str[i]<='Z')
                    str[i]=s[str[i]-'A'];
            }
            printf("%s\n",str);
        }
    }
    return 0;
}

第二题:

A color reduction is a mapping from a set of discrete colors to a smaller one. The solution to this problem requires that you perform just such a mapping in a standard twenty-four bit RGB color space. The input consists of a target set of sixteen RGB color values, and a collection of arbitrary RGB colors to be mapped to their closest color in the target set. For our purposes, an RGB color is defined as an ordered triple (R,G,B) where each value of the triple is an integer from 0 to 255. The distance between two colors is defined as the Euclidean distance between two three-dimensional points. That is, given two colors (R1,G1,B1) and (R2,G2,B2), their distance D is given by the equation

Input:

The input is a list of RGB colors, one color per line, specified as three integers from 0 to 255 delimited by a single space. The first sixteen colors form the target set of colors to which the remaining colors will be mapped. The input is terminated by a line containing three -1 values.

Output:

For each color to be mapped, output the color and its nearest color from the target set.

If there are more than one color with the same smallest distance, please output the color given first in the color set.

Sample Input:

0 0 0
255 255 255
0 0 1
1 1 1
128 0 0
0 128 0
128 128 0
0 0 128
126 168 9
35 86 34
133 41 193
128 0 128
0 128 128
128 128 128
255 0 0
0 1 0
0 0 0
255 255 255
253 254 255
77 79 134
81 218 0
-1 -1 -1

Sample Output:

(0,0,0) maps to (0,0,0)
(255,255,255) maps to (255,255,255)
(253,254,255) maps to (255,255,255)
(77,79,134) maps to (128,128,128)
(81,218,0) maps to (126,168,9)

具体代码:

#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;

typedef struct
{
    int R;
    int G;
    int B;
}RGB;

int main()
{
    RGB rgb[16];
    for(int i=0;i<16;i++)
    {
        cin>>rgb[i].R>>rgb[i].G>>rgb[i].B;
    }
    double r,g,b;
    while((cin>>r>>g>>b)&&r!=-1 && g!=-1 && b!=-1)
    {

        double m=255*255*3;
        double d;
        int j=0;
        for(int i=0;i<16;i++)
        {
            d=sqrt((rgb[i].R-r)*(rgb[i].R-r)+(rgb[i].G-g)*(rgb[i].G-g)+(rgb[i].B-b)*(rgb[i].B-b));
            if(d<m)
            {
                m=d;
                j=i;
            }

        }
        cout<<"("<<r<<","<<g<<","<<b<<")"<<" maps to "<<"("<<rgb[j].R<<","<<rgb[j].G<<","<<rgb[j].B<<")"<<endl;
    }
    return 0;
}

第三题:

Problems in Computer Science are often classified as belonging to a certain class of problems (e.g., NP, Unsolvable, Recursive). In this problem you will be analyzing a property of an algorithm whose classification is not known for all possible inputs.

Consider the following algorithm:

1.      input n 

2.      print n 

3.      if n = 1 then STOP 

4.           if n is odd then n <- 3n + 1 

5.           else n <- n / 2 

6.      GOTO 2 

Given the input 22, the following sequence of numbers will be printed 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1

It is conjectured that the algorithm above will terminate (when a 1 is printed) for any integral input value. Despite the simplicity of the algorithm, it is unknown whether this conjecture is true. It has been verified, however, for all integers n such that 0 < n < 1,000,000 (and, in fact, for many more numbers than this.)

Given an input n, it is possible to determine the number of numbers printed (including the 1). For a given n this is called the cycle-length of n. In the example above, the cycle length of 22 is 16.

For any two numbers i and j you are to determine the maximum cycle length over all numbers between i and j.

Input:

The input will consist of a series of pairs of integers i and j, one pair of integers per line. All integers will be less than 1,000,000 and greater than 0.

You should process all pairs of integers and for each pair determine the maximum cycle length over all integers between and including i and j.

You can assume that no opperation overflows a 32-bit integer.

Output:

For each pair of input integers i and j you should output i, j, and the maximum cycle length for integers between and including i and j. These three numbers should be separated by at least one space with all three numbers on one line and with one line of output for each line of input. The integers i and j must appear in the output in the same order in which they appeared in the input and should be followed by the maximum cycle length (on the same line).

Sample Input:

1 10
100 200
201 210
900 1000

Sample Output:

1 10 20
100 200 125
201 210 89
900 1000 174

具体代码:

#include <iostream>

using namespace std;

int main()
{
    int i,j,m;
    int _i,_j;
    int max,count;
    while(cin>>i>>j)
    {
        _i=i;
        _j=j;
        max=0;
        if(i>j)
        {
            m=i;
            i=j;
            j=m;
        }
        for(int n=i;n<=j;n++)
        {
            count=1;
            m=n;
            if(m==1)
                count=1;
            while(m!=1)
            {
                if(m%2==0)
                    m/=2;
                else
                    m=m*3+1;
                count++;
            }
            if(max<count)
                max=count;
        }
        cout<<_i<<" "<<_j<<" "<<max<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_39360985/article/details/79124250