【笔试】诺西二

1. 写一个strcopy函数

#include "stdafx.h"
#include <string.h>
#include <malloc.h>
#include <assert.h>

char *strcopy(char *strDest, const char *strSrc)
{
	assert(strDest != NULL && strSrc != NULL);

	char *address = strDest;
	while ((*address++ = *strSrc++) != '\0')
	{
		;
	}
	return address;

}

2.   A piece of DSP program want to do the following tasks:

1) Use funcA() to process 1M bytes data A and genrate 2M bytes data B.
2) Use funcB() to process 1M bytes data A and genrate 3M bytes data C.
3) Move the data B genrated in 1) external memory.

Assume that Task1 needs 50ns and Task2 needs 60ns. QDMA is used in Task3 and the time needed for QDMA configuation and startup is 0.1ns. Data moving for 2M bytes needs 40ns.

In order to finish the above tasks, whats the minimum time needed for this DSP program____.

任务1和2可以并行处理,一共耗时(50,60)ns,

QDMA启动可以在任务1,2执行时同步

任务1和3只能并行处理,40ns

一共就是min(50,60)+40 = 90ns?


3.Ugly numbers are numbers whose only prime factors are 2,3or 5.The sequence 1,2,3,4,5,6,8,9,10,12,15,…shows the first 11ugly numbers. By convention,1 is included.
Write a time efficient program to find and print the 1500’th ugly number.

http://blog.csdn.net/ojshilu/article/details/17653775

思路:题目要求丑数的因子只能是2、3、5。要我找出从1开始的N个丑数。

方法一:直观的笨办法是,从1开始逐个每个数,看除了2、3、5外还有没有别的因子。效率极低。对于每个自然数,我都要循环除2,循环除3,循环除4,然后看剩下的数是否为1。

方法二:构造丑数表法。由于丑数只是有2、3、5相乘构造出来的,因为我们来分析一下规律。

第一个丑数1,其因子是0个2,0个3,0个5;

第二个丑数2,其因子是1个2,0个3,0个5; 它是在第一个丑数1基础上乘以2得到的;

第三个丑数3,其因子是0个2,1个3,0个5; 它是在第一个丑数1基础上乘以3得到的;

第四个丑数4,其因子是2个2,0个3,0个5; 它是在第二个丑数2基础上乘以2得到的;

第五个丑数5,其因子是0个2,0个3,1个5; 它是在第一个丑数1基础上乘以5得到的;

第六个丑数6,其因子是1个2,1个3,0个5; 它是在第二个丑数2基础上乘以3得到的,同时也是第三个丑数3基础上乘以2得到的;

第七个丑数8,其因子是3个2,0个3,0个5; 它是在第四个丑数4基础上乘以2得到的;

第八个丑数9,其因子是0个2,2个3,0个5; 它是在第三个丑数3基础上乘以3得到的;

第八个丑数10,其因子是1个2,0个3,1个5; 它是在第二个丑数3基础上乘以5得到的;

………………

    观察后会发现,每一个丑数都一定正好3次作为之后某丑数的基础,尽管顺序会有所不同。同一个丑数i,会有一次乘2的机会,一次乘3的机会,一次乘5的机会。每个机会一旦用过,就不能再用了。因此,我们设定三个游标i2,i3,i5,用以保存当前乘2机会、乘3机会、乘5机会的使用情况,i2表示当前可以有乘2机会的是第i2个丑数,i3表示当前可以有乘机会的是第i3个丑数,i5表示当前可以有乘5机会的是第i5个丑数。

    我首先设置第一个丑数为1,之后,在递推选择下一个丑数的过程中,每次我都会从三个游标所表示的机会如果使用之后所得到的数中选择最小的数,把它任命为下一个丑数,同时使用掉这个机会,即把对应的游标加1,其他的没被选到的机会留作下轮参选。(尤其注意,像6这样的丑数,就同时用掉了2的乘3机会和3的乘2机会)。


代码如下:

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include <stdio.h>  
  2. const int N = 1501;  
  3.   
  4. int getmin(int a, int b, int c)  
  5. {  
  6.     if(a <= b)  
  7.     {  
  8.         if(a <= c)  
  9.             return a;  
  10.         else  
  11.             return c;  
  12.     }  
  13.     else  
  14.     {  
  15.         if(b <= c)  
  16.             return b;  
  17.         else  
  18.             return c;  
  19.     }  
  20. }  
  21.   
  22.   
  23. int find_ugly(int n)  
  24. {  
  25.     int i2=1,i3=1,i5=1;  
  26.     int ugly[N];  
  27.     ugly[1] = 1; //ugly[0]  not used  
  28.     int i=2;  
  29.     int min;  
  30.     while(i <= n)  
  31.     {  
  32.         min = getmin(ugly[i2]*2, ugly[i3]*3, ugly[i5]*5);  
  33.         ugly[i] = min;  
  34.         if(min == ugly[i2]*2)  
  35.             i2++;  
  36.         if(min == ugly[i3]*3)  
  37.             i3++;  
  38.         if(min == ugly[i5]*5)  
  39.             i5++;  
  40.         i++;  
  41.     }  
  42.     return ugly[n];  
  43. }  
  44.   
  45.   
  46. int main()  
  47. {  
  48.     int n;  
  49.     while(scanf("%d",&n)!=EOF)  
  50.     {  
  51.         printf("%d\n", find_ugly(n));  
  52.     }  
  53.     return 0;  
  54. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值