BUPT复试专题—寻找i*j=m的个数(2016)

题目描述

3*3的矩阵内容。

1 2 3

2 4 6

3 6 9

即a[i][j](1<=i<=n,1<=j<=n)=i*j。

问一个这样n*n的矩阵里面,里面m出现的次数。

 

例如n为3,m为6.

那么出现的次数就是2

输入

输入正整数N,表示N例测试(N<=20)。接着输入n(n<=10^5),m(<=10^9)。

输出

对每组输入数据,输出m出现的次数。

样例输入

2
3 6
3 3

样例输出

2
2

来源

2016机考C题 

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<map>
#include<math.h>
#include<string.h>
#include<queue>
#include<vector>
#include<set>
#define LL long long
#define exp 1e-9
#define MAXN 1000010
 
using namespace std;
 
int main()
{
//  freopen("D:\in.txt","r",stdin);
    int i,j,k,n,m,l,t,ans;
    scanf("%d",&t);
    for(i=1;i<=t;i++)
    {
        scanf("%d%d",&n,&m);
        ans=0;
        k=(int)sqrt(m);
        for(j=1;j<=k&&j<=n;j++)
        {
            if(m%j==0)
            {
                l=m/j;
                if(l<=n)
                {
                    if(l!=j)
                        ans+=2;
                    else
                    {
                        ans+=1;
                    }
//                  printf("j=%d l=%d
",j,l);
                }
            }
        }
        printf("%d
",ans);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/dzzy/p/8484672.html