NC79. 丑数
描述
示例1
输入:
7
输出:
8
C 解法, 执行用时: 2ms, 内存消耗: 336KB, 提交时间: 2020-09-08
/** * * @param index int整型 * @return int整型 */ int GetUglyNumber_Solution(int index ) { // write code here int ugly_count = 0; // recording how many Ugly Number has found int ugly_buf[index]; int index2, index3, index5; int last2, last3, last5, minium; if (index < 1) { return 0; } ugly_buf[0] = 1; index2 = 0; index3 = 0; index5 = 0; for (ugly_count = 1; ugly_count < index; ugly_count++) { last2 = ugly_buf[index2] * 2; last3 = ugly_buf[index3] * 3; last5 = ugly_buf[index5] * 5; if (last2 <= last3) // find the minium value of last2.3.5 { if (last2 <= last5) { minium = last2; } else { minium = last5; } } else { if (last3 <= last5) { minium = last3; } else { minium = last5; } } if (last2 == minium) { index2++; } if (last3 == minium) { index3++; } if (last5 == minium) { index5++; } ugly_buf[ugly_count] = minium; } return ugly_buf[index - 1]; }
C 解法, 执行用时: 2ms, 内存消耗: 348KB, 提交时间: 2021-02-18
/** * * @param index int整型 * @return int整型 */ int GetUglyNumber_Solution(int index ) { // write code here int array[index]; array[0] = 1; if(index<=0) return 0; int p2 =0,p3=0,p5=0,temp,temp2,temp3,temp5; for(int i = 1;i<index;i++){ //arr[i] = (((arr[p2]*2 < arr[p3]*3)?arr[p2]*2:arr[p3]*3)<arr[p5]*5)?((arr[p2]*2 < arr[p3]*3)?arr[p2]*2:arr[p3]*3):arr[p5]*5; temp2 = 2 * array[p2]; temp3 = 3 * array[p3]; temp5 = 5 * array[p5]; temp = (temp2 < temp3) ? temp2 : temp3; temp = (temp < temp5) ? temp : temp5; array[i] = temp; if(temp == temp2) p2++; if(temp == temp3) p3++; if(temp == temp5) p5++; } return array[index-1]; }
C 解法, 执行用时: 2ms, 内存消耗: 348KB, 提交时间: 2021-02-10
/** * * @param index int整型 * @return int整型 */ int min(int a, int b, int c) { if(a <= b && a <= c) { return a; } else if(b <= a && b <= c) { return b; } else if(c <= a && c <= b) { return c; } else return 0; } int GetUglyNumber_Solution(int index ) { if(index <= 0) return 0; int a[100000000]; a[0] = 1; int p2 = 0, p3 = 0, p5 = 0, i; int j = 0; //a[1] = min(p2*2, p3*3, p5*5); for(i = 1; i <= index; i++) { a[i] = min(a[p2]*2, a[p3]*3, a[p5]*5); if(a[i]%2 == 0) p2++; if(a[i]%3 == 0) p3++; if(a[i]%5 == 0) p5++; } return a[index-1]; }
C 解法, 执行用时: 2ms, 内存消耗: 348KB, 提交时间: 2020-12-17
/** * * @param index int整型 * @return int整型 */ int GetUglyNumber_Solution(int index ) { // write code here int arr[index]; int i2 = 1, i3 = 1, i5 =1; arr[0] = 0; arr[1] = 1; int tmp, tmp2, tmp3, tmp5; for(int i=1; i<index; i++) { tmp2 = 2*arr[i2]; tmp3 = 3*arr[i3]; tmp5 = 5*arr[i5]; tmp = (tmp2<tmp3)?tmp2:tmp3; tmp = (tmp<tmp5)?tmp:tmp5; arr[i+1] = tmp; if(tmp == tmp2) i2++; if(tmp == tmp3) i3++; if(tmp == tmp5) i5++; } return arr[index]; }
C 解法, 执行用时: 2ms, 内存消耗: 352KB, 提交时间: 2021-03-14
/** * * @param index int整型 * @return int整型 */ #define min(a,b,c) a>b? b>c ? c:b :a>c? c:a int GetUglyNumber_Solution(int index ) { // write code here if(index < 7) return index; int ret[index]; ret[0] = 1; int po2=0,po3=0,po5=0; for(int i=1;i<index;++i){ ret[i] = min(ret[po2]*2,ret[po3]*3,ret[po5]*5); if(ret[i] == ret[po2]*2) po2++; if(ret[i] == ret[po3]*3) po3++; if(ret[i] == ret[po5]*5) po5++; } return ret[index-1]; }