头疼的Litmxs
http://oj.acm.zstu.edu.cn/JudgeOnline/problem.php?id=4435
C++版本一
題解:
負(fù)進(jìn)制,頭一次聽(tīng)說(shuō)
根據(jù)進(jìn)制轉(zhuǎn)換的權(quán)值來(lái)類(lèi)比
1 -2 4 -8
奇次冪是負(fù)數(shù)說(shuō)明在這個(gè)位置上有個(gè)1,不是加而是減一個(gè)權(quán)值
其他的見(jiàn)代碼
/* *@Author: STZG *@Language: C++ */ #include <bits/stdc++.h> #include<iostream> #include<algorithm> #include<cstdlib> #include<cstring> #include<cstdio> #include<string> #include<vector> #include<bitset> #include<queue> #include<deque> #include<stack> #include<cmath> #include<list> #include<map> #include<set> //#define DEBUGusing namespace std; typedef long long ll; const int N=100; const double PI = acos(-1.0); const double EXP = 1E-8; const int INF = 0x3f3f3f3f; int t,n,m,k; bool a[N]; int main() { #ifdef DEBUGfreopen("input.in", "r", stdin);//freopen("output.out", "w", stdout); #endifscanf("%d",&t);while(t--){scanf("%d%d",&n,&k);int cnt=0;if(n==0){cout<<0<<endl;continue;}if(k==2){while(n!=0){a[cnt++]=n%2;n/=2;}}else{while(n!=0){a[cnt++]=n%2;if(a[cnt-1]==1&&cnt%2==0)n=(n+1)/2;elsen/=2;}}for(int i=cnt-1;i>=0;i--){cout<<a[i];}cout<<endl;}//cout << "Hello world!" << endl;return 0; }?
C++版本二
題解:本題關(guān)鍵在-2 進(jìn)制如何解決
? 在做2 進(jìn)制的時(shí)候,我們是直接對(duì)n=n/2,忽略掉將n 減去余數(shù)的過(guò)程,因?yàn)閕nt 的除法會(huì)向下取整。
? 對(duì)于-2 進(jìn)制,我們只要補(bǔ)回減去余數(shù)的過(guò)程就可以了。?
C++版本三
#include<bits/stdc++.h>using namespace std; typedef long long ll; const int N=1e5+5;int main(void){int n;int k;int T;scanf("%d",&T);while(T--){scanf("%d%d",&n,&k);if(n==0){cout << 0 << endl;continue;}// cout <<n<<" "<<k<<endl;if(k==-2){string ans="";while(n){ll t=n%(-2);n/=(-2);if(t<0) t+=2,n++;// ??μ?++ans+='0'+t;}reverse(ans.begin(),ans.end());cout << ans << endl;}else if(k==2){string ans="";while(n){ll t=n%2;n/=2;ans+='0'+t;}reverse(ans.begin(),ans.end());cout << ans << endl;}}return 0; }?
總結(jié)
- 上一篇: Suffix Zeroes
- 下一篇: 王子救公主