Programming Challenge Description: You will be given a positive integer N, and t
ID: 3820207 • Letter: P
Question
Programming Challenge Description:
You will be given a positive integer N, and two single-digit integers p and q, where p != q and both are greater than 1. You must output all of the integers from 1 to N inclusive, separated by a comma ','.
However, any integer divisible by p or q should be replaced by the text OUT and any integer whose decimal representation contains digit p or q should be replaced by the text THINK. Integers for which both of the preceding statements are true should instead be replaced by the text OUTTHINK.
Input: A single line on standard input per test case: N p q
Output: The comma-separated sequence as described above (only numbers and uppercase chars), with no leading or trailing spaces.
Test 1
Test Input
Download Test Input
Expected Output
Download Test Output
Test 2
Test Input
Download Test Input
Expected Output
Download Test Output
Code that has to be completed:
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
class Program {
static void Main(string[] args) {
using (StreamReader reader = new StreamReader(Console.OpenStandardInput()))
while (!reader.EndOfStream) {
string line = reader.ReadLine();
Console.WriteLine(line);
}
}
}
Explanation / Answer
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
class Program {
static void Main(string[] args) {
int digit, i, j;
int N, p, q;
using (StreamReader reader = new StreamReader(Console.OpenStandardInput()))
while (!reader.EndOfStream) {
string line = reader.ReadLine();
Console.WriteLine(line);
}
/*Split the line into words and get out values for N, p, q */
words = line.split(' ');
N = Integer.parseInt(words[0]);
p = Integer.parseInt(words[1]);
q = Integer.parseInt(words[2]);
/* Loop i from 1 to N */
for(i = 1; i <= N; i++) {
/* Check if the number is divisible by p or q */
if(i%p == 0 || i%q == 0) {
System.out.println("OUT");
}
/*Check if the number has any digit equal to p or q */
for(int j = i; j > 0; j=j/10) {
/*digit = j modulus 10. Keep dividing by 10 till j is 0*/
digit = j%10;
/* if digit is p or q break out of the loop*/
if(digit == p || digit == q)
break;
}
/* if j is 0 that means loop completed without finding any matches */
/* also if i is not divisible by p or q, we just print i, since none of the conditions matched */
if(j == 0 && !(i%p == 0 || i%q == 0))
System.out.println(i);
else /* if j is not 0, means we found a digit in i that is equal to p or q */
System.out.println("THINK");
/* If this is not the last number print a comma separator */
if(i < N)
System.out.println(",");
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.