ATM (negative binary)

Source: Internet
Author: User
Document directory
  • Problem description
  • Input
  • Output
  • Sample Input
  • Sample output

 

Problem description
There are 30 ATMs in the lobby of a bank, numbered 0 .. 29. Each member customer can use these ATMs to extract 10 ^ 9 ducats (an early coin circulation unit) of the loan business, however, you must use these ATMs to complete the repayment within one week. This type of teller machine is very special, and each teller machine can only perform one simple action: for customers to extract a fixed amount of cash or accept the customer's fixed amount of repayment. The I-th teller machine can only provide loan services of 2 ^ I ducats, If I is an even number, and if I is an odd number, then, the I-th teller machine can only provide the repayment service of 2 ^ I ducats.
Now, if a customer wants to borrow a certain amount of money from these ATMs, but each of them can only be used once, what ATMs should he use?
Which of the following ATMs should he use during a week's repayment?
For example, if he wants to borrow 7 ducats, he will get 16 ducats from the No. 4 teller machine, 1 ducats from the No. 0 teller machine, and 8 ducats will be removed from the No. 3 teller machine, 2 ducats will be removed from the teller machine on the first day.
During a week's repayment, he will first pay off 8 ducats on the No. 3 teller machine, and then get 1 ducats on the No. 0 teller machine.
Please write a program to tell a customer which ATMs should be used and which ones should be used for repayment. Input the first behavior integer n (n <= 1000), indicating the number of customers. There are n rows below, each of which is a positive integer, indicating the amount of money each customer wants to borrow. Note: Each customer can borrow a maximum of 10 ^ 9 ducats. There are 2n lines in output, and 2i-1 lines indicate the ATMs used by the customer when I borrow money. 2I lines indicate the ATMs (1 <= I <= N) used by the customer when I repay the loan ), each line must be output from large to small according to the teller machine number. Each number is separated by a space.
Output Nie if the business cannot be closed. Sample Input
27385171980
Sample output
4 3 1 03 0NIE29 28 27 24 20 19 18 17 16 15 14 9 5 4 2

 

So-called negative binary: 3 = 4-2 + 1. 7 = 16-8 + 0*4-2 + 1.

This question is to input a number n to calculate the negative binary representation of N and-N respectively.

Take 3 For example, 3% 2 = 1, so 0 is 1, then-3/2 =-1,-1% 2 is 1, so 1 is 1, then it is worth noting that-1 cannot be processed as it was just now, so it will not get the error result with 3 digits being 0.

It should be (-1*1 + 1)/2.

-N is the same.

 

#include<cstdio>#include<iostream>using namespace std;int main(){int t;int i;int num[40];int n,temp;cin>>t;while(t--){cin>>n;temp=n;for(i=0;temp!=0;i++){num[i]=temp%2;if(num[i]<0)num[i]=-num[i];if(temp<0&&temp%2!=0)temp=(-temp+1)/2;elsetemp=-temp/2;}i--;if(i>=30){cout<<"NIE"<<endl;}else{cout<<i;for(i--;i>=0;i--)if(num[i]!=0)printf(" %d",i);cout<<endl;}temp=-n;for(i=0;temp!=0;i++){num[i]=temp%2;if(num[i]<0)num[i]=-num[i];if(temp<0&&temp%2!=0)temp=(-temp+1)/2;elsetemp=-temp/2;}i--;if(i>=30){cout<<"NIE"<<endl;}else{cout<<i;for(i--;i>=0;i--)if(num[i]!=0)printf(" %d",i);cout<<endl;}}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.