blue bridge cup questions

Number of hailstones【Simulation, 2016, provincial competition】

Given any positive integer N,

If it is an even number, execute: N/2;

If it is odd, execute: N×3+1,

The generated new numbers perform the same action again, and the cycle repeats.

Through observation, it is found that this number will rise to a high level for a while, and then fall down again.

It goes up and down like this, but it will eventually fall to "1", which is a bit like the way small hail particles roll and grow in a hail cloud.

For example, N=9,

9,28,14,7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1

It can be seen that when N=9, this "little hailstone" rushed to a height of 52 at the highest.

enter description

Enter a positive integer) N (N<106).

output description

Output a positive integer, indicating the number not greater than N, after the conversion process of the number of hailstones, how much it hits the highest.

Input and output samples

example

enter

10

output

52

operating limit

language maximum run time Maximum running memory
C++ 1s 256M
C 1s 256M
Java 2s 256M
Python3 15s 256M
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
  long long int n,max=0;
  scanf("%lld",&n);
  for(long long int i=2;i<=n;i++){
    long long int t=i;
    while(t!=1){
      if(t&1){t=t*3+1;}
      else {t>>=1;}
      if(t>max){max=t;}
    }
    
  }
  printf("%lld",max);
  return 0;
}


ISBN number (analogue, string, 2008, NOIP popularity group)

topic description

Each officially published book has an ISBN number corresponding to it. The ISBN code includes 9 digits, 1 identification code and 3 separators. Its prescribed format is "x-xxx-xxxxx-x", where the symbol " -" is the separator (the minus sign on the keyboard), and the last digit is the identification code, for example, 0-670-82162-4 is a standard ISBN code. The first digit of the ISBN code indicates the publication language of the book, such as 0 for English; the three digits after the first separator "-" represent the publisher, for example 670 for Virgin Press; the five digits after the second separator Represents the serial number of the book at the publishing house; the last digit is the identification code.

The identification code is calculated as follows:

The first digit is multiplied by 1 plus the second digit is multiplied by 2... By analogy, use the result mod 11, and the remainder is the identification code. If the remainder is 10, the identification code is a capital letter X. For example, the identification code 4 in the ISBN number 0-670-82162-4 is obtained in this way: for the 9 numbers of 067082162, from left to right, multiply them by 1, 2, ..., 9, and then sum them up, that is, 0× 1+6×2+……+2×9=158, then take the result 4 of 158 mod 11 as the identification code. Your task is to write a program to judge whether the identification code in the input ISBN number is correct, and if it is correct, it will only output  Right; if it is wrong, it will output the ISBN number that you think is correct.

enter description

Enter a line, which is a sequence of characters representing the ISBN number of a book (make sure the input conforms to the format requirements of the ISBN number).

output description

Output one line, if the identification code of the input ISBN number is correct, then output Right, otherwise, output the correct ISBN number (including separator "-") according to the specified format.

Input and output samples

Example 1

enter

0-670-82162-4

output

Right

Example 2

enter

0-670-82162-0

output

0-670-82162-4 

operating limit

  • Maximum running time: 1s
  • Maximum running memory: 128M

#include<stdio.h>
int main(){
  char arr[14],ch[12]="0123456789X";
    scanf("%s",arr);
      int i,j=1,sum=0;
        for(i=0;i<12;i++){
            if(arr[i]=='-')continue;
                sum+=(arr[i]-'0')*(j++);
                  }
                    if(ch[sum%11]==arr[12])printf("Right");
                      else {arr[12]=ch[sum%11];
                              printf("%s",arr);}
                              return 0;
                              }

Guess you like

Origin blog.csdn.net/m0_74154295/article/details/130300858