1298. Digital Conversion
Description
There is a number system with a base number of 3. The weight value can be-101, 1, and expressed respectively with the symbol-, 0, 1. For example, of this number system represents 10 of the decimal number, that is, 1*(3 ^ 2) + 0*(3 ^ 1) + 1*(3 ^ 0) = 10, another example is that-0 in this number system indicates-3 of the decimal number, that is,-1 * (3 ^ 1) + 0*(3 ^ 0) =-3. Programming requires that a given signed integer be converted to a new number. There cannot be any excess 0 before this number. For example, if the value of 10 is 101, do not output it to 0101.
Input
The file has one or more rows, and each row has an integer.N(-2,147,483,647 ≤ n ≤ 2,147,483,647), there is no other Separator in the integer.
Output
Output a line for each row of the input file. This line is a new numeric representation of the integer of the input line. No extra blank lines are allowed. Leading spaces are not allowed before each line.
Sample Input
10
-3
Sample output
101
-0
Problem Source
Zsuacm team member
Pay attention to various situations.
#include <iostream>#include <cstring>using namespace std;int main(){ int n; while(cin>>n) { if(n==0) { cout<<0<<endl; continue; } char result[50]; for(int i=0;i<50;i++) result[i]='A'; int shang,yu,i=0; while(n!=0) { yu=n%3; shang=n/3; if(yu==2) { shang++; result[i]='-'; } else if(yu==-1) { result[i]='-'; } else if(yu==1) { result[i]=1+'0'; } else if(yu==-2) { shang--; result[i]=1+'0'; } else result[i]='0'; n=shang; i++; } int index=49; while(result[index]=='A') index--; for(int i=index;i>=0;i--) cout<<result[i]; cout<<endl; } return 0;}