Programming Challenge Description: You will be given a positive integer N, and t
ID: 3844482 • 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
The code in JAVA that should be completed:
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String s;
while ((s = in.readLine()) != null) {
System.out.println(s);
}
}
}
Explanation / Answer
import java.io.*;
import java.util.*;
public class test {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String s;
String res="";
int n,p,q,i=0,x,y;
int[] dec;
while ((s = in.readLine()) != null) {
String[] strs = s.trim().split("\s+");
n = Integer.parseInt(strs[0]);
p = Integer.parseInt(strs[1]);
q = Integer.parseInt(strs[2]);
int dup = n;
for(i=1;i<=n;i++){
x=0;y=0;
if(i%p == 0 || i%q == 0){
res= "OUT";
x=1;
}
if ( check(i,p,q) == true){
res = "THINK";
y=1;
}
if (x==1 && y==1){
System.out.print("OUTTHINK");
System.out.print(",");
continue;
}
if(x==1 || y==1){
System.out.print(res);
System.out.print(",");
continue;
}
else{
System.out.print(i);
System.out.print(",");
continue;
}
}
}
}
static boolean check(int a,int b,int c){
int[] dec = new int[100];
int i=0,j;
while (a!=0){
dec[i]=a%10;
a=a/10;
i++;
}
for(j=0;j<i;j++){
if( dec[j]==b || dec[j]==c){
return true;
}
}
return false;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.