整除个数

来源:互联网 发布:淘宝香云纱 编辑:程序博客网 时间:2024/06/06 05:34

整除个数

时间限制:3000 ms  |  内存限制:65535 KB
难度:1
描述
1、2、3… …n这n(0<n<=1000000000)个数中有多少个数可以被正整数b整除。
输入
输入包含多组数据
每组数据占一行,每行给出两个正整数n、b。
输出
输出每组数据相应的结果。
样例输入
2 15 310 4
样例输出
212
来源
自编
上传者

mix_math



问题链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=399


非正确代码1:

#include <iostream>  #include <stdio.h>   #include <string.h>  #include <math.h>  #include <vector>  #include <queue>  #include <stack>  #include <map>  #include <string>  #include <algorithm>  #include <iomanip>#define MAX 1000000000using namespace std;/* run this program using the console pauser or add your own getch, system("pause") or input loop */bool mark[MAX];int main(int argc, char** argv) {/*freopen("file/input.txt","r",stdin);freopen("file/output.txt","w",stdout);*/int n,b;while(scanf("%d%d",&n,&b)!=EOF){memset(mark,false,sizeof(mark));int ans=0;for(int i=1;i<=n;i++){if(mark[i] == true){continue;}if(i % b == 0){  //找到一个可以整除的i,则将之后i的倍数的数全部标记为可以整数,//并累加的结果中 mark[i]=true;ans++;for(int j=2*i;j>0 && j<=n;j+=i){mark[j]=true;ans++;}}}printf("%d\n",ans);}return 0;}
错误:MemoryLimitExceeded

在标记时候申请了很大一块空间进行标记数是否被检查过。


改进代码:

#include <iostream>  #include <stdio.h>   #include <string.h>  #include <math.h>  #include <vector>  #include <queue>  #include <stack>  #include <map>  #include <string>  #include <algorithm>  #include <iomanip>#define MAX 1000000000using namespace std;/* run this program using the console pauser or add your own getch, system("pause") or input loop */int main(int argc, char** argv) {/*freopen("file/input.txt","r",stdin);freopen("file/output.txt","w",stdout);*/int n,b;while(scanf("%d%d",&n,&b)!=EOF){int ans=0;for(int i=b;i<=n;i+=b){ans++;}printf("%d\n",ans);//printf("%d\n",n/b);}return 0;}

错误:TimeLimitExceeded

改进第一个超出内存错误时发现在遍历时,可以整除的一定是b的倍数。因 此直接对n以内的b的倍数进行统计即可。

但是却超时了。 忽然发现其实直接n/b就是n内可以整除b的个数。为什么呢?希望知道的能解惑一下,非常感谢!q q 1 2 2 51 5 4 2 9 6

#include <iostream>  #include <stdio.h>   #include <string.h>  #include <math.h>  #include <vector>  #include <queue>  #include <stack>  #include <map>  #include <string>  #include <algorithm>  #include <iomanip>#define MAX 1000000000using namespace std;/* run this program using the console pauser or add your own getch, system("pause") or input loop */int main(int argc, char** argv) {/*freopen("file/input.txt","r",stdin);freopen("file/output.txt","w",stdout);*/int n,b;while(scanf("%d%d",&n,&b)!=EOF){//int ans=0;//for(int i=b;i<=n;i+=b){//ans++;//}//printf("%d\n",ans);printf("%d\n",n/b);}return 0;}


原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 诛仙账号被盗了怎么办 诛仙3账号忘了怎么办 伤害世界车翻了怎么办 诛仙3没用的护符怎么办 灵跃武器选错了怎么办 qq账号不记得了怎么办 突然不来月经了怎么办 闭经2个月了怎么办 闭经怎么办我才19岁 闭经3 4个月怎么办 月经来了十多天了还不走怎么办 3个月没有来月经怎么办 48岁突然没月经怎么办 月经来了疼的厉害怎么办 月经肚子疼怎么办快速止疼 来月经疼的厉害怎么办 22岁闭经6个月怎么办 月经推迟16天了怎么办 月经半年没来了怎么办 月经推迟两个月了还不来怎么办 四个月月经不来怎么办 快两个月没来月经了怎么办 月经停了两个月怎么办 别人诬告我我该怎么办 有人造谣我我该怎么办 宝宝晚上不睡觉哭闹怎么办 婴儿晚上不睡觉哭闹怎么办 2月宝宝排便困难怎么办 3岁宝宝老是哭闹怎么办 2岁了囟门闭合晚怎么办 宝宝卤门闭合慢怎么办 手经常碰水脱皮怎么办 迅雷文件已移除怎么办 手机不读sd卡怎么办 g买卖卖错账号怎么办 森林被野人拖走怎么办 我的世界没有羊怎么办 黑魂3杀死铁匠后怎么办 幻境7下8走错了怎么办 换了手机号微信怎么办 微信游戏没了怎么办