2021-09-28 PAT甲级(2) C语言

目录

1008 Elevator (20 分)

Input Specification:

Output Specification:

Sample Input:

Sample Output:

答案: 

1120 Friend Numbers (20 分)

Input Specification:

Output Specification:

Sample Input:

Sample Output:

答案: 

1113 Integer Set Partition (25 分)

Input Specification:

Output Specification:

Sample Input 1:

Sample Output 1:

Sample Input 2:

Sample Output 2:

答案(不完全正确): 

题目大意:

设计思路:


拖了好久啊...不愿面对...只能先做简单题了,数据结构搞不懂,先多练其他题吧。

1008 Elevator (20 分)

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>
#include <stdlib.h>

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

设置to和now两个变量,到后就将now=to。

1120 Friend Numbers (20 分)

Two integers are called "friend numbers" if they share the same sum of their digits, and the sum is their "friend ID". For example, 123 and 51 are friend numbers since 1+2+3 = 5+1 = 6, and 6 is their friend ID. Given some numbers, you are supposed to count the number of different friend ID's among them.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N. Then N positive integers are given in the next line, separated by spaces. All the numbers are less than 104.

Output Specification:

For each case, print in the first line the number of different friend ID's among the given integers. Then in the second line, output the friend ID's in increasing order. The numbers must be separated by exactly one space and there must be no extra space at the end of the line.

Sample Input:

8
123 899 51 998 27 33 36 12

Sample Output:

4
3 6 9 26

答案: 

#include <stdio.h>
#include <stdlib.h>

int main(void){
	int N,num;
    
	//数组初始化不同类型在PAT专题(1)中有 
    //37是因为最大数为 9999,最大的证号为4*9 = 36
    //数组功能为:下标是朋友证号,若有相同的证号,则frined[i]存在,有几次存在count就+几次。
	int friends[37] = {0};
	int total,count=0;
	
	scanf("%d",&N);
	
	for(int i=0;i<N;i++){
		scanf("%d",&num);
        //求证号
        for (total = 0;num; num /= 10)
                total += num % 10;
		if(!friends[total]){
			friends[total] =1;
			count++;
		}
	}
	printf("%d\n",count);
	for(int i=1;i<37;i++){
		if(friends[i]){
            //格式要求最后一个输出无空格
			 printf("%d%s", i, --count ? " " : "");
		}
	}
}

注释仔细阅读。

1113 Integer Set Partition (25 分)

Given a set of N (>1) positive integers, you are supposed to partition them into two disjoint sets A1​ and A2(分成两个集合A1,A2)​ of n1​ and n2​ numbers(每个集合有n1,n2个), respectively. Let S1​ and S2​ denote the sums of all the numbers in A1​ and A2(S1,S2是两个集合的元素和的差的绝对值)​, respectively. You are supposed to make the partition so that ∣n1​−n2​∣ is minimized first, and then ∣S1​−S2​∣ is maximized.

Input Specification:

Each input file contains one test case. For each case, the first line gives an integer N (2≤N≤105), and then N positive integers follow in the next line, separated by spaces. It is guaranteed that all the integers and their sum are less than 231.

Output Specification:

For each case, print in a line two numbers: ∣n1​−n2​∣ and ∣S1​−S2​∣, separated by exactly one space.

Sample Input 1:

10
23 8 10 99 46 2333 46 1 666 555

Sample Output 1:

0 3611

Sample Input 2:

13
110 79 218 69 3721 100 29 135 2 6 13 5188 85

Sample Output 2:

1 9359

答案(不完全正确): 

#include <stdio.h>
#include <stdlib.h>

int main(void){
	int N;
	int a[100010];
	int t;
	scanf("%d",&N);
	for(int i=0;i<N;i++){
		scanf("%d",&a[i]);
	}
	
	//选择排序 
	for(int i=0;i<N-1;i++){
		for(int j=i+1;j<N;j++){
			if(a[i] > a[j]){
				t = a[i];
				a[i] = a[j];
				a[j] = t;
			}
		}
	}
	
	int b[100000],sum1=0;
	int c[100000],sum2=0;
	for(int i=0;i<N/2;i++){
		b[i] = a[i];
		sum1 = sum1 + b[i];
	}
	for(int i=N/2;i<N;i++){
		c[i] = a[i];
		sum2 = sum2 + c[i];
	}
	
	//判断偶数 
	if(N%2 == 0){
		printf("0");
	}else{
		printf("1"); 
	}
	
	int sum=0;
	if(sum1 > sum2){
		sum = sum1 - sum2;
	}else{
		sum = sum2 - sum1;
	}
	printf(" %d",sum);
}

题目大意:

输入 N 个数,分为两个集合,使两个集合元素个数相差最小,元素之和相差最大

输出相差个数 ∣n1​​−n2​​∣,元素和的差 ∣S1​​−S​2​​∣


设计思路:

元素排序,前一半为集合 1,后一半为集合 2,计算输出

这题不完全正确,有两个运行超时的测试点,不知道怎么改了...

题目理解。这题不难,但是题没读懂。

选择排序。

偶数判断。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值