



































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Online Judge Program challenges
Typology: Quizzes
1 / 43
This page cannot be seen from the preview
Don't miss anything!
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:
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.
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.
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).
Universidad de Valladolid OJ: 100 – The 3 n + 1 problem 2/
if(c>=s)
{ s=c;
}
} cout<<s<<endl;
}
return 0;
}
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.
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.
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.
#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;
}
#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); }
}
#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;
}
Universidad de Valladolid OJ: 10050 – Hartals 2/
#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;
}
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.
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.
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.
and dick jane puff spot yertle bjvg xsb hxsn xsb qymm xsb rqat xsb pnetfn xxxx yyy zzzz www yyyy aaa bbbb ccc dddddd
dick and jane and puff and spot and yertle
#include <stdio.h>
#include <string.h>
#include
#include
#include
using namespace std;
string word[1005];
set
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 {