1121 Damn Single (25 分)

"Damn Single (單身狗)" is the Chinese nickname for someone who is being single. You are supposed to find those who are alone in a big party, so they can be taken care of.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤50,000), the total number of couples. Then N lines of the couples follow, each gives a couple of ID's which are 5-digit numbers (i.e. from 00000 to 99999). After the list of couples, there is a positive integer M (≤?10,000) followed by M ID's of the party guests. The numbers are separated by spaces. It is guaranteed that nobody is having bigamous marriage (重婚) or dangling with more than one companion.

Output Specification:

First print in a line the total number of lonely guests. Then in the next line, print their ID's in increasing order. The numbers must be separated by exactly 1 space, and there must be no extra space at the end of the line.

Sample Input:

3
11111 22222
33333 44444
55555 66666
7
55555 44444 10000 88888 22222 11111 23333

Sample Output:

5
10000 23333 44444 55555 88888
//初始化用用-1
//因為有人的編號可能是00000
//所以第二個點不過 
#include<cstdio>
const int maxn = 100010;int spouse[maxn] = {0};int main(){int n,id1,id2;for(int i = 0; i < maxn; i++) spouse[i] = -1; scanf("%d",&n);while(n--){scanf("%d%d",&id1,&id2);spouse[id1] = id2;spouse[id2] = id1;  }scanf("%d",&n);while(n--){scanf("%d",&id1);if(spouse[id1] >= 0 && spouse[spouse[id1]] >= 0){spouse[id1] = -2;}else if(spouse[id1] >= 0 &&spouse[spouse[id1]] == -2){spouse[spouse[id1]] = 2;}else{spouse[id1] = -3;}}int cnt = 0;for(int i = 0; i < maxn; i++){if(spouse[i] < -1) cnt++;}printf("%d\n",cnt);for(int i = 0; i < maxn; i++){if(spouse[i] < -1){printf("%05d",i);cnt--;if(cnt > 0) printf(" ");}    }return 0;
}

?

轉載于:https://www.cnblogs.com/wanghao-boke/p/10453470.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/385133.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/385133.shtml
英文地址,請注明出處:http://en.pswp.cn/news/385133.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

1124 Raffle for Weibo Followers (20 分)

John got a full mark on PAT. He was so happy that he decided to hold a raffle&#xff08;抽獎&#xff09; for his followers on Weibo -- that is, he would select winners from every N followers who forwarded his post, and give away gifts. Now you are suppose…

987. 二叉樹的垂序遍歷

給定二叉樹&#xff0c;按垂序遍歷返回其結點值。 對位于 (X, Y) 的每個結點而言&#xff0c;其左右子結點分別位于 (X-1, Y-1) 和 (X1, Y-1)。 把一條垂線從 X -infinity 移動到 X infinity &#xff0c;每當該垂線與結點接觸時&#xff0c;我們按從上到下的順序報告結點的值…

28. 實現 strStr()

實現 strStr() 函數。 給定一個 haystack 字符串和一個 needle 字符串&#xff0c;在 haystack 字符串中找出 needle 字符串出現的第一個位置 (從0開始)。如果不存在&#xff0c;則返回 -1。 示例 1: 輸入: haystack "hello", needle "ll" 輸出: 2 示例…

1136 A Delayed Palindrome (20 分)

Consider a positive integer N written in standard notation with k1 digits a?i?? as a?k???a?1??a?0?? with 0 for all i and a?k??>0. Then N is palindromic if and only if a?i??a?k?i?? for all i. Zero is written 0 and is also palindrom…

1044 火星數字 (20 分)

火星人是以 13 進制計數的&#xff1a; 地球人的 0 被火星人稱為 tret。地球人數字 1 到 12 的火星文分別為&#xff1a;jan, feb, mar, apr, may, jun, jly, aug, sep, oct, nov, dec。火星人將進位以后的 12 個高位數字分別稱為&#xff1a;tam, hel, maa, huh, tou, kes, he…

43. 字符串相乘

給定兩個以字符串形式表示的非負整數 num1 和 num2&#xff0c;返回 num1 和 num2 的乘積&#xff0c;它們的乘積也表示為字符串形式。 示例 1: 輸入: num1 "2", num2 "3" 輸出: "6" 示例 2: 輸入: num1 "123", num2 "456&qu…

