codeforces#588 A.Dawid and Bags of Candies B.Ania and MinimizingTable

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;



int main() {
	vector<int> in_data(4,0);
	int i = 0;
	while (i<4) {
		cin >> in_data[i];
		i++;
	}
	sort(in_data.begin(),in_data.end());

	if ((in_data[0] + in_data[3]) == (in_data[1] + in_data[2]))
		cout << "YES";
	else if((in_data[0] + in_data[1] + in_data[3]) == (in_data[2]))
	    cout << "YES";
	else
		cout << "NO";
	return 0;
}

第一道AC,题意好理解,解也好写

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
// void call(string &num, long long pos, long long len, long long chance){
    
//     if((pos>len-1) || !chance)
//         return;
//     if(len == 1){
//         num[0] = '0';
//         return;
//     }
    
//     if(!pos){
//         if(num[pos] == '1')
//             call(num,pos+1,len,chance);
//         else{
//             num[pos] = '1';
//             call(num,pos+1,len,chance-1);
//         }
//     }else{
//         if(!num[pos])
//             call(num,pos+1,len,chance);
//         else{
//             num[pos] = '0';
//             call(num,pos+1,len,chance-1);
//         }
//     }
// }


// int main() {
//     string num;
//     long long N,K;
//     cin>>N>>K;
//     cin>>num;
//     call(num,0,N,K);
//     cout<<num;
// 	return 0;
// }

int main()
{
    // int n,m,k;
    // string str;
    // while(cin>>n>>k)
    // {
    //     cin>>str;
    //     if(!k)
    //         cout<<str<<endl;
    //     else if(n==1)
    //         cout<<0<<endl;
    //     else
    //     {
    //         if(str[0]!='1')
    //             {str[0] = '1';k--;}
            
    //         for(int i=1; i<str.size() && k; i++)
    //         {
    //             if(str[i]!='0')
    //             {str[i]='0';k--;}
 
    //         }
    //         cout<<str<<endl;
    //     }
    // }
    int n,m,k;
    string str;
    cin>>n>>k;
    cin>>str;
    if(!k)
      cout<<str<<endl;
    else if(n==1)
      cout<<0<<endl;
    else{
        if(str[0]!='1')
            {str[0] = '1';k--;}
        if(k>str.size())
            string mid_str(str.size(),'0');
        else
            string mid_str(k,'0');
        string tail_str(str.substr(k+1));
        cout<<("1"+mid_str+tail_str)<<endl;
    }
    terurn 0;
}

第二道有点说法,按照要求模拟迭代过程AC;

但是想要简化一下:

1.先写了递归,好理解,但是数据长度太大,估计爆栈,所以不要用递归

2.用迭代的话,时间复杂度有点高。然后就找列一下规律,实际上就是原数据的后m-k位是不需要修改的,那就直接拼接两个字符串就完事了。但是很奇葩,死活过不了第五个用例,很想知道BUG在哪

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值