New number system

In China, number “4” is not good because it is has the same pronunciation as “death” in Chinese. So there is a new number system which we may make 4 disappear in the current decimal system like: 1,2,3,5,6,7…….13,15…….23,25…….33,35….39,50……..
Here 5 is 4 in decimal system, and 15 is 13.
So write a function, whose input is a positive number and output should be like this:
1 -> 1;
2 -> 2;
5 -> 4;
4 -> illegal;
15 -> 13;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int oldNum = 19;
            int illegalDigit = 4;
            Console.WriteLine(oldNum.ToString() + " maps to " + getNewNum(oldNum, illegalDigit) + " in the new number system");
        }

        public static int getNewNum(int oldNum, int illegalDigit)
        {
            int i = 1;
            for (int count = 1; count <= oldNum; i++, count++)
            {
                if (i.ToString().Contains(illegalDigit.ToString()))
                {
                    count--;
                    Console.WriteLine(i + " --> illegal");
                }
                else
                {
                    Console.WriteLine(i + " --> " + count);
                }
            }
            return (--i);
        }
    }
}

Output
=======
1 --> 1
2 --> 2
3 --> 3
4 --> illegal
5 --> 4
6 --> 5
7 --> 6
8 --> 7
9 --> 8
10 --> 9
11 --> 10
12 --> 11
13 --> 12
14 --> illegal
15 --> 13
16 --> 14
17 --> 15
18 --> 16
19 --> 17
20 --> 18
21 --> 19
19 maps to 21 in the new number system
Press any key to continue . . .
Advertisements
This entry was posted in Information Technology. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s