5 solutions
-
5
(好像题解区没有跟我思路一样的吧?)#include <bits/stdc++.h> using namespace std; int n, cur, a[10]; int main() { cin >> n; while (n != 0) { int t = n % 3; n /= 3; if (t == 2) t = -1, n += 1; a[cur++] = t; } for (int i = 8; i >= 0; --i) if (a[i] != 0) cout << a[i] * pow(3, i) << " "; return 0; }
-
0
这题用pow不会报错吧
#include <cmath> using namespace std; int main() { int n,z[100] = {},temp,out[100]={}; cin >> n; for (int i=0;n>0;i++){ temp=i; z[i]=n%3; n/=3; } int cnt=0; for (int i=0;i<=temp+1;i++){ if (z[i]==2){ out[cnt]=pow(3,i)-pow(3,i)*2; cnt++; z[i]=0; z[i+1]+=1; }else if(z[i]==3){ z[i+1]+=1; }else if(z[i]==1){ out[cnt]=pow(3,i); cnt++; } } for (int i=cnt-1;i>=0;i--){ cout << out[i] << " "; } }
-
0
#include <bits/stdc++.h> using namespace std; int f[10]; string s3; int pow(int n,int c){ // 重写 pow(因为用 cmath 库的那个会报错) int r = 1; for (int i = 0;i < c;i++){ r *= n; } return r; } int main(int argc, char **argv){ int n; cin >> n; while (n){ // 将 n 变成 3 进制倒序 s3 += n % 3 + '0'; n /= 3; } for (int i = 0;s3[i];i++){ if(s3[i] == '1' || s3[i] == 1){ // 如果等于 1,直接把砝码放上去 f[i] = 1; }else if (s3[i] == '2'){ // 如果要用两个砝码,就把这个砝码放到另一边,并进一 f[i] = -1; s3[i + 1]++; }else if (s3[i] == '3'){ s3[i] = '0'; s3[i + 1]++; } } for (int i = 10;i >= 0;i--){ // 输出 if (f[i] == 1){ printf("%d ",pow(3,i)); }else if (f[i] == -1){ printf("-%d ",pow(3,i)); } } return 0; }
-
-4
#include <bits/stdc++.h> using namespace std; int n, k = 0, f[100006] = {0}, a[100006]; //原本的pow函数用不了 int pow(int x)//第i个砝码 3^i { int w = 1; for (int i = 1;i <= x;i++) { w *= 3; } return w; } int main() { cin >> n; //15的三进制倒序 //15->021 while (n)// 把n变成3进制倒序 { a[k] = n%3; //cout << a[k] << " "; n /= 3; k++; } //a[0]=0 a[1]=2 a[2]=1 //f[0]=0 f[1]=0 f[2]=0 for (int i = 0; i <= k; i++) { if(a[i] == 1)// 如果等于 1,直接把砝码放上去 { f[i] = 1;//放在右侧 } else if (a[i] == 2)// 如果等于2,就把这个砝码放到另一边,并进位 { f[i] = -1;//放在左侧 a[i + 1] += 1; } else if (a[i] == 3)//进位 { a[i] = 0; a[i + 1] += 1; } } //a[0]=0 a[1]=2 a[2]=2 a[3]=1 //f[0]=0 f[1]=-1 f[2]=-1 f[3]=1 for (int i = 10;i >= 0;i--)//倒序输出从大到小 { if (f[i] == 1)//放在右侧 { //printf("%d ",pow(i)); cout << pow(i) << " "; } else if (f[i] == -1)//放在左侧 { //printf("-%d ",pow(i)); cout << "-" << pow(i) << " "; } } return 0; }
- 1
Information
- ID
- 950
- Time
- 1000ms
- Memory
- 256MiB
- Difficulty
- 7
- Tags
- (None)
- # Submissions
- 119
- Accepted
- 26
- Uploaded By