注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号
x
这两天在地里逛了下 做了一些OA题,今天做OA预期会遇到变态的数论题,于是周末好好准备了一下但是没有遇到,人品好!
收集的Twitter OA的题及自己写的代码- //N=p^q
- bool super_power(unsigned long long int n) {
- for (unsigned long long int p = sqrt(n); p > 1; p--) {
- if (pow(p, (int)(log(n)/log(p))) == n) {
- cout << p << "_" << log(n)/log(p) << endl;
- return true;
- }
- }
- return false;
- }
- //n lines cut plain to x parts
- int n_cuts(int n) {
- //f(n) = f(n-1) + n;
- //f(n) = (2+n*n + n)/2;
- return (2 + n + n * n) / 2;
- }
- // game of thrones
- bool palindrome_permutation(string &s)
- {
- int n = s.size();
- int table[26] = {0};
- int odds = 0;
- for (int i = 0; i < n; i++) {
- table[s[i]-'a'] = (table[s[i]-'a'] + 1) % 2;
- odds += (table[s[i]-'a'] ?1 :-1);
- }
- return (odds <= 1);
- }
- // valid flag
- int main() {
- /* Enter your code here. Read input from STDIN. Print output to STDOUT */
- int t;
- cin >> t;
- for (int i = 0; i < t; i++) {
- int n, m;
- cin >> n >> m;
- vector<string> strvec;
- for (int j = 0; j < n; j++) {
- string cur;
- cin >> cur;
- cout << cur << endl;
- strvec.push_back(cur);
- }
- bool valid = true;
- for (int j = 0; j < n; j++) {
- if (strvec[j].size() != m) {
- valid = false;
- break;
- }
- for (int k = 0; k < m-1; k++) {
- if (strvec[j][k] != strvec[j][k+1]) {
- valid = false;
- break;
- }
- }
- if (valid == false) {
- break;
- }
- if (j != 0 && strvec[j-1][0] == strvec[j][0]) {
- valid = false;
- break;
- }
- }
- cout << (valid ?"YES" :"NO") << endl;
- }
- return 0;
- }
- //gcd
- int gcd(int a, int b) {
- while (a != b) {
- int d = fabs(a-b);
- a = min(a, b);
- b = d;
- }
- return a;
- }
- // common elements in array
- int common_elements(int start, int step, int end, int start1, int factor, int end1) {
- int cnt = 0;
- for (int i = start1, i <= end1 && i <= end, i *= factor) {
- if (i < start) continue;
- if ((i - start) % step == 0) cnt++;
- }
- return cnt;
- }
- // count primes + 1 = # of x fufill (x-1)! % x == x-1
- int countPrimes(int n) {
- if (n <= 2) {
- return 0;
- }
- //only test odd number start from 3 to n;
- //idx = num-1/2 - 1 ;
- char bitmap[n] = {0};
- int count = 1;
- int upper = sqrt(n);
- for (int i = 3; i < n; i += 2) {
- if (bitmap[i] == 0) {//i is prime
- count++;
- if (i > upper) continue;
- for (int j = i*i; j < n; j += 2*i) {
- bitmap[j] = 1;//j is not prime;
- }
- }
- }
- return count;
- }
- // first repeating
- char first_repeating_char(string &s) {
- int n = s.size();
- int pos = n;
- int table[256][2] = {0};
- for (int i = 0; i < n; i++) {
- int idx = s[i];
- if (table[idx][0] == 0) {
- table[idx][1] = i; //first appearance
- }
- if (++table[idx][0] == 2) {//repeat
- pos = min(pos, table[idx][1]);
- }
- }
- return pos < n ?' ' : s[pos];
- }
- //euler function mutual prime less than n
- int mutual_prime_cnt(int n) {
- double ans;
- int temp = n;
- ans = n;
- for(int i = 2; i*i <= n; i++) {
- if(n % i == 0){
- ans = ans / i * (i-1);
- n /= i;
- while( n%i == 0) n/=i;
- }
- }
- if(n != 1 ) {
- ans = ans/n*(n-1);
- }
- return (temp == 1 ?0 :ans);
- }
- //closet numbers
- int main() {
- /* Enter your code here. Read input from STDIN. Print output to STDOUT */
- int n;
- cin >> n;
- vector<int> array;
- for (int i = 0; i < n; i++) {
- int a;
- cin >> a;
- array.push_back(a);
- }
- sort(array.begin(), array.end());
- int min_d = 0x7FFFFFFF;
- for (int i = 0; i < n-1; i++) {
- if (array[i+1] - array[i] <= min_d) {
- min_d = array[i+1] - array[i];
- }
- }
- for (int i = 0; i < n-1; i++) {
- if (array[i+1] - array[i] == min_d) {
- cout << array[i] << " " << array[i+1] << " ";
- }
- }
- return 0;
- }
复制代码 |