A
快排,注意输出空格
#include<stdio.h>
#include<stdlib.h>
int mycopy(void const* pt, void const* pt1);
int main()
{
int num,site;
while (scanf("%d%d", &num,&site) != EOF)
{
int ar[3001];
int tar[4600001] = { 0 };
for (int i = 1; i <= num; i++)scanf("%d", &ar[i]);
long int tot = 1;
for (int i = 1; i < num; i++) {
for (int j = 1 + i; j <= num; j++) {
tar[tot] = ar[i] + ar[j];
tot++;
}
}
qsort(tar + 1, tot - 1, sizeof(int), mycopy);
for (int i = 1; i <= site; i++) {
printf("%d", tar[i]);
if (i != site)printf(" ");
}
printf("\n");
}
}
int mycopy(void const* pt, void const* pt1)
{
const int* st = (const int)pt;
const int
st1 = (const int)pt1;
return *st1 - *st;
}
B
排序,但应注意是稳定,注意题目条件
#include<stdio.h>
#include<string.h>
typedef struct stu { char student[51]; int score; }Stu;
int main()
{
Stu tar[301],ar[301];
int num;
while (scanf("%d", &num) != EOF)
{
int stat1 = 1, stat2 = 1;
for (int i = 1; i <= num; i++) {
scanf("%s%d", tar[i].student, &tar[i].score);
}
for (int i = 1; i <= num; i++) {
scanf("%s%d", ar[i].student, &ar[i].score);
if (i>1&&ar[i].score > ar[i - 1].score)stat1 = 0;
}
for (int i = 1; i < num; i++) {
for (int j = 1; j <= num-i; j++) {
if (tar[j].score < tar[j+1].score) {
int temp1=tar[j].score;
tar[j].score = tar[j+1].score;
tar[j+1].score = temp1;
char temp2[51];
strcpy(temp2, tar[j].student);
strcpy(tar[j].student, tar[j+1].student);
strcpy(tar[j+1].student, temp2);
}
}
}
if (stat1 == 0) {
printf("Error\n");
for (int i = 1; i <= num; i++)printf("%s %d\n", tar[i].student, tar[i].score);
}
else {
for (int i = 1; i <= num; i++) {
if (strcmp(tar[i].student, ar[i].student) != 0 || tar[i].score != ar[i].score)stat2 = 0;
}
if (stat2 == 1)printf("Right\n");
else {
printf("Not Stable\n");
for (int i = 1; i <= num; i++) {
printf("%s %d\n", tar[i].student, tar[i].score);
}
}
}
}
}
C
主要运用了标准函数库。
#include<stdio.h>
#include<string.h>
typedef struct stu { char name[20]; char lasts[20]; char nexts[20]; }Stu;
int mycomparemin(Stu a, Stu b);
int mycomparemax(Stu a, Stu b);
int main()
{
int n;
scanf("%d", &n);
while (n--)
{
Stu maxss, minss;
int num;
scanf("%d", &num);
scanf("%s %s %s", maxss.name, maxss.lasts, maxss.nexts);
strcpy(minss.name, maxss.name);
strcpy(minss.lasts, maxss.lasts);
strcpy(minss.nexts, maxss.nexts);
for (int i = 2; i <= num; i++) {
Stu temp;
scanf("%s %s %s", temp.name, temp.lasts, temp.nexts);
if (mycomparemin(temp, minss) == 1) {
strcpy(minss.name, temp.name);
strcpy(minss.lasts, temp.lasts);
}
if (mycomparemax(temp, maxss) == 1) {
strcpy(maxss.name, temp.name);
strcpy(maxss.nexts, temp.nexts);
}
}
printf("%s %s\n", minss.name, maxss.name);
}
}
int mycomparemin(Stu a, Stu b)
{
if (strcmp(a.lasts, b.lasts) < 0)return 1;
else return 0;
}
int mycomparemax(Stu a, Stu b)
{
if (strcmp(a.nexts, b.nexts) > 0)return 1;
else return 0;
}
i
用了前缀和+快排。
#include<stdio.h>
#include<stdlib.h>
int mycomp(const void
pt, const void* pt1);
int main()
{
int m, n;
while (scanf("%d%d", &m, &n) != EOF)
{
int tar[20001] = { 0 };
for (int i = 1; i <= m + n; i++)scanf("%d", &tar[i]);
qsort(tar + 1, m+n, sizeof(int), mycomp);
printf("%d", tar[1]);
for (int i = 2; i <= m + n; i++) {
if(tar[i]!=tar[i-1])printf(" %d", tar[i]);
}
printf("\n");
}
}
int mycomp(const void* pt, const void* pt1)
{
const int* st = (const int)pt;
const int
st1 = (const int*)pt1;
return *st - *st1;
}
H
应注意的是括号本身是否得当,而不应仅仅注意是否等号数量满足题意
#include<stdio.h>
#include<string.h>
int main()
{
char ar[2010];
scanf("%s", ar);
int l = strlen(ar);
int lefts, rights;
lefts = rights = 0;
for (int i = 0; i < l; i++) {
if (ar[i] == '(')lefts++;
else if (ar[i] == ')')rights++;
if (lefts < rights) {
printf("NO");
return;
}
}
if (lefts == rights)printf("YES");
else printf("NO");
}
K
没啥好的技巧,读完直接处理了.
#include<stdio.h>
int main()
{
long int num;
while (scanf("%ld", &num) != EOF)
{
long int ar[100000] = { 0 };
for (long int i = 1; i <= num; i++) {
long int temp;
scanf("%ld", &temp);
ar[temp] += 1;
}
long int judges = (num + 1) / 2;
for (long int i = 1; i <= 100000; i++) {
if (ar[i] >= judges) {
printf("%ld\n", i);
break;
}
}
}
}
N
也没啥好说的.就单纯的处理了-,-
#include<stdio.h>
int main()
{
int n;
scanf("%d", &n);
while (n--)
{
int num;
scanf("%d", &num);
int ar[1001] = { 0 };
for (int i = 1; i <= num; i++) scanf("%d", &ar[i]);
int lefts = 1, rights = num, step = 0;
long long int Alice = 0, Bob = 0, site = 0;
for (; lefts <= rights;) {
if (lefts <= rights) {
long long int temp = 0;
for (; lefts <= rights && temp <= site;) {
temp += ar[lefts++];
}
site = temp;
Alice += site;
step++;
}
if (lefts <= rights) {
long long int temp = 0;
for (; lefts <= rights&&temp<=site;) {
temp += ar[rights--];
}
site = temp;
Bob += site;
step++;
}
}
printf("%d %lld %lld\n", step, Alice, Bob);
}
}