Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

3. Due to E. W. Dijkstra] Consider decimal numerals containing only the digits 1

ID: 3590794 • Letter: 3

Question

3. Due to E. W. Dijkstra] Consider decimal numerals containing only the digits 1-3. A numeral is considered "good" if no two adjacent non-empty substrings of it are equal; otherwise it is "bad." Hence, the numerals ‘1','12, and 11213, are good, while '11, 32121, and 1121321312, are bad. You are to write a program that, given n 0, finds the smallest good n-digit numeral. The input consists of a sequence of positive integers. For each of these integers, n, the output is to contain a line of the form The smallest good numeral of length n is s. where s is the answer. For example, In The snallest good numeral of length 1 is 1. The smallest good numeral of length 4 is 1213. The smallest good numeral of length 7 is 1213121. The smallest good numeral of length 9 is 121312313. 1 4 7

Explanation / Answer

import java.util.Scanner;
class good
{
public static void main(String args[])
{
int i,n; //variable declaration
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number"); //accepting the length from user
n=sc.nextInt();
int a[]={1,2,1,3};
System.out.print("The smallest good numeral of length "+n+" is ");
for(i=0;i<n;i++)   
{
System.out.print(a[i]); //displaying the digits
if(i==3)
{
n=n-4;
i=-1;
}
  
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote