Sandwich numbers

We define a sandwich number as a natural number nn with only two different digits dd and ee, forming a sequence de1e2ekdde_1e_2\cdots e_kd. That is, the digit dd is the first and last digit of nn (it’s the bread), and the digit ee is repeated k1k \geq 1 times in between the two dd digits (it’s the filling). For example, 121 is a sandwich number with d=1d = 1, e=2e = 2, and k=1k = 1. And 4004 is a sandwich number with d=4d = 4, e=0e = 0 and k=2k = 2.

More examples of sandwich numbers: 7227, 41114, 966669, 10001, and 535.
Examples of numbers that are not sandwich numbers: 9, 12, 113311, 7878, 1234, 9991, 1000.

Implement a function is_sandwich that receives a natural number and returns true if it is a sandwich number and false otherwise.

The function header should be:

/**
 * @pre  n >= 0
 * @post returns true if n is a sandwich number, false otherwise
 */
bool is_sandwich(int n);

Observation

You only need to submit the requested function; the main program will be ignored.

Problem information

Author: PRO1

Generation: 2026-01-25T13:08:44.766Z

© Jutge.org, 2006–2026.
https://jutge.org