View Code of Problem 3910

#include<iostream>
#include<string>
using namespace std;
int main(){
	int T;
	cin>>T;
	while(T--){
		bool flag=false;
		long long n,k,end;
		string s;
		cin>>n;
		cin>>k;
		cin>>s;
		long long it;
		if(k==0){
			if(s[0]=='0'){
				if(s.find('1')!=string::npos){
				it = s.find('1');
				cout<<s.substr(it)<<endl;
				}
				else{
					cout<<0<<endl;
				}
			}
			else if(s[0]=='1'){
				s.erase(0,1);
				if(s.find('1')!=string::npos){
				it = s.find('1');
				cout<<'-'<<s.substr(it)<<endl;
			    }
			    else{
			    	cout<<0<<endl;
				}
			}
		}
		else if(k==1){
			if(s[0]=='1'){
				for(int i=n;i>=0;i--){
					if(s[i]=='1'){
						 end=i;
						 break;
					}
				}
				for(int i=end-1;i>=0;i--){
						if(s[i]=='1'){
							s[i]='0';
						}
						else if(s[i]=='0'){
							s[i]='1';
						}
					}
				it = s.find('1');
				cout<<'-'<<s.substr(it)<<endl;
			}
			else if(s[0]=='0'){
				if(s.find('1')!=string::npos){
				it=s.find('1');
				cout<<s.substr(it)<<endl;
			}
				else{
					cout<<0<<endl;
				}
			}
		}
}
}

Double click to view unformatted code.


Back to problem 3910