Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

The 3n + 1 Problem: A Computational Challenge in Number Theory, Quizzes of Programming Languages

Online Judge Program challenges

Typology: Quizzes

2019/2020

Uploaded on 05/25/2023

asmita-waghmare
asmita-waghmare 🇮🇳

1 document

1 / 43

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
100 The 3n+ 1 problem
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 nis 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
221134175226134020105168421
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 nsuch 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 before and including
the 1 is printed. For a given nthis is called the cycle-length of n. In the example above, the cycle
length of 22 is 16.
For any two numbers iand jyou are to determine the maximum cycle length over all numbers
between and including both iand j.
Input
The input will consist of a series of pairs of integers iand j, one pair of integers per line. All integers
will be less than 10,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 iand j.
You can assume that no operation overflows a 32-bit integer.
Output
For each pair of input integers iand jyou should output i,j, and the maximum cycle length for integers
between and including iand 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 iand jmust
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
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b

Partial preview of the text

Download The 3n + 1 Problem: A Computational Challenge in Number Theory and more Quizzes Programming Languages in PDF only on Docsity!

100 The 3 n + 1 problem

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 ←− 3 n + 1
  5. else n ←− n/
  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 before and including the 1 is printed. 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 and including both 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 10,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 operation 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

Universidad de Valladolid OJ: 100 – The 3 n + 1 problem 2/

Sample Output

if(c>=s)

{ s=c;

}

} cout<<s<<endl;

}

return 0;

}

Acceptance Letter:-

10189 Minesweeper

Have you ever played Minesweeper? It’s a cute little game which comes within a certain Operating System which name we can’t really remember. Well, the goal of the game is to find where are all the mines within a M × N field. To help you, the game shows a number in a square which tells you how many mines there are adjacent to that square. For instance, supose the following 4 × 4 field with 2 mines (which are represented by an ‘*’ character):

... .... ... ....

If we would represent the same field placing the hint numbers described above, we would end up with:

2210 1* 1110

As you may have already noticed, each square may have at most 8 adjacent squares.

Input

The input will consist of an arbitrary number of fields. The first line of each field contains two integers n and m ( 0 < n, m ≤ 100 ) which stands for the number of lines and columns of the field respectively. The next n lines contains exactly m characters and represent the field. Each safe square is represented by an ‘.’ character (without the quotes) and each mine square is represented by an ‘*’ character (also without the quotes). The first field line where n = m = 0 represents the end of input and should not be processed.

Output

For each field, you must print the following message in a line alone:

Field #x:

Where x stands for the number of the field (starting from 1). The next n lines should contain the field with the ‘.’ characters replaced by the number of adjacent mines to that square. There must be an empty line between field outputs.

Sample Input

Code: -

#include<stdio.h>

#include<string.h>

#define MAXSIZE 101

static char MineField[MAXSIZE][MAXSIZE];

static const int drow[] = {0, 1, 1, 1, 0, - 1, - 1, - 1};

static const int dcol[] = {-1, - 1, 0, 1, 1, 1, 0, - 1};

int main(){

int n, m;

int FieldNumber = 0; while( scanf("%d%d", &n, &m), n ){

getchar(); for(int i = 0; i < n; ++i)

scanf("%s", &MineField[i]);

if( FieldNumber ) printf("\n");

for(int i = 0; i < n; ++i){

for(int j = 0; j < m; ++j){ int temp = 0;

if( MineField[i][j] == '*' )

continue; for(int k = 0; k < 8; ++k){

if( i + drow[k] < 0 || j + dcol[k] < 0 || i + drow[k] >= n || j + dcol[k] >= m )

continue; if( MineField[i + drow[k] ][j + dcol[k]] == '*' )

++temp;

} MineField[i][j] = temp + '0';

}

} printf("Field #%d:\n", ++FieldNumber);

for(int i = 0; i < n; ++i){

for(int j = 0; j < m; ++j)

putchar(MineField[i][j]);

printf("\n"); }

}

return 0;

}

