《算法笔记》练习(一)

1. PAT1032B

了用事实说明挖掘机技术到底哪家强,PAT 组织了一场挖掘机技能大赛。现请你根据比赛结果统计出技术最强的那个学校。

输入格式:

输入在第 1 行给出不超过 10​5​​ 的正整数 N,即参赛人数。随后 N 行,每行给出一位参赛者的信息和成绩,包括其所代表的学校的编号(从 1 开始连续编号)、及其比赛成绩(百分制),中间以空格分隔。

输出格式:

在一行中给出总得分最高的学校的编号、及其总分,中间以空格分隔。题目保证答案唯一,没有并列。

输入样例:

6
3 65
2 80
1 100
2 70
3 40
3 0

输出样例:

2 150
#include<cstdio>
#include<iostream>
using namespace std;
const int maxn = 100010;
int arr[maxn] = {0};
int main() {
	int n,score,sId;
	cin>>n;
	int i=0;
	for(;i<n;i++){
		cin>>sId>>score;
		arr[sId]+=score;
	}
	int k=1,maxS=-1;
	for(int j=1;j<=n;j++){
		if(arr[j]>maxS){
			maxS=arr[j];
			k=j;
		}
	}
	cout<<k<<" "<<maxS<<endl;
	return 0;
}

段错误,数组越界

2.  Codeup1934B

题目描述

输入一个数n,然后输入n个数值各不相同,再输入一个值x,输出这个值在这个数组中的下标(从0开始,若不在数组中则输出-1)。

输入

测试数据有多组,输入n(1<=n<=200),接着输入n个数,然后输入x。

输出

对于每组输入,请输出结果。

样例输入

4
1 2 3 4
3

样例输出

2
#include<iostream>
#include<cstdio>
using namespace std;
int arr[201];
int main(){
//	int arr[201] = {0};
	int n = 0;
	cin>>n;
    while(n!=EOF){
	    for(int i=0;i<n;i++){
		    cin>>arr[i];
    	}
    }
	int key = 0;
	cin>>key;
	int j = 0;
	for(;j<n;j++){
		if(arr[j]==key){
			break;
		}
	}
	if(j==n){
		j=-1;
	}
	cout<<j<<endl;
	return 0;
}

一直报0.5的错误,不知道哪里存在问题

3.PAT B 1036

美国总统奥巴马不仅呼吁所有人都学习编程,甚至以身作则编写代码,成为美国历史上首位编写计算机代码的总统。2014 年底,为庆祝“计算机科学教育周”正式启动,奥巴马编写了很简单的计算机代码:在屏幕上画一个正方形。现在你也跟他一起画吧!

输入格式:

输入在一行中给出正方形边长 N(3≤N≤20)和组成正方形边的某种字符 C,间隔一个空格。

输出格式:

输出由给定字符 C 画出的正方形。但是注意到行间距比列间距大,所以为了让结果看上去更像正方形,我们输出的行数实际上是列数的 50%(四舍五入取整)。

输入样例:

10 a

输出样例:

aaaaaaaaaa
a        a
a        a
a        a
aaaaaaaaaa
#include<cstdio>
#include<iostream>
#include<cmath>
using namespace std;
int main(){
	double row;
	cin>>row;
	char c;
	cin>>c;
	double line=round(row/2.0);
	//第一行输出列数个字符 
	for(int i=0;i<row;i++){
		cout<<c;
	}
	//第二行到倒数第一行仅第一列和最后一列输出字符 
	for(int i=1;i<line-1;i++){
		cout<<endl;
		cout<<c;
		for(int j=1;j<row-1;j++){
			cout<<" ";
		}
		cout<<c;
	}
	cout<<endl;
	for(int i=0;i<row;i++){
		cout<<c;
	}
	return 0;
}

 4.Codup1928

题目描述

有两个日期,求两个日期之间的天数,如果两个日期是连续的我们规定他们之间的天数为两天。

输入

有多组数据,每组数据有两行,分别表示两个日期,形式为YYYYMMDD

输出

每组数据输出一行,即日期差值

样例输入

20130101
20130105

样例输出

5
#include<cstdio>
#include<iostream>
using namespace std;
//表示每个月天数,第二维为0时是平年
int month[12][2]={{0,0},{31,31},{28,29},{31,31},{30,30},{31,31},{30,30},{31,31},{31,31},{30,30},{31,31},{30,30},{31,31}} ;
int isLeap(int year){
	if((year/4 == 0)&&(year/100 == 0)&&year/400 != 0){
		return 1;
	}
	else return 0;
}
int main(){
	int temp,temp1,temp2;
	cin>> temp1>>temp2;
	if(temp1>temp2){
		temp = temp1;
		temp1 = temp2;
		temp2 = temp;
	}
	int year1,month1,date1,year2,month2,date2;
	year1 = temp1/10000;
	month1 = (temp1%10000)/100;
	date1 = (temp1%100);
	year2 = temp2/10000;
	month2 = (temp2%10000)/100;
	date2 = (temp2%100);
	//标记累加日期数
	//int count=0;
	//相邻两天之间天数为两天 
	int count = 1; 
	// 通过使 temp1==temp2,获得差值
	while(year1<year2||month1<month2||date1<date2){
		date1++;
		//相加后的日期数满足该月天数,变为下一个月的第一天 
		if(date1 == month[month1][isLeap(year1)]){
			month1++;
			date1 = 1;
		}
		//如果月份数超过12,即为下一年的1月 
		if(month1 ==13){
			year1++;
			month1 = 1;
		}
		count++;
	}
	cout<<count<<endl;
	return 0;
} 

又是50%的错误率,啊,要疯了

5. PAT1022B

输入两个非负 10 进制整数 A 和 B (≤2​30​​−1),输出 A+B 的 D (1<D≤10)进制数。

输入格式:

输入在一行中依次给出 3 个整数 A、B 和 D。

输出格式:

输出 A+B 的 D 进制数。

输入样例:

123 456 8

输出样例:

1103
#include<cstdio>
#include<iostream>
using namespace std;
int main(){
	//十进制数a,b,d进制数 
	int a,b,d;
	cin>>a>>b>>d; 
	int sum = a+b;
	//存放取余的结果 
	int arr[31]={0};
	//存放结果位数 
	int ans=0; 
	//sum之和取余d 
	while(sum != 0){
		arr[ans++]=sum%d;
		sum=sum/d;
	}
	for(int i=ans-1;i>=0;i--){
		cout<<arr[i];
	}
	return 0;
}

 部分正确,扣了2分

修改,把while改成do while

发布了43 篇原创文章 · 获赞 4 · 访问量 6146

猜你喜欢

转载自blog.csdn.net/didadu/article/details/95376386
今日推荐