1.數的大小可以很大,因為我是用陣列編號去做,所以要設比較大
2.插入元素可以重複(陷阱),所以要判斷是否已經有了
3.判斷子集合,輸出為空集合,即指A本身是空集合,才輸出empty
例:
(1). A {3,4} B {2,3,5} 輸出 F
(2). A {} B {1,2,3} 輸出 empty
4.交集若為空集合,仍要輸出empty
註:num為集合中元素的個數
#include<iostream>
using namespace std;
class poly {
public:
poly() { initializeSet(); }
void initializeSet(void);
void insertElement(void);
void deleteElement(void);
void Intersection(poly a);
void Union(poly a);
void subSet(poly a);
void setPrint(void);
private:
int set[10001];
int num;
};
void poly::initializeSet()
{
for (int i = 0; i < 10001; i++)
set[i] = 0;
num = 0;
}
void poly::insertElement()
{
int x;
cin >> x;
if (set[x] == 0)
{
set[x] = 1;
num++;
}
}
void poly::deleteElement()
{
int y;
cin >> y;
if (set[y] != 0)
{
set[y] = 0;
num--;
}
}
void poly::Intersection(poly a)
{
int j = 0, isEmpty = 1;
int inset[10001] = { 0 };
for (int i = 0; i < 10001; i++)
{
if (set[i] != 0 && a.set[i] != 0)
{
inset[i] = 1;
isEmpty = 0;
}
}
if (isEmpty == 0)
{
for (int i = 0; i < 10001; i++)
{
if (inset[i] != 0)
{
if (j > 0)
cout << ",";
cout << i;
j++;
}
}
cout << endl;
}
else
cout << "empty" << endl;
}
void poly::Union(poly a)
{
int j = 0;
int unset[10001] = { 0 };
for (int i = 0; i < 10001; i++)
{
if (set[i] != 0 || a.set[i] != 0)
unset[i] = 1;
}
for (int i = 0; i < 10001; i++)
{
if (unset[i] != 0)
{
if (j > 0)
cout << ",";
cout << i;
j++;
}
}
cout << endl;
}
void poly::subSet(poly a)
{
int count = 0;
for (int i = 0; i < 10001; i++)
{
if (set[i] != 0 && a.set[i] != 0)
count++;
}
if (num == 0)
cout << "empty" << endl;
else if (count == num)
cout << "T" << endl;
else
cout << "F" << endl;
}
void poly::setPrint()
{
int j = 0;
for (int i = 0; i < 10001; i++)
{
if (set[i] != 0)
{
if (j > 0)
cout << ",";
cout << i;
j++;
}
}
cout << endl;
}
int main()
{
poly A, B;
A.insertElement();
A.insertElement();
A.insertElement();
A.deleteElement();
B.insertElement();
B.insertElement();
B.insertElement();
B.deleteElement();
A.setPrint();
B.setPrint();
A.subSet(B);
A.Intersection(B);
A.Union(B);
return 0;
}
2018年8月13日 星期一
2018年8月10日 星期五
itsa [C_AR026-中]選擇排序法動態展示
1.輸入測資時需使用while,在輸入時以非整數做結束,例:字元( / * - + )
2.選擇排序法:從最大或最小開始排序,這題是從最大
3.先找最大值與陣列編號按順序交換,交換後找次大,以此類推
例:42135
陣列編號:01234
第1次 最大值是5 跟4做交換(編號0) 輸出 52134
第2次 最大值是4 跟2做交換(編號1) 輸出 54132
#include<iostream>
using namespace std;
int main()
{
int N = 0, i = 0;
static int num[1500000]; //其實不用很大 大概最多到100就好
while (cin >> num[i++]);
N = i - 1;
for (int k = 0; k < N; k++)
{
if (k > 0)
cout << ",";
cout << num[k];
}
cout << endl;
for (int i = 0; i < N - 1; i++)
{
int max = i;
for (int j = i + 1; j < N; j++)
if (num[j] > num[max])
max = j;
if (max == i)
continue;
int tmp = num[i];
num[i] = num[max];
num[max] = tmp;
for (int k = 0; k < N; k++)
{
if (k > 0)
cout << ",";
cout << num[k];
}
cout << endl;
}
return 0;
}
2.選擇排序法:從最大或最小開始排序,這題是從最大
3.先找最大值與陣列編號按順序交換,交換後找次大,以此類推
例:42135
陣列編號:01234
第1次 最大值是5 跟4做交換(編號0) 輸出 52134
第2次 最大值是4 跟2做交換(編號1) 輸出 54132
#include<iostream>
using namespace std;
int main()
{
int N = 0, i = 0;
static int num[1500000]; //其實不用很大 大概最多到100就好
while (cin >> num[i++]);
N = i - 1;
for (int k = 0; k < N; k++)
{
if (k > 0)
cout << ",";
cout << num[k];
}
cout << endl;
for (int i = 0; i < N - 1; i++)
{
int max = i;
for (int j = i + 1; j < N; j++)
if (num[j] > num[max])
max = j;
if (max == i)
continue;
int tmp = num[i];
num[i] = num[max];
num[max] = tmp;
for (int k = 0; k < N; k++)
{
if (k > 0)
cout << ",";
cout << num[k];
}
cout << endl;
}
return 0;
}
itsa [C_AR025-易]計算ASCII字元
#include<iostream>
#include<string>
using namespace std;
int main()
{
int count = 0, x = 0;
int num[100] = { 0 }, times[100] = { 0 };
string s;
getline(cin, s);
for (int i = 0; i < s.length(); i++)
{
count = 1;
if (s[i] != ' ')
{
for (int j = 0; j < s.length(); j++)
{
if (i != j && s[j] == s[i])
{
count++;
s[j] = ' ';
}
}
num[x] = s[i];
times[x] = count;
s[i] = ' ';
x++;
}
}
for (int i = 0; i < x - 1; i++)
{
for (int j = i + 1; j < x; j++)
{
if (i != j && num[i] < num[j])
{
int tmp = num[i];
num[i] = num[j];
num[j] = tmp;
int temp = times[i];
times[i] = times[j];
times[j] = temp;
}
}
}
for (int i = 0; i < x; i++)
cout << num[i] << " " << times[i] << endl;
return 0;
}
#include<string>
using namespace std;
int main()
{
int count = 0, x = 0;
int num[100] = { 0 }, times[100] = { 0 };
string s;
getline(cin, s);
for (int i = 0; i < s.length(); i++)
{
count = 1;
if (s[i] != ' ')
{
for (int j = 0; j < s.length(); j++)
{
if (i != j && s[j] == s[i])
{
count++;
s[j] = ' ';
}
}
num[x] = s[i];
times[x] = count;
s[i] = ' ';
x++;
}
}
for (int i = 0; i < x - 1; i++)
{
for (int j = i + 1; j < x; j++)
{
if (i != j && num[i] < num[j])
{
int tmp = num[i];
num[i] = num[j];
num[j] = tmp;
int temp = times[i];
times[i] = times[j];
times[j] = temp;
}
}
}
for (int i = 0; i < x; i++)
cout << num[i] << " " << times[i] << endl;
return 0;
}
itsa [C_AR024-難]約會配對問題
1.跑n次迴圈,找目前未配對的陣列最大值
2.每跑完一次,紀錄配對完的男女編號
3.在跑下一次時,剔除配對過的編號
#include<iostream>
using namespace std;
int main()
{
int n, x = 0, max = 0, count = 0, check = 0;
int arr[100][100], a[100], b[100];
cin >> n;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
cin >> arr[i][j];
while (count != n)
{
max = 0;
for (int i = 0; i < n; i++)
{
check = 0;
for (int j = 0; j < x; j++)
if (i == a[j])
{
check = 1;
break;
}
if (check == 0)
{
for (int j = 0; j < n; j++)
{
check = 0;
for (int k = 0; k < x; k++)
if (j == b[k])
{
check = 1;
break;
}
if (check == 0 && arr[i][j] > max)
{
max = arr[i][j];
a[x] = i;
b[x] = j;
}
}
}
}
count++;
x++;
}
for (int k = 0; k < x; k++)
cout << "boy " << a[k] + 1 << " pair with girl " << b[k] + 1 << endl;
return 0;
}
2.每跑完一次,紀錄配對完的男女編號
3.在跑下一次時,剔除配對過的編號
#include<iostream>
using namespace std;
int main()
{
int n, x = 0, max = 0, count = 0, check = 0;
int arr[100][100], a[100], b[100];
cin >> n;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
cin >> arr[i][j];
while (count != n)
{
max = 0;
for (int i = 0; i < n; i++)
{
check = 0;
for (int j = 0; j < x; j++)
if (i == a[j])
{
check = 1;
break;
}
if (check == 0)
{
for (int j = 0; j < n; j++)
{
check = 0;
for (int k = 0; k < x; k++)
if (j == b[k])
{
check = 1;
break;
}
if (check == 0 && arr[i][j] > max)
{
max = arr[i][j];
a[x] = i;
b[x] = j;
}
}
}
}
count++;
x++;
}
for (int k = 0; k < x; k++)
cout << "boy " << a[k] + 1 << " pair with girl " << b[k] + 1 << endl;
return 0;
}
itsa [C_AR023-易]字根與子字串
注意:有可能會有要尋找的字串大於被搜尋的字串!
#include<iostream>
#include<string>
using namespace std;
int main()
{
int a = 0, ans = 0;
string s1, s2;
getline(cin, s1);
getline(cin, s2);
if (s1.length() <= s2.length())
{
for (int i = 0; i <= s2.length() - s1.length(); i++)
{
a = 0;
for (int j = 0; j < s1.length(); j++)
{
if (s2[i + j] != s1[j])
{
a = 1;
break;
}
}
if (a == 0)
{
ans = 1;
break;
}
}
}
if (ans == 1)
cout << "YES" << endl;
else
cout << "NO" << endl;
return 0;
}
#include<iostream>
#include<string>
using namespace std;
int main()
{
int a = 0, ans = 0;
string s1, s2;
getline(cin, s1);
getline(cin, s2);
if (s1.length() <= s2.length())
{
for (int i = 0; i <= s2.length() - s1.length(); i++)
{
a = 0;
for (int j = 0; j < s1.length(); j++)
{
if (s2[i + j] != s1[j])
{
a = 1;
break;
}
}
if (a == 0)
{
ans = 1;
break;
}
}
}
if (ans == 1)
cout << "YES" << endl;
else
cout << "NO" << endl;
return 0;
}
itsa [C_AR022-易]字母出現的頻率
先轉全部大寫或小寫,再算出現次數。
#include<iostream>
#include<string>
using namespace std;
int main()
{
int a[26] = { 0 };
string s;
getline(cin, s);
for (int i = 0; i < s.length(); i++)
{
if (s[i] >= 'a' && s[i] <= 'z')
s[i] -= 32; //轉大寫
if ((s[i] >= 'a' && s[i] <= 'z') || (s[i] >= 'A' && s[i] <= 'Z'))
a[s[i] - 'A']++;
}
for (int i = 0; i < 26; i++)
{
if (i > 0)
cout << " ";
cout << a[i];
}
cout << endl;
return 0;
}
#include<iostream>
#include<string>
using namespace std;
int main()
{
int a[26] = { 0 };
string s;
getline(cin, s);
for (int i = 0; i < s.length(); i++)
{
if (s[i] >= 'a' && s[i] <= 'z')
s[i] -= 32; //轉大寫
if ((s[i] >= 'a' && s[i] <= 'z') || (s[i] >= 'A' && s[i] <= 'Z'))
a[s[i] - 'A']++;
}
for (int i = 0; i < 26; i++)
{
if (i > 0)
cout << " ";
cout << a[i];
}
cout << endl;
return 0;
}
2018年7月23日 星期一
itsa [C_CH20-易]盜墓驚魂
1.把錢轉成2進位
2.用XOR做運算
#include<iostream>
using namespace std;
int main()
{
int n,num,sum,flag;
int arr[8] = {0};
num = 0;
sum = 0;
cin >> n;
int p[100];
int b[100] = {0};
for (int j = 0;j < n; j++)
{
cin >> p[j];
}
for (int k = 0;k < n; k++)
{
for (int x = 7;x >= 0; x--)
{
arr[x] = p[k] % 2;
p[k] /= 2;
}
int y = 1;
for (int x = 7;x >= 0; x--)
{
b[k] += arr[x] * y;
y *= 10;
}
}
for (int z = 0;z < n; z++)
{
if (z == 0)
num = b[z];
else
num = num ^ b[z];
}
flag = num ^ 0;
if (flag != 0)
cout << "yes" << endl;
else
cout << "no" << endl;
return 0;
}
2.用XOR做運算
#include<iostream>
using namespace std;
int main()
{
int n,num,sum,flag;
int arr[8] = {0};
num = 0;
sum = 0;
cin >> n;
int p[100];
int b[100] = {0};
for (int j = 0;j < n; j++)
{
cin >> p[j];
}
for (int k = 0;k < n; k++)
{
for (int x = 7;x >= 0; x--)
{
arr[x] = p[k] % 2;
p[k] /= 2;
}
int y = 1;
for (int x = 7;x >= 0; x--)
{
b[k] += arr[x] * y;
y *= 10;
}
}
for (int z = 0;z < n; z++)
{
if (z == 0)
num = b[z];
else
num = num ^ b[z];
}
flag = num ^ 0;
if (flag != 0)
cout << "yes" << endl;
else
cout << "no" << endl;
return 0;
}
訂閱:
文章 (Atom)