codeforce 172 Div.2

本文介绍了使用C++解决三个典型编程问题的方法,包括字符串首字母大写、寻找最接近分数及最大异或值的高效算法实现。通过这些实例,读者可以了解到如何运用C++标准库和算法优化来提高代码效率。

wa的好high 啊。。。

A - Word Capitalization

不解释了。。。

/*************************************************************************
    > File Name: a.cpp
    > Author: withwind
    > Mail: withwind93@gmail.com 
    > Created Time: 2013/3/10 23:21:19
 ************************************************************************/

#include <limits.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <algorithm>
#include <iostream>
using namespace std;

int main()
{
	int i,j,k;
	int n;
	char s[1100];
	scanf("%s",s);
	if(s[0]>='a' && s[0]<='z')
	  s[0]=s[0]-'a'+'A';
	printf("%s\n",s);
	return 0;

}


B - Nearest Fraction

b直接1到n暴力求解。a 在范围t=(x*1.0/(y*1.0))*i,[t+2,t-2]内,求解下最小值。。坑爹。。。

/*************************************************************************
    > File Name: b.cpp
    > Author: withwind
    > Mail: withwind93@gmail.com 
    > Created Time: 2013/3/10 23:21:27
 ************************************************************************/

#include <limits.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <algorithm>
#include <iostream>
using namespace std;

int main()
{
	int i,j,k;
	int x,y,a,b;
	int n;
	double min=1000000.0;
	scanf("%d %d %d",&x,&y,&n);
	double x1 = (x*1.0)/(y*1.0);

	double	tmin = x1;
	for(i = n;i>=1;i--)
	{
		int t = (x*1.0/(y*1.0))*i+0.5;
		j=t+2;
		for( ;j>=t-2 ;j--)
		{
			if(j<0)	break;
			double tmp = fabs(x1-(j*1.0)/(i*1.0));
			if(tmp<=tmin)
			{
				tmin = tmp;
		//		printf("tmin  = %llf\n",tmin);
				a = j;
				b = i;
			}
		}
	}
	printf("%d/%d\n",a,b);
	return 0;
}


D - Maximum Xor Secondary

看别人代码学到的。。。

/*************************************************************************
    > File Name: d.cpp
    > Author: withwind
    > Mail: withwind93@gmail.com 
    > Created Time: 2013/3/11 13:28:36
 ************************************************************************/

#include <limits.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <algorithm>
#include <iostream>
using namespace std;
int a[100010],stack[100010];
int main()
{
	int i,j,k;
	int n;
	int ans = 0;
	scanf("%d",&n);
	for(i = 1;i<=n;i++)
	{
		scanf("%d",&a[i]);
	}
	int top;
	/*正向搜索,将递减的数字放入stack中,所以当a>stack时候,stack那一位数开始到a,a为最大,stack为第二大*/
	top = 0;
	stack[++top] = a[1];
	for(i = 2;i<= n;i++)
	{
		while(top >=1 && a[i]>stack[top])
		{
			ans = max(ans,a[i]^stack[top]);
			top--;
		}
		stack[++top] = a[i];/*上层循环之后,a[i]必定为stack中最小值*/
	}
	/*方向搜索,将递减的数字放入stack中,所以当a>stack时候,stack那一位数开始到a,a为最大,stack为第二大*/
	top = 0;
	stack[++top] = a[n];
	for(i = n;i>=1;i--)
	{
		while(top >=1 && a[i]>stack[top])
		{
			ans = max(ans,a[i]^stack[top]);
			top--;
		}
		stack[++top] = a[i];/*上层循环之后,a[i]必定为stack中最小值*/
	}
	printf("%d\n",ans);
	return 0;
}


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值