A. Xenia and Divisors codeforces-problem-342A

A. Xenia and Divisors
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Xenia the mathematician has a sequence consisting of n (n is divisible by 3) positive integers, each of them is at most 7. She wants to split the sequence into groups of three so that for each group of three a, b, c the following conditions held:

  • a < b < c;
  • a divides bb divides c.

Naturally, Xenia wants each element of the sequence to belong to exactly one group of three. Thus, if the required partition exists, then it has  groups of three.

Help Xenia, find the required partition or else say that it doesn't exist.

Input

The first line contains integer n (3 ≤ n ≤ 99999) — the number of elements in the sequence. The next line contains n positive integers, each of them is at most 7.

It is guaranteed that n is divisible by 3.

Output

If the required partition exists, print  groups of three. Print each group as values of the elements it contains. You should print values in increasing order. Separate the groups and integers in groups by whitespaces. If there are multiple solutions, you can print any of them.

If there is no solution, print -1.

Examples
input
6
1 1 1 2 2 2
output
-1
input
6
2 2 1 1 4 6
output
1 2 4
1 2 6

题意:

输入n个数字,分类 ,输出n/3组 a,b,c 形式数据,b%a==0,c%b==0,a!=b!=c.

若不能分类则输出-1。

想法:最大数字为7  只有三种组合可能  

1,2,6

1,2,4

1,3,6

N(1)=N(2)+N(3)

N(1)=N(4)+N(6)

N(136)=N(3)

N(146)=N(4)

N(126)=N(6)-N(3)=N(2)-N(4)

注意N(3)<=N(6),N(4)<=N(2)

#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
    int n;
    scanf("%d",&n);
    int term,save[7];
    memset(save,0,sizeof(save));
    int flag=1;
    while(n--)
    {
        scanf("%d",&term);
        save[term]++;
        if(term==5||term==7)flag=0;
    }
    if(flag&&save[1]==save[2]+save[3]&&save[1]==save[4]+save[6]&&save[6]>=save[3])
    {//注意save[6]>=save[3]的条件,可换为save[2]>=save[4] 避免 1 1 2 3 4 4
        for(int i=0;i<save[6]-save[3];i++)
            printf("1 2 6\n");
        for(int i=0;i<save[4];i++)
            printf("1 2 4\n");
        for(int i=0;i<save[3];i++)
            printf("1 3 6\n");
    }
    else printf("-1\n");
}
/*注意点:
在输入数据时考虑数据是否要被复用,若不需要复用,则可考虑用单个变量反复赋值来增加效率。*/



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值