Acceptance Letter:-

Code: -

#include

using namespace std;

int n;

double c[1010];

int main() {

for(;;) { scanf("%d", &n); if(n == 0) break; double avg = 0; for(int i = 0; i < n; i++) { scanf("%lf", &c[i]); avg += c[i]; } avg = avg / n; double pos = 0, neg = 0; for(int i = 0; i < n; i++) { double v = (long) ((c[i] - avg) * 100.0) / 100.0; if(v > 0) pos += v; else neg += v; } neg = - neg; printf("$%.2f\n", neg > pos? neg : pos); }

}

Acceptance Letter:-

Code: -

#include<stdio.h>

#include<stdlib.h>

int main() {

int n, A[3001], i;

while(scanf("%d", &n) == 1) {

for(i = 0; i < n; i++) scanf("%d", &A[i]);

int mark[3001] = {0}, flag = 0;

for(i = 1; i < n; i++) { if(abs(A[i] - A[i-1]) < n)

mark[abs(A[i] - A[i-1])]++; }

for(i = 1; i < n; i++)

if(mark[i] == 0) flag = 1;

if(flag)

puts("Not jolly"); else

puts("Jolly");

} return 0;

}

Acceptance Letter: -

Universidad de Valladolid OJ: 10050 – Hartals 2/

Sample Output

Code: -

#include

#include

#include

using namespace std;

int main()

{

int N;

cin >> N;

int * results = new int[N]; for (int i=0; i<N; i++)

{

int days, nParties, screwedDays=0; cin >> days >> nParties;

int * hartals = new int[nParties];

for (int j=0; j<nParties; j++) cin >> hartals[j]; for (int j=0,daysPassed=1; daysPassed<=days; j++, j%= 7,daysPassed++)

if ((j != 5) && (j != 6))

for (int k=0; k<nParties; k++) if (!(daysPassed % hartals[k]))

{ screwedDays++; break;}

results[i] = screwedDays; }

for (int i=0; i<N; i++) cout << results[i] << endl; return 0;

}

843 Crypt Kicker

A common but insecure method of encrypting text is to permute the letters of the alphabet. That is, in the text, each letter of the alphabet is consistently replaced by some other letter. So as to ensure that the encryption is reversible, no two letters are replaced by the same letter. Your task is to decrypt several encoded lines of text, assuming that each line uses a different set of replacements, and that all words in the decrypted text are from a dictionary of known words.

Input

The input consists of a line containing an integer n, followed by n lower case words, one per line, in alphabetical order. These n words comprise the dictionary of words which may appear in the decrypted text. Following the dictionary are several lines of input. Each line is encrypted as described above. There are no more than 1000 words in the dictionary. No word exceeds 16 letters. The encrypted lines contain only lower case letters and spaces and do not exceed 80 characters in length.

Output

Decrypt each line and print it to standard output. If there is more than one solution, any will do. If there is no solution, replace every letter of the alphabet by an asterisk.

Sample Input

and dick jane puff spot yertle bjvg xsb hxsn xsb qymm xsb rqat xsb pnetfn xxxx yyy zzzz www yyyy aaa bbbb ccc dddddd

Sample Output

dick and jane and puff and spot and yertle


Code: -

#include <stdio.h>

#include <string.h>

#include

#include

#include

using namespace std;

string word[1005];

set W;

string buf[1005];

int n, m;

bool hasSol;

char mapped[128], mapped2[128];

char trans[128];

void dfs(int idx) {//decode buf[idx]

if(hasSol) return;

int i, j, err;

int exist = 1; if(idx == m) {

for(i = 0; i < m; i++) {

if(i) putchar(' '); for(j = 0; j < buf[i].length(); j++)

putchar(mapped[buf[i][j]]);

} puts("");

hasSol = 1;

return; }

for(i = 0; i < buf[idx].length(); i++) {

if(mapped[buf[idx][i]] == 0) exist = 0;

else {