- 注册时间
- 2016-4-18
- 最后登录
- 1970-1-1
- 威望
- 星
- 金币
- 枚
- 贡献
- 分
- 经验
- 点
- 鲜花
- 朵
- 魅力
- 点
- 上传
- 次
- 下载
- 次
- 积分
- 415
- 在线时间
- 小时
|
发表于 2017-1-14 15:03:43
|
显示全部楼层
本帖最后由 happysxyf 于 2017-1-14 15:45 编辑
基本实现,只要允许使用while循环和数组,下面代码就成立。不知题目中的纳秒是什么数量级,是C#中的纳秒,还是10的-9次方秒。我只能做到0.0000001秒出结果。并且全程没用任何乘除。
- /*COPYRIGHT@2016~2018 BY HAPPY*/
- #include <stdio.h>
- typedef unsigned long long uint64_t;
- static const int mang[64]={1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288, 1048576, 2097152, 4194304, 8388608, 16777216, 33554432, 67108864, 134217728, 268435456, 536870912, 73741824, 147483648, 294967296, 589934592, 179869184, 359738368, 719476736, 438953472, 877906944, 755813888, 511627776, 23255552, 46511104, 93022208, 186044416, 372088832, 744177664, 488355328, 976710656, 953421312, 906842624, 813685248, 627370496, 254740992, 509481984, 18963968, 37927936, 75855872, 151711744, 303423488, 606846976, 213693952, 427387904, 854775808};
- //uint64_t取余函数
- int Uint64ModBillion(uint64_t a)
- {
- int S=0, *p=(int*)mang;
- while(a){
- if(a-((a>>1)<<1)){
- S+=*p, S=(S>=1000000000)?S-=1000000000:S;
- }
- a>>=1, p++;
- }
- return S;
- }
- //主函数入口
- int main()
- {
- uint64_t a=8446744073709551616;
- printf("%d\n", Uint64ModBillion(a));
- return 0;
- }
复制代码
下面是返回bool类型的函数
- /*COPYRIGHT@2016~2018 BY HAPPY*/
- #include <stdio.h>
- #include <stdbool.h>
- typedef unsigned long long uint64_t;
- typedef unsigned long uint32_t;
- static const uint32_t mang[64]={1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288, 1048576, 2097152, 4194304, 8388608, 16777216, 33554432, 67108864, 134217728, 268435456, 536870912, 73741824, 147483648, 294967296, 589934592, 179869184, 359738368, 719476736, 438953472, 877906944, 755813888, 511627776, 23255552, 46511104, 93022208, 186044416, 372088832, 744177664, 488355328, 976710656, 953421312, 906842624, 813685248, 627370496, 254740992, 509481984, 18963968, 37927936, 75855872, 151711744, 303423488, 606846976, 213693952, 427387904, 854775808};
- //uint64_t取余函数
- bool Uint64ModBillion(uint64_t a)
- {
- uint32_t S=0, *p=(uint32_t*)mang;
- while(a){
- if(a-((a>>1)<<1)){
- S+=*p, S=(S>=1000000000)?S-=1000000000:S;
- };
- a>>=1, p++;
- }
- return (S<500000000)?true:false;
- }
- //主函数入口
- void main()
- {
- uint64_t a=8073709551616;
- Uint64ModBillion(a)?fputs("TRUE\n", stdout):fputs("FALSE\n", stdout);
- }
复制代码 |
|