Question 49: Ugly Numbers
topic:
We call numbers that contain only factors 2, 3, and 5 ugly numbers. Find the 1500th ugly number in ascending order. For example, 6 and 8 are ugly numbers, but 14 is not because it contains the factor 7. By convention we treat 1 as the first ugly number.
Problem solving program:
#include<iostream> #include<stdio.h> using namespace std; // Solution 1: Determine whether each integer is a solution for drawing numbers one by one, which is intuitive but not efficient enough bool IsUgly(int number) { while( number % 2 == 0 ) number /= 2; while( number % 3 == 0 ) number /= 3; while( number % 5 == 0 ) number /= 5; return (number == 1) ? true:false; } int GetUglyNumber(int index) { if( index <= 0 ) { return 0; } int number = 0; int uglyFound = 0; while(uglyFound < index) { ++number; if( IsUgly(number) ) { ++uglyFound; } } return number; } // Solution 2: Create an array to save the ugly numbers that have been found, and use time for space // Get the smallest of the three numbers int Min(int num1,int num2,int num3) { int min = num1 < num2 ? num1:num2; min = min <num3? min: num3; return min; } int GetUglyNumber1(int index) { if(index <= 0) return 0; int *pUglyNumbers = new int[index]; for(int i=0;i<index;i++) { pUglyNumbers[i] = 0; } pUglyNumbers[0] = 1; int nextUglyIndex = 1; int *pMultiply2 = pUglyNumbers; int *pMultiply3 = pUglyNumbers; int *pMultiply5 = pUglyNumbers; while(nextUglyIndex < index) { int min = Min(*pMultiply2*2,*pMultiply3*3,*pMultiply5*5); pUglyNumbers[nextUglyIndex] = min; //printf("*pMultiply2 = %d,*pMultiply3 = %d,*pMultiply5 = %d\n",*pMultiply2,*pMultiply3,*pMultiply5); //printf("min = %d,pUglyNumbers[nextUglyIndex] = %d\n",min,pUglyNumbers[nextUglyIndex]); while( *pMultiply2 * 2 <= pUglyNumbers[nextUglyIndex] ) ++ pMultiply2; while( *pMultiply3*3 <= pUglyNumbers[nextUglyIndex] ) ++pMultiply3; while( *pMultiply5*5 <= pUglyNumbers[nextUglyIndex] ) ++pMultiply5; ++nextUglyIndex; } int ugly = pUglyNumbers[nextUglyIndex-1]; delete []pUglyNumbers; pUglyNumbers = NULL; return ugly; } // test case void test() { int index; printf("Please input index: \n"); scanf("%d",&index); printf("Solution 1:\n"); int ret = GetUglyNumber(index); if (ret) printf("%d ugly number is: %d\n",index,ret); else printf("Program error!\n"); printf("Solution 1:\n"); ret = GetUglyNumber1(index); if (ret) printf("%d ugly number is: %d\n",index,ret); else printf("Program error!\n"); } int main(void) { test(); return 0; }
Question 1: The first character in a string that occurs only once
Find the first character that occurs only once in a string. If the input "abaccdeff", the output 'b'.
Problem solving program:
#include<iostream> #include<stdio.h> using namespace std; char FirstNotRepeartingChar(char *str) { if( str == NULL ) return '\0'; const int tableSize = 256; unsigned int hashTable[tableSize]; for(int i=0;i<tableSize;i++) { hashTable[i] = 0; } char * pHashKey = str; while( *pHashKey != '\0' ) { printf("*pHashKey = %d\n",*pHashKey); hashTable[*pHashKey++]++; } pHashKey = str; while(*pHashKey != '\0') { if(hashTable[*pHashKey] == 1) return *pHashKey; pHashKey++; } return '\0'; } // test case void test() { char str[100] = ""; printf("Please enter a string:\n"); scanf("%s",str); char c = FirstNotRepeartingChar(str); if(c != '\0') printf("The first character that occurs only once: %c\n",c); else printf("Program error\n"); } int main(void) { test(); return 0; }
Topic 2: The first character in the character stream that appears only once
Please implement a function that finds the first one-occurrence character in a character stream. For example, when only the current two characters "go" are read from the character stream, the first character that appears only once is 'g'; when the first six characters "gegoole" are read from the character stream, the first character A character that occurs only once is 'l'.
Problem solving program:
#include<iostream> #include<limits> #include<stdio.h> using namespace std; class CharStatistics { public: CharStatistics():index(0) { for(int i=0;i<256;i++) { occurrence[i] = -1; } } void Insert(char ch) { if(occurrence[ch] == -1) occurrence[ch] = index; else if(occurrence[ch] >= 0) occurrence[ch] = -2; index++; } char FirstAppearOnce() { char ch = '\0'; int minIndex = numeric_limits<int>::max(); for(int i=0;i<256;i++) { if(occurrence[i] >=0 && occurrence[i] <minIndex) { ch = char(i); minIndex = occurrence[i]; } } return ch; } private: int index; int occurrence[256]; }; // test case void test() { CharStatistics chars; chars.Insert('g'); chars.Insert('o'); chars.Insert('o'); chars.Insert('g'); chars.Insert('l'); chars.Insert('e'); char c = chars.FirstAppearOnce(); if(c != '\0') { printf("The character that appears only once in the character stream is: %c\n",c); } else { printf("Program error\n"); } } int main(void) { test(); return 0; }