#include <bits/stdc++.h> using namespace std; string a(int n){ // 建立一個函式庫計算十進位轉三進位 if(n==0) return "0"; // 如果n等於0則回傳0 string s=""; while(n>0){ int m=n%3; // 將n除以3的餘數m s=to_string(m)+s; // 將n除以3的餘數加到s後面 n/=3; // 將n除以3 } return s; // 回傳s } int main() { int n; while(cin>>n&&n>=0){ string t=a(n); // 將n傳入函式a cout<<t<<endl; } }
#include using namespace std; string a(int n){ // 建立一個函式庫計算十進位轉三進位 if(n==0) return "0"; // 如果n等於0則回傳0 string s=""; while(n>0){ int m=n%3; // 將n除以3的餘數m s=to_string(m)+s; // 將n除以3的餘數加到s後面 n/=3; // 將n除以3 } return s; // 回傳s } int main() { int n; while(cin>>n&&n>=0){ string t=a(n); // 將n傳入函式a cout<endl; } }
11 題 ans : b