上机笔记5.1

本文解析了PAT系列的四个问题:PATB1003字符串规则验证、PATB1049/A1104数列片段和子串和计算、PATA1008电梯行程计时、PATA1049计数1的个数。涉及正则表达式匹配、数列操作及算法优化。
摘要由CSDN通过智能技术生成

*5.1 PAT B1003

答案正确”是自动判题系统给出的最令人欢喜的回复。本题属于 PAT 的“答案正确”大派送 —— 只要读入的字符串满足下列条件,系统就输出“答案正确”,否则输出“答案错误”。

得到“答案正确”的条件是:

  1. 字符串中必须仅有 PAT这三种字符,不可以包含其它字符;
  2. 任意形如 xPATx 的字符串都可以获得“答案正确”,其中 x 或者是空字符串,或者是仅由字母 A 组成的字符串;
  3. 如果 aPbTc 是正确的,那么 aPbATca 也是正确的,其中 abc 均或者是空字符串,或者是仅由字母 A 组成的字符串。

现在就请你为 PAT 写一个自动裁判程序,判定哪些字符串是可以获得“答案正确”的。

输入格式:

每个测试输入包含 1 个测试用例。第 1 行给出一个正整数 n (≤10),是需要检测的字符串个数。接下来每个字符串占一行,字符串长度不超过 100,且不包含空格。

输出格式:

每个字符串的检测结果占一行,如果该字符串可以获得“答案正确”,则输出 YES,否则输出 NO

输入样例:

10
PAT
PAAT
AAPATAA
AAPAATAAAA
xPATx
PT
Whatever
APAAATAA
APT
APATTAA

输出样例:

YES
YES
YES
YES
NO
NO
NO
NO
NO
NO
#include<stdio.h>
#include<string.h>

int main() {
	int n;
	
	scanf("%d", &n);
	
	while (n--) {
		char str[110];
		scanf("%s", str);
		int len = strlen(str);
		int num_p = 0, num_t = 0, other = 0;
		int loc_p = -1, loc_t = -1;
		for (int i = 0; i < len; i++) {
			if (str[i] == 'P') {
				num_p++;
				loc_p = i;
			}
			else if (str[i] == 'T') {
				num_t++;
				loc_t = i;
			}
			else if (str[i] != 'A') {
				other++;
			}
		}
		if ((num_p != 1) || (num_t != 1) || (other != 0) || (loc_t - loc_p <= 1)) {
			printf("N0\n");
			continue;
		}
		int x = loc_p, y = loc_t - loc_p - 1, z = len - loc_t - 1;
		if (z - x * (y - 1) == x) {
			printf("YES\n");
		}
		else {
			printf("NO\n");
		}
	}
	return 0;
}

*5.1 PAT B1049/A1104

B1049

给定一个正数数列,我们可以从中截取任意的连续的几个数,称为片段。例如,给定数列 { 0.1, 0.2, 0.3, 0.4 },我们有 (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.4) 这 10 个片段。

给定正整数数列,求出全部片段包含的所有的数之和。如本例中 10 个片段总和是 0.1 + 0.3 + 0.6 + 1.0 + 0.2 + 0.5 + 0.9 + 0.3 + 0.7 + 0.4 = 5.0。

输入格式:

输入第一行给出一个不超过 105 的正整数 N,表示数列中数的个数,第二行给出 N 个不超过 1.0 的正数,是数列中的数,其间以一个空格分隔。

输出格式:

在一行中输出该序列所有片段包含的数之和,精确到小数点后 2 位。

输入样例:

4
0.1 0.2 0.3 0.4

输出样例:

5.00

A1104

Given a sequence of positive numbers, a segment is defined to be a consecutive subsequence. For example, given the sequence { 0.1, 0.2, 0.3, 0.4 }, we have 10 segments: (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) and (0.4).

