杭電OJ1003 Max Sum
大意:
算幾個數的連續子序列的最大值,這讓人想到了dp的連續不下降子序列,雖然兩個並沒什麼大的關係。
幾個數相加的最大值,算和,從起點爲第一個數算起,一直相加到後面,令最大值mmax等於一個很小的數,讓mmax和相加的數作比較,當相加的數大於mmax時,進行值的替換,和前後下標數的改變。
當相加的數和小於0時,我們就知道了和小於0,則再往後即使會往大走也不會是最大值的子序列,而如果下標一個一個的加一,那麼它一直邁不過那個坎,就是那個很大的複數坎,所以應該改變起始下標,使下標在當前位置加一,繼續以這個下標計算其的最大和值。
過程:細節錯了,大意,之前寫的沒改過來,之後是presentation error ,因爲沒加換行符。
#include<iostream>
#include<algorithm>
#include<cstring>
const int maxn=100010;
int s[maxn];
using namespace std;
int main()
{
int n,t;
cin>>t;
n=t;
int num=1;
while(n--)
{
int mmax=-1010;
int a,b,sum=0;
cin>>a;
int qi,ten=1,mo;
for(int i=1;i<=a;i++)
{
cin>>b;
sum+=b;
if(sum>mmax)
{
mmax=sum;
qi=ten;
mo=i;
}
if(sum<0)
{
ten=i+1;
sum=0;
}
}
cout<<"Case "<<num<<":"<<endl;
cout<<mmax<<" "<<qi<<" "<<mo;
if(num!=t)cout<<endl;
cout<<endl;
num++;
}
}
杭電oj 1004 Let the Balloon Rise
大意:
用strcmp進行單詞比較,char a【】【】存儲多個字符串,char a【i】爲字符串i的地址,strcmp對地址進行比較,cout寫地址輸出字符串。
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
if(n)
{
char a[1005][16];
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
int t[1005];
memset(t,0,sizeof(t));
for(int i=1;i<=n;i++)
{
for(int j=1;j<=i;j++)
{
if(strcmp(a[i],a[j])==0)
{
t[i]+=1;
}
}
}
int mmax=t[1];int xia=1;
for(int i=1;i<=n;i++)
{
if(mmax<t[i])
{
xia=i;
mmax=t[i];
}
}
cout<<a[xia]<<endl;
}
}
}