標題:
c462: apcs 交錯字串 解答
[打印本頁]
作者:
ray
時間:
2020-7-11 11:15
標題:
c462: apcs 交錯字串 解答
本帖最後由 ray 於 2020-7-11 11:16 編輯
#include <iostream>
#include <string>
using namespace std;
int main()
{
int k,max,l,len,type;
string str;
cin>>k>>str;
max = l = len = 0;
for(int i=0;i<str.length();i++)
{
int t = isupper(str[i]);
if(i == 0)
type = t;
if(t == type)
{
l++;
if(l > k)
len = k;
//cout<<str[i]<<"==/"<<endl;
}
else
{
if(l < k)
len=0;
l = 1;
//cout<<str[i]<<"<>/"<<endl;
}
if(l == k)
len += k;
if(len > max)
max = len;
//cout<<"len="<<len<<"/l="<<l<<endl;
type = t;
}
cout<<max<<endl;
//system("pause");
return 0;
}
複製代碼
作者:
陳泓瑜
時間:
2020-7-11 11:32
#include<iostream>
#include<cstdlib>
#include<string>
using namespace std;
int main()
{
int k, max=0, kl=0, type, ts, now=0;//ts=char type, type = switch, now -> max, kl -> k
string s;
cin>>k>>s;
for(int i = 0; i < s.length(); i++)
{
ts = isupper(s[i]);
if(i == 0)
type = ts;
if(type == ts)
{
now++;
if(now > k)
kl = k;
if(now == k)
kl += k;
//cout<<s[i]<<"=="<<endl;
}
else
{
if(now < k)
kl = 0;
now=1;
if(now == k)
kl += k;
//cout<<s[i]<<"<>"<<endl;
}
if(kl > max)
max = kl;
type = ts;
//cout<<"kl="<<kl<<"now="<<now<<endl;
}
cout<<max;
return 0;
}
複製代碼
作者:
黃璽安
時間:
2020-7-11 11:36
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
int main(){
int k, max, l, len, type;
string str;
cin>>k>>str;
max = l = len = 0;
for(int i = 0; i < str.length(); i++)
{
int t = isupper(str[i]);
if(i == 0)
type = t;
if(t == type)
{
l++;
if(l > k)
len = k;
}
else
{
if(l < k)
len = 0;
l = 1;
}
if(l == k)
len += k;
if(len > max)
max = len;
type = t;
}
cout<<max<<endl;
// system("pause");
return 0;
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2