最多约数问题

求区间a到b(正整数)之间约数个数最多的数,并输出他的约数。

约数是能整除x的正整数。

 1 #include<stdio.h>
 2 #include<stdafx.h>
 3 #include<iostream>
 4 #include<string>
 5 using namespace std;
 6 
 7 int div(int a)
 8 {
 9     int count,i,k;
10         count=0;
11     for(i=1;i<=a;i++){
12         k=a%i;
13         if(k==0)
14             count++;
15     }
16     return count;
17 }
18 
19 int main(){
20     freopen("D://input.txt","r",stdin);
21     freopen("D://output.txt","w",stdout);
22 
23     int a,b;
24 
25     while(scanf("%d%d",&a,&b)==2){
26     int i,temp,max=0;
27     for(i=a;i<=b;i++){
28         temp=div(i);
29             if(temp>=max)
30                 max=temp;
31     }
32 
33     cout<<max<<endl;
34     }
35 
36     return 0;
37 }
View Code

PS:

 scanf("%d%d", &a, &b);
如果a和b都被成功读入,那么scanf的返回值就是2
如果只有a被成功读入,返回值为1
如果a和b都未被成功读入,返回值为0
如果遇到错误或遇到end of file,返回值为EOF。
此外写成2 == scanf()格式是一种编程风格~防止由于程序员手误写成"="

posted @ 2015-09-14 10:30  Wei_java  阅读(588)  评论(0编辑  收藏  举报