1018 锤子剪刀布 1025 反转链表
迪丽瓦拉
2024-05-29 01:08:12
0

现给出两人的交锋记录,请统计双方的胜、平、负次数,并且给出双方分别出什么手势的胜算最大。

输入格式:
输入第 1 行给出正整数 N(≤10
5
),即双方交锋的次数。随后 N 行,每行给出一次交锋的信息,即甲、乙双方同时给出的的手势。C 代表“锤子”、J 代表“剪刀”、B 代表“布”,第 1 个字母代表甲方,第 2 个代表乙方,中间有 1 个空格。

输出格式:
输出第 1、2 行分别给出甲、乙的胜、平、负次数,数字间以 1 个空格分隔。第 3 行给出两个字母,分别代表甲、乙获胜次数最多的手势,中间有 1 个空格。如果解不唯一,则输出按字母序最小的解。

输入样例:
10
C J
J B
C B
B B
B C
C C
C B
J B
B C
J J
输出样例:
5 3 2
2 3 5
B B

// 题目链接  https://pintia.cn/problem-sets/994805260223102976/exam/problems/994805304020025344#include
#includeusing namespace std;int main() {int n;char l, r;scanf("%d", &n);int lc[3] = {0};// 存储胜利的 符号次数int lc1[3] = {0};int rc1[3] = {0};for (int i = 0; i < n; ++i) {getchar(); //去除 \nscanf("%c %c", &l, &r);if (l == 'C' && r == 'J') {lc[0] += 1;lc1[0] += 1;} else if (l == 'C' && r == 'B') {lc[2] += 1;rc1[2] += 1;} else if (l == 'C' && r == 'C') {lc[1] += 1;} else if (l == 'J' && r == 'J') {lc[1] += 1;} else if (l == 'J' && r == 'C') {lc[2] += 1;rc1[0] += 1;} else if (l == 'J' && r == 'B') {lc[0] += 1;lc1[1] += 1;} else if (l == 'B' && r == 'C') {lc[0] += 1;lc1[2] += 1;} else if (l == 'B' && r == 'B') {lc[1] += 1;} else if (l == 'B' && r == 'J') {lc[2] += 1;rc1[1] += 1;}}printf("%d %d %d\n", lc[0], lc[1], lc[2]);printf("%d %d %d\n", lc[2], lc[1], lc[0]);if (lc1[0] >= lc1[1] && lc1[0] >= lc1[2]) {if (lc1[0] == lc1[2]) {l = 'B';} else {l = 'C';}}if (lc1[1] >= lc1[0] && lc1[1] >= lc1[2]) {if (lc1[0] == lc1[1]) {l = 'C';} else if (lc1[1] == lc1[2]) {l = 'B';} else {l = 'J';}}if (lc1[2] >= lc1[0] && lc1[2] >= lc1[1]) {l = 'B';}if (rc1[0] >= rc1[1] && rc1[0] >= rc1[2]) {if (rc1[0] == rc1[2]) {r = 'B';} else {r = 'C';}}if (rc1[1] >= rc1[0] && rc1[1] >= rc1[2]) {if (rc1[0] == rc1[1]) {r = 'C';} else if (rc1[1] == rc1[2]) {r = 'B';} else {r = 'J';}}if (rc1[2] >= rc1[0] && rc1[2] >= rc1[1]) {r = 'B';}printf("%c %c", l, r);return 0;
}

--------------------------------------------------------------------

给定一个常数 K 以及一个单链表 L,请编写程序将 L 中每 K 个结点反转。例如:给定 L 为 1→2→3→4→5→6,K 为 3,则输出应该为 3→2→1→6→5→4;如果 K 为 4,则输出应该为 4→3→2→1→5→6,即最后不到 K 个元素不反转。

输入格式:
每个输入包含 1 个测试用例。每个测试用例第 1 行给出第 1 个结点的地址、结点总个数正整数 N (≤10
5
)、以及正整数 K (≤N),即要求反转的子链结点的个数。结点的地址是 5 位非负整数,NULL 地址用 −1 表示。

接下来有 N 行,每行格式为:

Address Data Next
其中 Address 是结点地址,Data 是该结点保存的整数数据,Next 是下一结点的地址。

输出格式:
对每个测试用例,顺序输出反转后的链表,其上每个结点占一行,格式与输入相同。

输入样例:
00100 6 4
00000 4 99999
00100 1 12309
68237 6 -1
33218 3 00000
99999 5 68237
12309 2 33218
输出样例:
00000 4 33218
33218 3 12309
12309 2 00100
00100 1 99999
99999 5 68237
68237 6 -1

// 题目链接:https://pintia.cn/problem-sets/994805260223102976/exam/problems/994805296180871168
#include 
typedef struct {int address;//节点地址int data; //整数数据int next;//下一节点的地址
} Node;
int main() {int addr, N, K;//读取首地址,节点数量,反转个数scanf("%d %d %d", &addr, &N, &K);Node origin[100001], sort[100001];//创建初始单链表,反转后的单链表for (int i = 0; i < N; i++) {//读取节点Node temp;scanf("%d %d %d", &temp.address, &temp.data, &temp.next);origin[temp.address] = temp;}for (int i = 0; i < N; i++) {//链接节点sort[i] = origin[addr];addr = sort[i].next;//获取下一个节点的地址if (addr == -1) {N = i + 1;//可能有无效的节点,需要更新链表中节点的数量break;}}for (int i = 0; i < N / K; i++) {//反转的次数for (int j = 0; j < K / 2; j++) {//反转Node temp;temp = sort[j + i * K];sort[j + i * K] = sort[K - 1 - j + i * K];//数组下标确认好sort[K - 1 - j + i * K] = temp;}}for (int i = 0; i < N; i++) {if(i != N - 1) {sort[i].next = sort[i+1].address;printf("%05d %d %05d\n", sort[i].address, sort[i].data, sort[i].next);} else {sort[i].next = -1;printf("%05d %d %d\n", sort[i].address, sort[i].data, sort[i].next);}}return 0;
}

相关内容