English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Write a program that reads in three zip codes from standard input
and writes to standard output a single integer and nothing else.
The integer it prints out is the number of duplicates in the
sequence of zip codes. Thus, it will print out 0, 1 or 2:
0 is printed if each zip code is unique.
1 is printed if only two of the zip codes are the same.
2 is printed if all three are the same.
So, if the input to the program is
11210 11217 10003
then the output would be:
------------------------------...
0
------------------------------...
But, if the input to the program is
11210 11210 11210
then the output would be:
------------------------------...
2
------------------------------...


okay, this isn't working out for me, it always comes out as incorrect, whether i use string, char, or whatever. PLEASE help. (remember its C++)

2006-10-09 16:09:06 · 1 answers · asked by who cares 1 in Computers & Internet Programming & Design

1 answers

I don't know C++ so I did it in Perl as a challenge to myself. I hope it points you in the right direction. I'll leave comments on the line for you.

#Build the list of numbers using an array
@list = ("1121", "1210", "1110");
%seen = ();
#Go through the array to see if each element is unique
foreach $item (@list) {
push(@uniq, $item) unless $seen{$item}++;
}
#print the values we pulled just for the heck of it
print "@uniq\n";
#count the number of elements
$count = @uniq;
#Depending on the number of elements, we assing a value
switch: {
if ($count eq 1){$value=2};
if ($count eq 2){$value=1};
if ($count eq 3){$value=0};
}
print "The magic value is: $value\n";

2006-10-09 19:50:24 · answer #1 · answered by thepinky 3 · 0 0

fedest.com, questions and answers