d177: 求最小正同界角

Tags: big-handle


出處:https://zerojudge.tw/ShowProblem?problemid=d177
提交:https://zerojudge.tw/Submissions?problemid=d177&account=allllllan123456


問題:給你一個非負整數 $n\in[0,36893488147419103232]$,求 n 除以 360 的餘數。


解法:這題看似需要大數處理,其實不然,因為 360 不是大數,我們可以好好利用在加、減、乘的運算底下會同餘的特性,以十進制表示 n 之後再拆開來取餘數。


 1#include <bits/stdc++.h>
 2using namespace std;
 3
 4int main() { ios_base::sync_with_stdio(false); cin.tie(0); // IO 優化
 5    string str; int T; cin >> T;
 6    while (T--) { cin >> str;
 7        int ans = 0;
 8        for (int i=0; i<str.length(); i++)
 9            ans = (ans * 10 + str[i]-'0') % 360;
10        cout << ans << '\n';
11    }
12    return 0;
13}

no image