5464: Star Arrangements

题目描述

The recent vote in Puerto Rico favoring United States statehood has made flag makers very excited. An updated flag with 51 stars rather than the current one with 50 would cause a huge jump in U.S. flag sales. The current pattern for 50 stars is five rows of 6 stars, interlaced with four offset rows of 5 stars. The rows alternate until all stars are represented.
* * * * * *
* * * * *
* * * * * *
* * * * *
* * * * * *
* * * * *
* * * * * *
* * * * *
* * * * * *
This pattern has the property that adjacent rows differ by no more than one star. We represent this star arrangement compactly by the number of stars in the first two rows: 6,5.
A 51-star flag that has the same property can have three rows of 9 stars, interlaced with three rows of 8 stars (with a compact representation of 9,8). Conversely, if a state were to leave the union, one appealing representation would be seven rows of seven stars (7,7).
A flag pattern is visually appealing if it satisfies the following conditions:
• Every other row has the same number of stars.
• Adjacent rows differ by no more than one star.
• The first row cannot have fewer stars than the second row.
Your team sees beyond the short-term change to 51 for the U.S. flag. You want to corner the market on flags for any union of three or more states. Given the number S of stars to draw on a flag, find all possible visually appealing flag patterns.

 

输入

The input consists of a single line containing the integer S (3 ≤ S ≤ 32,767).

 

输出

On the first line, print S, followed by a colon. Then, for each visually appealing flag of S stars,print its compact representation, one per line.
This list of compact representations should be printed in increasing order of the number of stars in the first row; if there are ties, print them in order of the number of stars in the second row. The cases 1-by-S and S-by-1 are trivial, so do not print those arrangements.
The compact representations must be printed in the form “x,y”, with exactly one comma between x and y and no other characters.

 

题目大意:输出能够组成s颗星星的所有的方案(只需要输出第一行和第二行的星星数就行)  。前提是  需要满足下列条件:

                 1. 星星的树木隔行相等

                 2.相邻两行星星  树木最多差一

                 3.第一行星星数要多于第二行    

               此外 还需满足    至少有两行星星,不能每行之有一颗星星。

思路:假设 第一行星星有n颗  则分以下三种情况讨论;  

          1.k*n=s   每一行都相等时 ,k表示有k行,此处的k需满足大于1

         2.k*(n+n-1)=s   第一行比第二行多一颗,共有偶数行

         3.k*(n+n-1)+n=s  第一行比第二行多一颗,共有奇数行

        此外注意一下输出的顺序。

# include <iostream>
# include <cstdio>
#include<algorithm>
#include<string>
using namespace std;
struct node{
    int a;
    int b;
};
bool cmp(node x,node y){
    if(x.a==y.a){
        return x.b<y.b;
    }
    else return x.a<y.a;
}
int main(){
    int s;
    cin>>s;
    cout<<s<<':'<<endl;
    node q[1008611];
    int k=0;
    for(int i=2;i<=s/2;i++){
        if(s%i==0&&s/i!=1){
            q[k].a=i;
            q[k].b=i;
            k++;
        }
    }
    for(int i=2;(2*i-1)<=s;i++){
        if(s%(2*i-1)==0){
            q[k].a=i;
            q[k].b=i-1;
            k++;

        }
    }
    for(int i=2;(2*i-1)<=(s-i);i++){
        if((s-i)%(2*i-1)==0){
            q[k].a=i;
            q[k].b=i-1;
            k++;
        }
    }
    sort(q,q+k,cmp);
    for(int i=0;i<k;i++){
        cout<<q[i].a<<','<<q[i].b<<endl;
    }
    return 0;
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值