Login Register






Challenge #1: From 1987 to 2013 filter_list
Author
Message
Challenge #1: From 1987 to 2013 #1
These are some old questions I found from last year's CCC, I'll post this year's questions if I can find the package I got from my teacher.

Problem S1: From 1987 to 2013
Problem Description
You might be surprised to know that 2013 is the first year since 1987 with distinct digits. The years
2014, 2015, 2016, 2017, 2018, 2019 each have distinct digits. 2012 does not have distinct digits,
since the digit 2 is repeated.

Given a year, what is the next year with distinct digits?

Input Specification
The input consists of one integer Y (0 <= Y <= 10000), representing the starting year.

Output Specification
The output will be the single integer D, which is the next year after Y with distinct digits.

Sample Input 1
1987

Output for Sample Input 1
2013

Sample Input 2
999

Output for Sample Input 2
1023

Reply

RE: Challenge #1: From 1987 to 2013 #2
Here's my quick submission:

Code:
#include <iostream> #include <algorithm> int next_distinct_year(int year) { int k[2] = {}; int digits[10] = {}; next_year: std::fill_n(digits, 10, 0); k[0] = ++year; do { k[1] = k[0] % 10; if (digits[k[1]]) goto next_year; digits[k[1]] = 1; k[0] /= 10; } while (k[0] > 0); return year; } int main() { std::cout << next_distinct_year(1987) << std::endl; std::cout << next_distinct_year(999) << std::endl; }

Not sure if I like my solution though. I try to avoid goto as it usually produces spaghetti code. But lots of Linux kernel developers like it because of it's simplicity and it's computationally less expensive than most loops given the fact that a comparison is not needed. An added boolean for a loop or some kind of conditional here though just adds to the overhead for no specific reason, so it's used here until the structure changes or I can brainstorm a better way, simply because it's the most optimized choice in this case. There's the odd time where I need to break out of a bunch of nested loops too, but that's one of the only other times I use it.

Reply

RE: Challenge #1: From 1987 to 2013 #3
I say the solution is fine. In the competition, you're simply graded as to whether you process all of the input files within 5 seconds (the online grader uses standard input). This is a simple Python solution, it's really short since Python has a built in function that helps do this quickly.

Code:
# CCC 2013 Senior 1: From 1987 to 2013 def distinct(y): s = str(y) for digit in s: if s.count(digit) > 1: return False return True file = open("s1.15.in", 'r') year = int(file.readline()) + 1 while not distinct(year): year = year + 1 print year

I might post my Java solution, but I want to rewrite it first Tongue

Reply

RE: Challenge #1: From 1987 to 2013 #4
I tried to avoid casting to a string to do this, but that, although making things perhaps less efficient, would've made things much easier too.

Reply