boj problem 1337 简单题 分奇数偶数

该程序计算给定面积下不同地毯尺寸的组合数,条件是宽度和长度不能同时为偶数。对于每个输入的面积,程序通过遍历所有可能的奇数和偶数宽度,检查是否满足条件并避免重复计数。
摘要由CSDN通过智能技术生成

地址http://acm.scs.bupt.cn/onlinejudge/showproblem.php?problem_id=1337

Rugs Submit: 87    Accepted:43 Time Limit: 1000MS  Memory Limit: 65535K

Description
Rugs come in various sizes. In fact, we can find a rug with any integer width and length, except that no rugs have a distinct width and length that are both even integers. For example, we can find a 4*4 rug, but not a 2*4 rug. We want to know how many different choices we have for a given area.
Create a class RugSizes the contains a method rugCount that is given the desired area and returns the number of different ways in which we can choose a rug size that will cover that exact area. Do not count the same size twice -- a 6*9 rug and a 9*6 rug should be counted as one choice.


Input
A single integer T, the number of cases. Then the following T lines, each case per line, there is only one integer S, representing the area, which is less than 2*10^9.

Output
For each case, output a single integer N, the number of ways.

Sample Input

2
4
8


Sample Output

2
1


Source
Chiara

 

#include<iostream>
using namespace std;
int a[100000];
int main()
{
    int i,j,n,x,count,other;
    while(cin>>n)
    {
      
      for(i=0;i<n;i++)
      {
          count=0;
          x=1;
          cin>>a[i];
     
          while(x*x<=a[i])
          {
                other=a[i]%x;
             if(x%2==1&&other==0)
             {
                    count++;       
                       }    
             else if(x%2==0&&other==0)
             {
                  if((a[i]/x)%2==1)
                  {
                     count++;           
                                 }
                  else if(x*x==a[i])
                  {
                  count++;
                  break;
                  }
            
                  }    
                  x++;  
                          }   //while  
                a[i]=count;     
                      }  //for
                     
       for(i=0;i<n;i++)
       cout<<a[i]<<endl;                   
                 }//while
                 system("pause");
                 return 0;
    }

思路 按奇数偶数划分,如果x的平方大于了a[i],那么x之后的组合一定与X之前的某个组合重复,可以不再继续计算。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值