Find the Difference(類似 Single Number)

#include<stdio.h>
#include<iostream>
#include<string.h>
char findTheDifference(char* s, char* t) {
	int i, j,s1=0,s2=0;
	for (i = 0; i < strlen(s)+1; i++)
	{
		s1 += s[i];
		s2 += t[i];
	}
	return(char)(s2 - s1);

}
int main()
{
	char *s = (char*)malloc(sizeof(char) * 100);
	char *t = (char*)malloc(sizeof(char) * 100);
	scanf("%s", s);
	//scanf("%s", t);隨機產生
	char k;
	k = findTheDifference(s, t);
	printf("%c", k);
	system("pause");
	return 0;
}

方法二:

char findTheDifference(char* s, char* t) {
   char ss=0;
	int i, j;
	for (i = 0; i < strlen(s); i++)
	{
		ss ^= s[i];
	}
	for (i = 0; i < strlen(s) + 1; i++)
	{
		ss ^= t[i];
	}
	return ss;
}

single number(只有一個出現奇數次)

nt singleNumber(int a[], int n) {  
     int num = 0;  
     for (int i = 0; i < n; ++i) {  
         num ^= a[i];  
     }  
     return num;  
    }  


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章