这题不要想任何骚操作…………………………
直接三重就行………………
using namespace std;
struct Node{
int x,y,z;
}stu[1000001];
bool cmp(Node a,Node b)
{
return a.x*10000+a.y*100+a.z<b.x*10000+b.y*100+b.z;
}
int main()
{
int t,i;cin>>t;
for(i=0;i<t;i++){
int k,n;cin>>n>>k;
int j,index=0;
if(k-n<0||2*n-k<0){
cout<<"case #"<<i<<":\n";
cout<<-1<<endl;
continue;
}
for(j=k-2*n;j<=(k-n)/2;j++){
if(j>=0&&k-n-2*j>=0&&2*n-k+j>=0){
stu[index].x=j;
stu[index].y=k-n-2*j;
stu[index++].z=2*n-k+j;
}
}
sort(stu,stu+index,cmp);
cout<<"case #"<<i<<":\n";
for(j=0;j<index;j++){
cout<<stu[j].x<<" "<<stu[j].y<<" "<<stu[j].z<<endl;
}
}
}
分享一个过了一周才弄出来的智障代码
_(:зゝ∠)_总是WA要哭了
然后发现是因为没有输出只有男人或者女人或者小孩的情况。。
int cmp1(const void a,const void b)
{ return (int)a-(int)b;}
void solve()
{
int N=0;long long int K=0;
scanf(“%d %I64d”,&N,&K);
int man;int woman;int children;int count=233;
int a[10000]={0},b[10000]={0},c[10000]={0};long long int sum[10000]={0};int i=0;int j;
for (man=0;man<=N;man++)
{
for (woman=0;woman<=N;woman++)
{
for (children=0;children<=N;children++)
{
if((3*man+2*woman+children==K)&&(man+woman+children==N))
{
a[i]=man;b[i]=woman;c[i]=children;count=1;i++;
} }}
}
for (j=0;j<i;j++)
{sum[j]=a[j]10000+b[j]100+c[j];}
qsort(sum,i,sizeof(sum[0]),cmp1);
for (j=0;j<i;j++)
{
printf("%d %d %d\n",a[j],b[j],c[j]);
}
if (N==0&&K==0) {printf(“0 0 0\n”);}
else if (count==233) {printf(“-1\n”);}
memset(a,0,sizeof(a));
}
int main()
{
int i,t;
scanf(“%d”,&t);
for (i=0;i<t;i++) {printf(“case #%d:\n”,i); solve(); }
return 0;
}
两个方程三个未知数 可以用一个变量来表示另外两个变量 然后判断解的情况即可
#include <bits/stdc++.h>
using namespace std;
int T, cnt, m, w, c;
struct data
{
int m;
int w;
int c;
int ans;
}d[1000];
bool cmp(data x, data y)
{
return x.ans<y.ans;
}
void solve(int n, int k)
{
int max_=(3*n-k)/2, min_=2*n-k;
int l=0;
if(max_<0||max_<min_) cout<<"-1"<<endl;
else if(min_<0){
for(c=0;c<=max_;++c){
d[l].m=k-2*n+c;
d[l].w=3*n-k-2*c;
d[l].c=c;
d[l].ans=10000*m+100*w+c;
l++;
}
}
else {
for(c=min_;c<=max_;++c){
d[l].m=k-2*n+c;
d[l].w=3*n-k-2*c;
d[l].c=c;
d[l].ans=10000*m+100*w+c;
l++;
}
}
sort(d,d+l,cmp);
for(int i=0;i<l;++i)
printf("%d %d %d\n",d[i].m, d[i].w, d[i].c);
}
int main()
{
cin>>T;
while(T--){
int n, k;
cin>>n>>k;
printf("case #%d:\n",cnt++);
solve(n,k);
}
return 0;
}
int main()
{
int T,Ti;
scanf(“%d”,&T);
for(Ti=0; Ti<T; Ti++)
{
int N,flag=0;
long long K,m,w,c;
scanf(“%d%lld”,&N,&K);
printf(“case #%d:\n”,Ti);
for(m=0; m<=N; m++)
for(w=0; w<=N; w++)
for(c=0; c<=N; c++)
if(m+w+c==N&&3m+2w+c==K)
{
printf(“%lld %lld %lld\n”,m,w,c);
flag=1;
}
if(flag==0)
printf(“-1\n”);
}
return 0;
}
#include <iostream>
using namespace std;
struct distribution
{
int m;
int w;
int c;
};
int cmp(const void *a, const void *b)
{
distribution *p1 = (distribution *)a;
distribution *p2 = (distribution *)b;
return (p1->m * 10000 + p1->w * 100 + p1->c) - (p2->m * 10000 + p2->w * 100 + p2->c);
}
int main()
{
int t;
cin >> t;
for (int z = 0; z < t; z++)
{
int n, k;
cin >> n >> k;
distribution list[1000];
int m, w, c;
int cnt = 0;
for (m = 0; (m <= k / 3) && m <= n; m++)
{
for (w = 0; (w <= k / 2) && (m + w <= n); w++)
{
for (c = 0; (c <= k) && (m + w + c <= n); c++)
{
if ((3 * m + 2 * w + c == k) && (m + w + c == n))
{
list[cnt].m = m;
list[cnt].w = w;
list[cnt].c = c;
cnt++;
}
}
}
}
cout << "case #" << z << ":" << endl;
if (cnt == 0)
{
cout << "-1" << endl;
continue;
}
qsort(list, cnt, sizeof(distribution), cmp);
for (auto i = 0; i < cnt; i++)
{
cout << list[i].m << ' ' << list[i].w << ' ' << list[i].c << endl;
}
}
return 0;
}
using namespace std;
int main()
{
int n;
cin >> n;
int i;
for(i = 0; i < n; i++){
int total, money;
cin >> total >> money;
int m, w, c;
int cnt = 0;
cout << “case #” << i << “: ” << endl;
for(m = 0; m <= money / 3; m++){
for(w = 0; w <= total - m && w <= money / 2; w++){
for(c = 0; c <= total - m - w && c <= money; c++){
if(m + w + c == total && 3m + 2w + c == money){
cnt++;
cout << m << ” ” << w << ” ” << c << endl;
}
}
}
}
if(cnt == 0){
cout << -1 << endl;
}
}
return 0;
}
屎山代码
大一写的代码..现在看确实很冗长..