1045 快速排序 (25 分)

著名的快速排序算法里有一個經典的劃分過程&#xff1a;我們通常采用某種方法取一個元素作為主元&#xff0c;通過交換&#xff0c;把比主元小的元素放到它的左邊&#xff0c;比主元大的元素放到它的右邊。 給定劃分后的 N 個互不相同的正整數的排列&#xff0c;請問有多少個元…

1049 數列的片段和 (20 分)

給定一個正數數列&#xff0c;我們可以從中截取任意的連續的幾個數&#xff0c;稱為片段。例如&#xff0c;給定數列 { 0.1, 0.2, 0.3, 0.4 }&#xff0c;我們有 (0.1) (0.1, 0.2) (0.1, 0.2, 0.3) (0.1, 0.2, 0.3, 0.4) (0.2) (0.2, 0.3) (0.2, 0.3, 0.4) (0.3) (0.3, 0.4) (0…

C++ Priemer目錄索引

序號內容1 【C Primer | 15】虛函數表剖析&#xff08;一&#xff09; 2 【C Priemr | 15】虛函數表剖析&#xff08;二&#xff09; 3 【C Priemr | 15】虛函數表剖析&#xff08;三&#xff09; 4一個C程序執行main函數前和執行完main函數后會發生什么。1 【C Priemr | 15】虛…

1046 劃拳 (15 分)

劃拳是古老中國酒文化的一個有趣的組成部分。酒桌上兩人劃拳的方法為&#xff1a;每人口中喊出一個數字&#xff0c;同時用手比劃出一個數字。如果誰比劃出的數字正好等于兩人喊出的數字之和&#xff0c;誰就贏了&#xff0c;輸家罰一杯酒。兩人同贏或兩人同輸則繼續下一輪&…

多線程順序交替打印ABCD

題目&#xff1a;按照 ABCD的順序交替打印。 1. 測試代碼&#xff1a; #include <iostream> #include <unistd.h> #include <stdlib.h> #include <pthread.h> using namespace std;struct {int t;pthread_mutex_t mutex;pthread_cond_t cond; } tes…

第一個只出現一次的字符

在一個字符串(0<字符串長度<10000&#xff0c;全部由字母組成)中找到第一個只出現一次的字符,并返回它的位置, 如果沒有則返回 -1&#xff08;需要區分大小寫&#xff09;. 解法&#xff1a; class Solution { public:int FirstNotRepeatingChar(string str) {unordered…

《Leetcode》目錄

序號題目題解標記1 43. 字符串相乘 字符串2513. 找樹左下角的值二叉樹3 450. 刪除二叉搜索樹中的節點 二叉樹486. 分隔鏈表鏈表155 155. 最小棧 C題解棧77. 組合C題解回溯算法15.三數之和C題解

一個C++程序執行main函數前和執行完main函數后會發生什么。

總結&#xff1a; main函數執行之前&#xff0c;主要就是初始化系統相關資源&#xff1a; 設置棧指針初始化static靜態和global全局變量&#xff0c;即data段的內容將未初始化部分的賦初值&#xff1a;數值型short&#xff0c;int&#xff0c;long等為0&#xff0c;bool為FALS…

1144 The Missing Number (20 分)

Given N integers, you are supposed to find the smallest positive integer that is NOT in the given list. Input Specification: Each input file contains one test case. For each case, the first line gives a positive integer N (≤10?5??). Then N integers are…

【面試寶典 | 01】面經

字節跳動提前批后端第三面涼經該來的終究會來的

1148 Werewolf - Simple Version (20 分)

Werewolf&#xff08;狼人殺&#xff09; is a game in which the players are partitioned into two parties: the werewolves and the human beings. Suppose that in a game, player #1 said: "Player #2 is a werewolf.";player #2 said: "Player #3 is a h…

算法默寫

序號內容1快速排序算法2堆排序算法3歸并排序算法

1149 Dangerous Goods Packaging (25 分)

When shipping goods with containers, we have to be careful not to pack some incompatible goods into the same container, or we might get ourselves in serious trouble. For example, oxidizing agent &#xff08;氧化劑&#xff09; must not be packed with flamma…

《數據結構與算法》

序號內容1排序算法概念2快速排序算法3堆排序算法4歸并排序算法