Now given a sequence, you are supposed to find the sum of all the numbers in all the segments. For the previous example, the sum of all the 10 segments is 0.1 + 0.3 + 0.6 + 1.0 + 0.2 + 0.5 + 0.9 + 0.3 + 0.7 + 0.4 = 5.0.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N, the size of the sequence which is no more than 105. The next line contains N positive numbers in the sequence, each no more than 1.0, separated by a space.

Output Specification:

For each test case, print in one line the sum of all the numbers in all the segments, accurate up to 2 decimal places.

Sample Input:

4
0.1 0.2 0.3 0.4

Sample Output:

5.00
//17分,没找到哪里有问题
//找到了,精度问题
#include<stdio.h>

int main() {
	int n;
	scanf("%d", &n);
	double out = 0, temp;
	for (int i = 0; i < n; i++) {
		scanf("%lf", &temp);
		out += temp * (i + 1) * (n - i);
	}
	printf("%.2f\n", out);
	return 0;
}
//知乎大佬
//10^5的情况,得将sum转换为long long且扩大1000倍,来多保存小数点后的位数
#include <iostream>
using namespace std;
int main()
{
    long long ans = 0;
    double tmp = 0.0;
    int n;
    cin >> n;
    for(int i=0;i<n;++i){
        cin >> tmp;
        ans += (long long)(i+1)*(long long)(n-i)*(long long)(tmp*1000);
    }
    printf("%.2lf\n", ans/1000.0);
}

*5.1 PAT A1008

The highest building in our city has only one elevator. A request list is made up with N positive numbers. The numbers denote at which floors the elevator will stop, in specified order. It costs 6 seconds to move the elevator up one floor, and 4 seconds to move down one floor. The elevator will stay for 5 seconds at each stop.

For a given request list, you are to compute the total time spent to fulfill the requests on the list. The elevator is on the 0th floor at the beginning and does not have to return to the ground floor when the requests are fulfilled.

Input Specification:

Each input file contains one test case. Each case contains a positive integer N, followed by N positive numbers. All the numbers in the input are less than 100.

Output Specification:

For each test case, print the total time on a single line.

Sample Input:

3 2 3 1

Sample Output:

41
//数组法
#include<stdio.h>

int main() {
	int n, time = 0;
	scanf("%d", &n);
	int arr[110];
	for (int i = 0; i < n; i++) {
		scanf("%d", &arr[i]);
	}
	time = arr[0] * 6 + 5;
	for (int i = 1; i < n; i++) {
		if (arr[i] > arr[i - 1]) {
			time += 6 * (arr[i] - arr[i - 1]) + 5;
		}
		else {
			time += 4 * (arr[i - 1] - arr[i]) + 5;
		}
	}
	printf("%d\n", time);
	return 0;

}
//算法笔记
#include<stdio.h>

int main() {
	int n, time = 0, now = 0, to;
	scanf("%d", &n);
	for (int i = 0; i < n; i++) {
		scanf("%d", &to);
		if (to > now) {
			time += ((to - now) * 6);
		}
		else {
			time += ((now - to) * 4);
		}
		time += 5;
		now = to;
	}

	printf("%d\n", time);
	return 0;
}

? *5.1 PAT A1049

The task is simple: given any positive integer N, you are supposed to count the total number of 1’s in the decimal form of the integers from 1 to N. For example, given N being 12, there are five 1’s in 1, 10, 11, and 12.

Input Specification:

Each input file contains one test case which gives the positive N (≤230).

Output Specification:

For each test case, print the number of 1’s in one line.

Sample Input:

12

Sample Output:

5
#include<stdio.h>

int main() {
	int n, a = 1, ans = 0;
	int left, now, right;
	scanf("%d", &n);
	while (n / a != 0) {
		left = n / (a * 10);
		now = n / a % 10;
		right = n % 10;
		if (now == 0)
			ans += left * a;
		else if (now == 1)
			ans += left * a + right + 1;
		else
			ans += (left + 1) * a;
		a *= 10;
	}
	printf("%d\n", ans);
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值