mirror of
https://github.com/xlucn/PAT.git
synced 2026-11-02 18:37:42 +08:00
add 1045 1054
This commit is contained in:
1 parent
0b07f6a1ba
commit
3970479bbc
2 files changed
+151
No files matched your search
@@ -0,0 +1,61 @@
|
||||
/**
|
||||
* 1045. 快速排序(25)
|
||||
*
|
||||
* 著名的快速排序算法里有一个经典的划分过程:我们通常采用某种方法取一个元素作为主元,
|
||||
* 通过交换,把比主元小的元素放到它的左边,比主元大的元素放到它的右边。 给定划分后的
|
||||
* N个互不相同的正整数的排列,请问有多少个元素可能是划分前选取的主元?
|
||||
*
|
||||
* 例如给定N = 5, 排列是1、3、2、4、5。则:
|
||||
*
|
||||
* 1的左边没有元素,右边的元素都比它大,所以它可能是主元;
|
||||
* 尽管3的左边元素都比它小,但是它右边的2它小,所以它不能是主元;
|
||||
* 尽管2的右边元素都比它大,但其左边的3比它大,所以它不能是主元;
|
||||
* 类似原因,4和5都可能是主元。
|
||||
* 因此,有3个元素可能是主元。
|
||||
*
|
||||
* 输入格式:
|
||||
*
|
||||
* 输入在第1行中给出一个正整数N(<= 105); 第2行是空格分隔的N个不同的正整数,每个数
|
||||
* 不超过109。
|
||||
*
|
||||
* 输出格式:
|
||||
*
|
||||
* 在第1行中输出有可能是主元的元素个数;在第2行中按递增顺序输出这些元素,其间以1个空
|
||||
* 格分隔,行末不得有多余空格。
|
||||
*
|
||||
* 输入样例:
|
||||
* 5
|
||||
* 1 3 2 4 5
|
||||
* 输出样例:
|
||||
* 3
|
||||
* 1 4 5
|
||||
*/
|
||||
#include <stdio.h>
|
||||
int main()
|
||||
{
|
||||
int N, count = 0;
|
||||
int array[100000], lmax[100000], rmin[100000];
|
||||
|
||||
scanf("%d", &N);
|
||||
for(int i = 0; i < N; i++) scanf("%d", array + i);
|
||||
|
||||
for(int i = 0, max = i; i < N; i++)
|
||||
lmax[i] = array[i] >= array[max] ? array[max = i] : array[max];
|
||||
for(int i = N - 1, min = i; i >= 0; i--)
|
||||
rmin[i] = array[i] <= array[min] ? array[min = i] : array[min];
|
||||
|
||||
for(int i = 0; i < N; i++)
|
||||
{
|
||||
if(array[i] == lmax[i] && array[i] == rmin[i])
|
||||
count++;
|
||||
else
|
||||
array[i] = 0;
|
||||
}
|
||||
|
||||
printf("%d\n", count);
|
||||
for(int i = 0; i < N && count; i++) if(array[i])
|
||||
printf("%d%c", array[i], --count ? ' ' : '\0');
|
||||
printf("\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
/**
|
||||
* 1054. 求平均值 (20)
|
||||
*
|
||||
* 本题的基本要求非常简单:给定N个实数,计算它们的平均值。但复杂的是有些输入数据可能
|
||||
* 是非法的。一个“合法”的输入是[-1000,1000]区间内的实数,并且最多精确到小数点后2位。
|
||||
* 当你计算平均值的时候,不能把那些非法的数据算在内。
|
||||
*
|
||||
* 输入格式:
|
||||
*
|
||||
* 输入第一行给出正整数N(<=100)。随后一行给出N个实数,数字间以一个空格分隔。
|
||||
*
|
||||
* 输出格式:
|
||||
*
|
||||
* 对每个非法输入,在一行中输出“ERROR: X is not a legal number”,其中X是输入。最后
|
||||
* 在一行中输出结果:“The average of K numbers is Y”,其中K是合法输入的个数,Y是它
|
||||
* 们的平均值,精确到小数点后2位。如果平均值无法计算,则用“Undefined”替换Y。如果K为
|
||||
* 1,则输出“The average of 1 number is Y”。
|
||||
*
|
||||
* 输入样例1:
|
||||
* 7
|
||||
* 5 -3.2 aaa 9999 2.3.4 7.123 2.35
|
||||
* 输出样例1:
|
||||
* ERROR: aaa is not a legal number
|
||||
* ERROR: 9999 is not a legal number
|
||||
* ERROR: 2.3.4 is not a legal number
|
||||
* ERROR: 7.123 is not a legal number
|
||||
* The average of 3 numbers is 1.38
|
||||
* 输入样例2:
|
||||
* 2
|
||||
* aaa -9999
|
||||
* 输出样例2:
|
||||
* ERROR: aaa is not a legal number
|
||||
* ERROR: -9999 is not a legal number
|
||||
* The average of 0 numbers is Undefined
|
||||
*/
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
int main()
|
||||
{
|
||||
int count = 0, N;
|
||||
double f, sum = 0;
|
||||
/* Maxium scenario: -1000.00. So just need to read 8 chars(+ '\0' = 9) */
|
||||
char strnum[9], *pEnd, *pDot, c;
|
||||
|
||||
scanf("%d", &N);
|
||||
for(int i = 0; i < N; i++)
|
||||
{
|
||||
/* Just read 8 chars */
|
||||
scanf("%8s", strnum);
|
||||
|
||||
/* If there is still non-white chars following, it is not legal */
|
||||
if(!isspace(ungetc(getchar(), stdin)))
|
||||
{
|
||||
printf("ERROR: %s", strnum);
|
||||
/* this can avoid array overflow(we don't know how long input is) */
|
||||
while(!isspace(c = getchar())) putchar(c);
|
||||
printf(" is not a legal number\n");
|
||||
continue;
|
||||
}
|
||||
|
||||
/* If this string is not a floating number, pEnd won't point to '\0' */
|
||||
f = strtod(strnum, &pEnd);
|
||||
/* convertion has left some chars or number is out of range */
|
||||
if(*pEnd || f > 1000 || f < -1000)
|
||||
{
|
||||
printf("ERROR: %s is not a legal number\n", strnum);
|
||||
continue;
|
||||
}
|
||||
|
||||
/* if the precision is more than two digits, it is not legal */
|
||||
pDot = strchr(strnum, '.');
|
||||
if(pDot && pDot - strnum < strlen(strnum) - 3)
|
||||
{
|
||||
printf("ERROR: %s is not a legal number\n", strnum);
|
||||
continue;
|
||||
}
|
||||
|
||||
/* legel number */
|
||||
count++;
|
||||
sum += f;
|
||||
}
|
||||
|
||||
if(count == 0) printf("The average of 0 numbers is Undefined\n");
|
||||
if(count == 1) printf("The average of 1 number is %.2lf", sum);
|
||||
if(count >= 2) printf("The average of %d numbers is %.2lf", count, sum / count);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user