3
11
2015
0

COGS 40 [NOIP1999] 回文数

#include <iostream>
#include <cstdio>
using namespace std;

string m,toc="0123456789abcdef";
int n,cnt;

string rev(string s)//翻转字符串
{
    string res="";
    for (int i=s.size();i>0;--i)
        res+=s[i-1];
    return res;
}

bool hw(string s)//判断是否为回文
{
    string s1,s2;
    s1=s.substr(0,s.size()/2);
    s2=s.substr(s.size()/2+s.size()%2,s.size()/2);
    return s1==rev(s2);
}

int main()
{
    freopen("huiwen.in","r",stdin);
    freopen("huiwen.out","w",stdout);
    cin>>n>>m;
    for (int i=0;i<m.size();++i)
        if (isalpha(m[i])) m[i]=m[i]-'a'+10;//若为字母,转化为相应数字
        else m[i]=m[i]-'0';//转化为相应数字
    string a,b; int g;
    while (cnt++<=30){
        a=rev(m); b=""; g=0;
        for (int i=0;i<m.size();++i){//高精度加法
            b+=char((a[i]+m[i]+g)%n);
            g=(a[i]+m[i]+g)/n;
        }
        if (g>0) b+=char(1);//处理进位
        m=b;
        if (hw(b)) {//若回文,输出并结束
            for (int i=0;i<b.size();++i)
                b[i]=toc[b[i]];
            cout<<b<<endl<<cnt;
            return 0;
        }
    }
    cout<<"impossible";
    return 0;
}
Category: 题解 | Tags: NOIP COGS 模拟 高精度
3
11
2015
0

COGS 588 [NOIP1999]拦截导弹

#include <cstdio>
#include <algorithm>
using namespace std;
int n,f[1001],a[1001];
int main()
{
	//freopen("missile.in","r",stdin);
	//freopen("missile.out","w",stdout);
	while (scanf("%d",&a[++n])>0);
	--n; f[1]=1; int ans=0;
	for (int i=2;i<=n;++i){//最长不下降子序列
		f[i]=1;
		for (int j=1;j<i;++j)
			if (a[j]>=a[i]) f[i]=max(f[i],f[j]+1);
		ans=max(ans,f[i]);
	}
	printf("%d\n",ans); ans=0;
	for (int i=2;i<=n;++i){//最长上升子序列
		f[i]=1;
		for (int j=1;j<i;++j)
			if (a[j]<a[i]) f[i]=max(f[i],f[j]+1);
			ans=max(ans,f[i]);
	}
	printf("%d\n",ans);
	return 0;
}
Category: 题解 | Tags: NOIP COGS 动规
3
5
2015
0

UVa 10050 Hartals

#include <iostream>
using namespace std;

int main() {
	int t,n,p,h[100],ans;
	cin>>t;
	while (t--){
		cin>>n>>p; ans=0;
		for (int i=0;i<p;++i)
			cin>>h[i];
		for (int i=1;i<=n;++i)
			if (i%7!=6 && i%7!=0)
				for (int j=0;j<p;++j)
					if (i%h[j]==0){
						++ans;
						break;
					}
		cout<<ans<<endl;//让人发火的是,最后一个输出如果不加空行会判错
	}
	return 0;
}
Category: 题解 | Tags: UVa 挑战编程 模拟

Host by is-Programmer.com | Power by Chito 1.3.3 beta | Theme: Aeros 2.0 by TheBuckmaker.com