POJ 1023 The expression of the fun number System integer

Source: Internet
Author: User
Tags printf

Test instructions

Give an integer n to see if it can be represented by a 01 string of K-bits, which is similar to the binary representation of an integer, except that the positive or negative of each representation is given by an input string s.

Analysis:

Understand that the only arbitrary binary representation of integers is determined by an integer pair of arbitrary number to seek the remainder of the only axiom derived from this problem is not difficult, the details of this problem is to pay attention to the negative balance: -5/2=-2,-5%2=-1.

Code:

POJ 1023
//sep9
#include <iostream>
#include <stack>
using namespace std;
int k;
Char s[80];
Long long N;

int main ()
{
	int cases;
	scanf ("%d", &cases);
	while (cases--) {
		scanf ("%d%s%lld", &k,s,&n);
		Stack<int> ans;	
		int i;
		if (n==0) {
			puts ("0");
			Continue;
		}
		for (i=k-1;i>=0;--i) {
			if (n%2==0) {
				ans.push (0);
				n/=2;
				Continue;
			}
			if (n%2==1) {
				ans.push (1);
				if (s[i]== ' P ') {
					n=n/2;
				} else{
					n=n/2+1;
				}
				Continue;
			}
			if (n%2==-1) {
				ans.push (1);
				if (s[i]== ' P ') {
					n=n/2-1;
				} else{
					n= (n+1)/2;
		}} if (! ( i==-1&&n==0))
			puts ("impossible");
		else{
			while (!ans.empty ()) {
				printf ("%d", ans.top ());
				Ans.pop ();
			}
			printf ("\ n");}
	}
	return 0;	
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.