CCF201312试题代码

试题:CCF201312试题

1.出现次数最多的数

分析:因为需要记录数字以及其出现次数,用C++STL中的map,可以简单完成

代码:

#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <map>

using namespace std;

int main()
{
	int n;
	int a;
	cin>>n;
	map<int, int>num;
	for(int i = 0; i<n; i++)
	{
		cin >> a;
		num[a]++;
	}
	int result = 0;
	int m = 0;
	for(map<int,int>::iterator it = num.begin(); it != num.end(); it++)
	{
		if(m < it->second)
		{
			m = it->second;
			result = it->first;
		}
	}
	cout << result << "\n";
	return 0;
}

2.ISBN号码

分析:用两个数组,一个存储输入字符串,一个存储数字,注意char转成int的格式和方法,还有注意题目对于模10结尾为X的要求

代码:

#include <iostream>
#include <string>
#include <cstring>
using namespace std; 
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int a[10];

int main(int argc, char** argv) {
	string s;
	cin >> s;
	a[0] = s[0] - '0';
	a[1] = s[2] - '0';
	a[2] = s[3] - '0';
	a[3] = s[4] - '0';
	a[4] = s[6] - '0';
	a[5] = s[7] - '0';
	a[6] = s[8] - '0';
	a[7] = s[9] - '0';
	a[8] = s[10] - '0';
	a[9] = s[12] - '0';
	int sum = 0;
	for(int i = 0; i<9; i++)
	{
		sum += a[i]*(i+1);
	}
	if(sum%11 == a[9])
		cout << "Right" << "\n";
	else if(sum%11 == 10)
	{
		s[12] = 'X';
		cout << s << endl;
	 } 
	else
	{
		s[12] = sum%11 + '0';
		cout << s;
		cout << "\n";
	}
	return 0;
}

3.最大的矩形

分析:暴力破解法,可以用数组或者可变长度的vector进行数据的存储,解题思路有两个,一个是算出每个数字左右总的长度,然后乘以该数字,得出面积,第二个从左边开始遍历,定住一个坐标,然后寻找数据中的最小数字并设为当前高度值,然后计算该最小数字最右边和设定坐标的坐标差值,得出长度,从而高乘长得面积。

代码:

方法一:

#include <iostream>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
const int maxn = 1000+5;
int a[maxn];
int len;
int n;

int get_lpos(int pos, int i)
{
	if(pos == 0)
		return pos;
	else{
		if(a[i] > a[pos-1])
			return pos;
		if(a[i] <= a[pos-1])
		{
			pos--;
			get_lpos(pos, i);
		}
	}
}

int get_rpos(int pos, int i)
{
	if(pos == n-1)
		return pos;
	else{
		if(a[i] > a[pos+1])
			return pos;
		if(a[i] <= a[pos-1])
		{
			pos++;
			get_rpos(pos, i);
		}
	}
}

int main(int argc, char** argv) {

	cin >> n;
	for(int i = 0; i<n; i++)
	{
		cin >> a[i];
	}
	int ans = 0;
	for(int i = 0; i<n; i++)
	{
		int lp = get_lpos(i, i);
		int rp = get_rpos(i, i);
		len = rp-lp+1;
		if(ans< a[i]*len)
			ans = a[i]*len;
	}
	cout << ans << endl;
	return 0;
}

方法二:

#include <iostream>
#include <vector>

using namespace std;

int main()
{
	int n;
	cin >> n;
	vector<int> a;
	for(int i = 0; i<n; i++)
	{
		int x;
		cin >> x;
		a.push_back(x);
	}
	int ans = 0;
	for(int i = 0; i<n; i++)
	{
		int h = a[i];
		for(int j = i; j < n; j++)
		{
			if(a[j] < h)    //不能写成(a[j]<a[i]) 
				h = a[j];
			int s = (j-i+1)*h;
			if(ans < s)
			{
				ans = s;
			}

		}
	}
	cout << ans << endl;
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值