zoukankan      html  css  js  c++  java
  • 有一个数字出现1次,其它都出现3次

    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    #include <functional>
    #include <vector>
    #include <cmath>
    
    using namespace std;
    #define N 1005
    int a[N];
    int b[N];
    int cnt = 0;
    int n;
    int main() {
    	cin >> n;
    	int s = 0;
    	for (int i = 0; i < n; i++) {
    		cnt = 0;
    		cin >> a[i];
    		int x = a[i];
    		while (x) {
    			b[cnt] += x % 3;
    			b[cnt] %= 3;
    			cnt++;
    			x /= 3;
    		}
    	}
    	int d = 1;
    	for (int i = 0; i < 20; i++) {
    		s += b[i] * d;
    		d *= 3;
    	}
    	printf("%d
    ", s);
    	return 0;
    }
    

    与两次的类似,两次的直接对每个数异或就行了,本质也是消掉2次重复的,3次换成3进制,每一位*3%3肯定等于0,所以影响为0 就只剩下 出现一次的数字了

  • 相关阅读:
    F
    D
    J
    M
    H
    L
    Android线程之间的通讯
    Java4Android基础学习之异常
    Java4Android基础学习之接口
    Java4Android基础学习之包与访问权限
  • 原文地址:https://www.cnblogs.com/HaibaraAi/p/5952305.html
Copyright © 2011-2022 走看看