PAT 字元,數字處理題,總是有坑,邊界,特殊,時間複雜度
阿新 • • 發佈:2019-01-04
1054 求平均值 (20)
題目地址
注意點
- 輸入輸出
- case4 把 “1.” 當成正確的 (坑)
1052 賣個萌 (20)
題目地址
注意點
- 輸出 \ 時 需要轉義 “\”(類似還有 %等等特殊字元)
1048 數字加密(20)
題目地址
注意點
- 字串長度不夠需要補齊
1051 複數乘法 (15)
題目地址
ac
- 注意保留小數位兩位
printf("%.2lf",-0.005); // -0.01
printf("%.2lf",-0.004999); // -0.00
int main()
{
//freopen("in.txt","r",stdin);
double R1,P1,R2,P2;
while(scanf("%lf%lf%lf%lf",&R1,&P1,&R2,&P2) != EOF)
{
double a1 = R1 * cos(P1);
double b1 = R1 * sin(P1);
double a2 = R2 * cos(P2);
double b2 = R2 * sin(P2);
double real = a1 * a2 - b1 * b2;
double xu = a1 * b2 + a2 * b1;
if(real < 0 && real > -0.005)
real = 0;
if(xu < 0 && xu > -0.005)
xu = 0;
double flag = 0.00;
if(xu >= 0){
printf("%.2lf+%.2lfi\n",real,xu);
}else{
printf("%.2lf%.2lfi\n",real,xu);
}
}// end while;
return 0;
}
1058 選擇題(20)
題目地址
主要是注意輸入輸出
- getchar() 吸收空格
- 可以直接scanf(“\n”); 處理換行符
1049 數列的片段和(20)
題目地址
ac注意
- 數學題,需要發現規律
- 採用long long int 防止超過範圍
int a, b;
int c = a*b; 有可能超出範圍 (c改成long long int 也一樣)
應該是
long long int a , b;
long long int c = a * b;
1028 List Sorting (25)
題目地址
ac注意
使用如下的結構體, 用char[] ,int ,不要用string
struct node{
int id;
char name[9];
int score;
};
1071 Speech Patterns (25)
題目地址
ac程式碼注意
單詞只包含 0-9 a-z A-Z
單詞 可以是數字和字母開頭
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <algorithm>
#include <sstream>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
using namespace std;
const int INF = 0x7fffffff;
const int MIN_INF = - INF -1;
typedef long long int LL;
int n;
bool isAlpha(char c)
{
if(c >= 'a' && c <= 'z')
return true;
if(c >='0' && c <= '9')
return true;
return false;
}
int main()
{
//freopen("in.txt","r",stdin);
string s;
getline(cin,s);
s += " ";
int len = s.size();
for(int i=0;i<len;i++)
{
if(s[i] >= 'A' && s[i] <= 'Z')
s[i] = s[i] - 'A' + 'a';
}
int maxCnt = -1;
map<string,int> mp;
mp.clear();
string word ="";
bool isStart = false;
for(int i=0;i<len;i++)
{
if(isStart == false && isAlpha(s[i]))
{
if(word != "")
{
//cout << word << endl;
mp[word] ++;
if(mp[word] > maxCnt)
{
maxCnt = mp[word];
}
}
word.clear();
word += s[i];
isStart = true;
}else if(isStart && isAlpha(s[i])){
word += s[i];
}else if(isStart && !isAlpha(s[i])){
if(word != "")
{
//cout << word << endl;
mp[word] ++;
if(mp[word] > maxCnt)
{
maxCnt = mp[word];
}
}
word.clear();
isStart = false;
}
}
map<string,int>::iterator it = mp.begin();
while(it != mp.end())
{
if(it->second == maxCnt)
{
cout << it->first << " " << maxCnt;
break;
}
++it;
}
printf("\n");
return 0;
}
1085 Perfect Sequence (25)
題目地址
ac注意特殊情況,儘可能優化時間
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <algorithm>
#include <sstream>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
using namespace std;
const int INF = 0x7fffffff;
const int MIN_INF = - INF -1;
typedef long long int LL;
const int N = 100005;
int n;
LL p;
LL v[N];
int main()
{
//freopen("in.txt","r",stdin);
while(scanf("%d%d", &n, &p) != EOF)
{
for(int i =0;i<n;i++)
{
scanf("%lld",&v[i]);
}
sort(v, v+ n);
if(v[n-1] <= v[0] * p)
{
printf("%d\n",n);
continue;
}
int ansLen = 0; // 儲存結果
int index = 0; // 找儘可能最大的結果
for(int i=0;i<n;i++)
{
LL m = v[i];
int flagj = -1;
if(n - i < ansLen || index >= n)
break;
for(int j = index ;j < n; j++)
{
LL M = v[j];
if(M > m * p){
flagj = j;
break;
}
}
if(flagj == -1)
flagj = n;
int len = flagj - i;
if(len > ansLen)
ansLen = len;
index = flagj;
}
printf("%d\n",ansLen);
}
return 0;
}
1032 Sharing (25)
題目地址
ac
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <algorithm>
#include <sstream>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
using namespace std;
const int INF = 0x7fffffff;
const int MIN_INF = - INF -1;
typedef long long int LL;
const int N = 100005;
struct data
{
int no;
char c;
int next;
};
data v[N];
int sta1;
int sta2;
int n;
int main()
{
//freopen("in.txt","r",stdin);
while(scanf("%d%d%d", &sta1, &sta2, &n) != EOF)
{
int addr;
char da;
int nex;
for(int i=0;i<n;i++)
{
scanf("%d %c %d", &addr, &da, &nex);
v[addr].no = addr;
v[addr].c = da;
v[addr].next = nex;
}
vector<data> v1;
vector<data> v2;
int fa1 = sta1;
while(fa1 != -1)
{
v1.push_back(v[fa1]);
fa1 = v[fa1].next;
}
int fa2 = sta2;
while(fa2 != -1)
{
v2.push_back(v[fa2]);
fa2 = v[fa2].next;
}
int len1 = v1.size();
int len2 = v2.size();
int ans = -1;
int in1 = len1 - 1;
int in2 = len2 - 1;
while(in1 >= 0 && in2 >= 0 && v1[in1].c == v2[in2].c && v1[in1].no == v2[in2].no)
{
ans = v1[in1].no;
in1 --;
in2 --;
}
/*if(len1 >= len2)
{
for(int i=0;i<len2;i++)
{
int in1 = len1- len2 + i;
int in2 = i;
if(v1[in1].c == v2[in2].c)
{
ans = v1[in1].no;
break;
}
}
}else{
for(int i=0;i<len1;i++)
{
int in2 = len2- len1 + i;
int in1 = i;
if(v1[in1].c == v2[in2].c)
{
ans = v1[in1].no;
break;
}
}
}*/
if(ans == -1)
printf("-1\n");
else
printf("%05d\n", ans);
}
//printf("\n");
return 0;
}
1061 Dating (20)
ac程式碼
#include <cstdio>
#include <memory>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <algorithm>
#include <sstream>
#include <list>
#include <stack>
#include <map>
#include <set>
using namespace std;
// freopen("in.txt", "r", stdin);
#define INF 0x7fffffff
const int N = 10l;
int main( )
{
//freopen("in.txt", "r", stdin);
char s1[61];
char s2[61];
char s3[61];
char s4[61];
gets(s1);
gets(s2);
gets(s3);
gets(s4);
int len1 = strlen(s1);
int len2 = strlen(s2);
int len3 = strlen(s3);
int len4 = strlen(s4);
string days[] = {"MON", "TUE", "WED", "THU", "FRI","SAT", "SUN"};
string day;
int hh;
int mm;
int i = 0;
int j = 0;
int cnt = 0;
while(i < len1 && j < len2)
{
if(s1[i] == s2[j] && s1[i] >= 'A' && s1[i] <= 'G' && cnt == 0)
{
day = days[ s1[i] - 'A'];
cnt = 1;
}else if(s1[i] == s2[j] &&
( (s1[i] >= 'A' && s1[i] <= 'N') || (s1[i] >= '0' && s1[i] <= '9') )
&& cnt == 1)
{
if(s1[i] >= 'A' && s1[i] <= 'N')
{
hh = 10 + s1[i] - 'A';
}else{
hh = s1[i] - '0';
}
break;
}
i++;
j ++;
}
i = 0;
j = 0;
while(i < len3 && j < len4)
{
if(s3[i] == s4[j] &&
( (s3[i] >= 'A' && s3[i] <= 'Z') || (s3[i] >= 'a' && s3[i] <= 'z') )
)
{
mm = i;
break;
}
i++;
j++;
}
cout << day;
printf(" %02d:%02d\n", hh,mm);
return 0;
}
1070. Mooncake (25)
#include <cstdio>
#include <memory>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <algorithm>
#include <sstream>
#include <list>
#include <stack>
#include <map>
#include <set>
using namespace std;
// freopen("in.txt", "r", stdin);
#define INF 0x7fffffff
int n, d;
struct pro{
double amount;
double tcost;
double price;
};
bool cmp(pro p1, pro p2)
{
return p1.price > p2.price;
}
int main()
{
//freopen("in.txt", "r", stdin);
while(scanf("%d%d", &n, &d) != EOF)
{
vector<pro> v(n);
for(int i=0;i<n;i++)
{
scanf("%lf", &v[i].amount);
}
for(int i=0;i<n;i++)
{
scanf("%lf", &v[i].tcost);
v[i].price = v[i].tcost / v[i].amount;
}
sort(v.begin(), v.begin() + n, cmp);
int index = 0;
int tAountNeed = d;
double sums = 0.0;
while(tAountNeed > 0)
{
pro tmp = v[index++];
if(tmp.amount >= tAountNeed)
{
sums += tmp.price * tAountNeed;
break;
}else{
tAountNeed -= (int)tmp.amount;
sums += tmp.tcost;
}
if(index >= n)
break;
}
printf("%.2lf\n", sums);
}
return 0;
}
1101. Quick Sort (25)
題目地址
ac程式碼(主要是輸出和演算法時間效率問題)
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <algorithm>
#include <sstream>
#include <list>
#include <stack>
#include <map>
#include <set>
#include <iterator>
#include <unordered_map>
using namespace std;
#define INF 0x7fffffff
typedef long long int LL;
const int N = 100000 + 5;
int n;
int v[N];
int main()
{
//freopen("in.txt", "r" ,stdin);
while( scanf("%d", &n) != EOF)
{
if(n == 1)
{
scanf("%d", &v[0]);
printf("1\n");
printf("%d\n", v[0]);
}else if(n == 2)
{
scanf("%d%d", &v[0], &v[1]);
if(v[0] < v[1])
{
printf("2\n");
printf("%d %d\n", v[0], v[1]);
}else{
printf("0\n");
}
}else{
vector<bool> left(n, false);
scanf("%d", &v[0]);
int maxLeft = v[0];
left[0] = true;
for(int i=1;i<n;i++)
{
scanf("%d", &v[i]);
if(v[i] > maxLeft)
{
left[i] = true;
maxLeft = v[i];
}
}
vector<int> ans;
int minRight = v[n-1];
/*vector<bool> right(n, false);
right[n-1] = true;*/
if(left[n-1])
{
ans.push_back(v[n-1]);
}
for(int i= n-2;i>=0;i--)
{
if(v[i] < minRight)
{
if(left[i])
ans.push_back(v[i]);
minRight = v[i];
}
}
int len = ans.size();
printf("%d\n", len);
if(len > 0)
{
sort(ans.begin(), ans.end());
printf("%d", ans[0]);
for(int i=1;i<len;i++)
{
printf(" %d", ans[i]);
}
//printf("\n");
}
printf("\n");
}
//printf("\n");
}
return 0;
}
1124. Raffle for Weibo Followers (20)
題目地址
ac
有就往下一個
s=2,n=3按照的順序如下
2 5 8 11
當5位置原來訪問過,那麼訪問序列將變成
2 6 9 12
如果6位置也是訪問過的,那麼訪問序列將變成
2 7 10 13
總之 就是,每兩個資料之間的序號一定是相差給定的n的