Codeforces Round #231 A,B,C

A. Counting Sticks

这是一道简单的算数题,提交以后被黑了一次,因为忽略了一个条件就是A,B,C不论什么时候都要大于0.

代码如下:

#include<iostream>
#include<stdio.h>
#include<cstring>
using namespace std;
int main()
{
    
    char ch[400];
    while(cin>>ch)
    {
    int l1=0,l2=0,l3=0,l;
    l=strlen(ch);
    int i=0;
        while(ch[i]!='+')
        {l1++;i++;}
        i++;
        while(ch[i]!='=')
        {l2++;i++;}
        i++;
        while(ch[i]!='\0')
        {l3++;i++;}
    if((l1+l2+2)==l3)
    {
        l1++;
        l3--;
    }
    if((l1+l2-2)==l3)
    {
        if(l1>1)
        l1--;
        else l2--;
        l3++;
    }
    if((l1+l2)==l3)
    {
        for(int i=0;i<l1;i++)
        cout<<'|';
        cout<<'+';
        for(int i=0;i<l2;i++)
        cout<<'|';
        cout<<'=';
        for(int i=0;i<l3;i++)
        cout<<'|';
        cout<<endl;
    }
    else cout<<"Impossible"<<endl;
    }
}

B. Very Beautiful Number

这道题是大数据运算,是我队友写出来的,我就直接抄袭了一下。。思路就是把个位数从1到9进行枚举,确定了个位数以后前面不论多少位都可以确定。代码如下:

#include<iostream>
using namespace std;
int c[1000010];
int main()
{
    int p,x;
    while(cin>>p>>x)
    {
        int a,m=0,i,j;
        for(i=1;i<=9;i++)
        {
            c[0]=i;
            a=0;
            for(j=1;j<p;j++)
            {
                c[j]=(c[j-1]*x+a)%10;
                a=(c[j-1]*x+a)/10;
                
            }
            if(c[p-1]*x+a==c[0]&&c[p-1]!=0)
            {
                m=1;
                break;
            }
            
        }
        if(m==1)
        {
            for(i=p-1;i>=0;i--)
                cout<<c[i];
                cout<<endl;
        }
        else cout<<"Impossible"<<endl;
    }
}

C. Dominoes

第三题是在赛后写出来的,输入以后因为要重新排序,所以他输入的顺序并不重要,将输入的数据存放在优先队列中,在给矩阵赋值的时候,按照一张牌左右两边总和的大小开始出队列,保证每行赋值完成时,任意两列之间差值不超过1。如果有地方不均衡,那么在下一列赋值时,这个地方先行赋值。用rec数组保存赋值的顺序。

#include<iostream>
#include<algorithm>
#include<queue>
#include<cstring>
using namespace std;

typedef struct card{
	int l,r;
	int val;
	bool operator < (const card &a)const {
	return val<a.val;
	}
} card;

card cnt[1005];
priority_queue <card> ca;
int rec[1005][1005];
card show[1005];

int main(){
//	freopen("data.txt","r",stdin);
	ios::sync_with_stdio(false);
	int n,m;
	cin>>n>>m;
	int sum=0;
	for(int i=0;i<n;++i){
		for(int t=0;t<m;++t){
			card c;
			int tmp;
			cin>>tmp;
			c.l=tmp/10;
			if(c.l==1)sum++;
			c.r=tmp-c.l*10;
			if(c.r==1)sum++;
			c.val=c.l+c.r;
			ca.push(c);
		}
	}
	memset(cnt,0,sizeof(cnt));
	for(int i=0;i<n;++i){
		for(int t=0;t<m;++t){
			rec[i][t]=t;
		}
		int tot=0;
		for(int t=0;t<m;++t){
			int pos;
			if(i==0)pos=t;
			else pos=rec[i-1][t];
			card c=ca.top();
			ca.pop();
			if(c.val==0){show[pos].l=0;show[pos].r=0;}
			else if(c.val==2){show[pos].l=1;show[pos].r=1;cnt[pos].l++;cnt[pos].r++;}
			else if(c.val==1){
				if(cnt[pos].l==cnt[pos].r){
					show[pos].l=1;show[pos].r=0;
					cnt[pos].l++;
					int tmp=rec[i][tot];
					rec[i][tot]=rec[i][pos];
					rec[i][pos]=tmp;
				//	cout<<pos<<' '<<tot<<' '<<rec[i][pos]<<' '<<rec[i][tot]<<endl; 
					tot++;
				}	
				else if(cnt[pos].l<cnt[pos].r){
					show[pos].l=1;show[pos].r=0;
					cnt[pos].l++;
				}
				else {
					show[pos].r=1;show[pos].l=0;
					cnt[pos].r++;
				}	
			}
		}
	//	for(int t=0;t<m;++t){
	//		cout<<rec[i][t]<<' ';
	//	}
	//	cout<<endl;
		for(int t=0;t<m;++t){
			cout<<show[t].l<<show[t].r<<' ';
		}
		cout<<endl;
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值