PAT A1009-1012
阅读原文时间:2023年07月10日阅读:1

A 1009 Product of Polynomials (25 point(s))

  读懂题意就行。

#include
#include
#include
#include

using namespace std;
typedef struct NODE
{
int exp;
double coe;
NODE(){exp = ; coe = ;}
NODE(int e, double v):exp(e),coe(v){}
}node;
vector poly1, poly2;
double rstPoly[]={};
int main()
{
int N, tmpExp;
double tmpCoe;
cin >> N;
for(int i = ; i < N; ++ i) { cin >> tmpExp >> tmpCoe;
poly1.push_back(NODE(tmpExp, tmpCoe));
}
cin >> N;
for(int i = ; i < N; ++ i) { cin >> tmpExp >> tmpCoe;
poly2.push_back(NODE(tmpExp, tmpCoe));
}
for(int i = ; i < poly1.size(); ++i) for(int j = ; j < poly2.size(); ++ j) rstPoly[poly1[i].exp+poly2[j].exp] += poly1[i].coe*poly2[j].coe; int cnt = ; for(int i = ; i < ; ++ i) if(rstPoly[i] != ) cnt++; cout << cnt; for(int i = ; i >= ; -- i)
if(rstPoly[i] != )
{
printf(" %d %.1f", i, rstPoly[i]);
}
return ;
}

A 1010 Radix (25 point(s))

  这道题目相当有趣,坑的莫名其妙,哈哈哈。

  注意:1.数的范围,10位数字,int 不够用

2.超时,因而不能从小到大递增取进制数

3.二分法,最大进制和最小进制的确定

4.超限,在找进制数的过程中,过大的进制数会发生超限,注意这种情况

5.待确定进制数只有一位,此时需要注意取最小进制

#include
#include
#include
#include
#include
#include
#include

using namespace std;
long long toNum(char c)
{
return isdigit(c) ? c-'' : c-'a'+;
}
long long strRadixToNum(string tmpStr, long long radix)
{
long long tmpNum = , ant = ;
for(int i = tmpStr.size()-; i >= ; -- i)
{
tmpNum += toNum(tmpStr[i])*ant;
if(tmpNum < || ant < ) return -; ant *= radix; } return tmpNum; } long long minRadix(string tmpStr) { char tmpChar = ; for(int i = ; i < tmpStr.size(); ++ i) { if(tmpStr[i] > tmpChar)
tmpChar = tmpStr[i];
}
return toNum(tmpChar) + ;
}
int main()
{
long long tag, radix, tmpNum, tmpTarget, lowRadix, highRadix;
string N1, N2;
cin >> N1 >> N2 >> tag >> radix;
//算出进制已经确定的数作为目标值
//并且 找出未确定进制的数的最小进制
if(tag == )
swap(N1, N2);
highRadix = tmpTarget = strRadixToNum(N1, radix);
lowRadix = max((long long) , minRadix(N2));
while(lowRadix <= highRadix) { long long midRadix = (lowRadix + highRadix)/; long long tmpNum1 = strRadixToNum(N2, midRadix); if(tmpNum1 >= tmpTarget ||tmpNum1 == -)
highRadix = midRadix - ;
else
lowRadix = midRadix + ;
}
if(strRadixToNum(N2, lowRadix) == tmpTarget)
cout << lowRadix;
else
cout << "Impossible";
return ;
}

A 1011 World Cup Betting (20 point(s))

#include
#include
#include
#include
#include
#include

using namespace std;
int main()
{
double sum = 0.65, tmpW, tmpT, tmpL;
for(int i = ; i < ; ++ i) { cin >> tmpW >> tmpT >> tmpL;
if(tmpW > max(tmpT, tmpL))
{
cout << "W "; sum *= tmpW; } else if(tmpT > max(tmpW, tmpL))
{
cout << "T ";
sum *= tmpT;
}
else
{
cout << "L ";
sum *= tmpL;;
}
}
printf("%.2f", (sum-)*);
return ;
}

A 1012 The Best Rank (25 point(s))

  注意:1.排名的问题,相同成绩同一排名

     2.最佳排名相同,按所给优先级

#include
#include
#include
#include
#include
#include
#include
#include
#include

using namespace std;
typedef struct NODE
{
int id, math, cCode, aver, english;
}node;
vector cCodeVec, mathVec, engVec, averVec;
unordered_map nodeMap;
unordered_map nodeTableMap;
void getBestRank(int u)
{
int type = , rank, cnt;
char typeStr[]="ACME";
node tmpNode = nodeMap[u];
cnt = ;
for(auto it = averVec.begin(); it != averVec.end(); ++it, ++cnt)
if(*it == tmpNode.aver)
{
type = ; rank = cnt;break;
}
cnt = ;
for(auto it = cCodeVec.begin(); it != cCodeVec.end(); ++it, ++cnt)
if(*it == tmpNode.cCode && cnt < rank) { type = ; rank = cnt;break; } cnt = ; for(auto it = mathVec.begin(); it != mathVec.end(); ++it, ++cnt) if(*it == tmpNode.math && cnt < rank) { type = ; rank = cnt;break; } cnt = ; for(auto it = engVec.begin(); it != engVec.end(); ++it, ++cnt) if(*it == tmpNode.english && cnt < rank) { type = ; rank = cnt;break; } printf("%d %c\n", rank, typeStr[type]); } int main() { int N, M; node tmpNode; cin >> N >> M;
for(int i = ; i < N; ++i) { cin >> tmpNode.id >> tmpNode.cCode >> tmpNode.math >> tmpNode.english;
tmpNode.aver = (tmpNode.cCode + tmpNode.math + tmpNode.english+1.5)/;
cCodeVec.push_back(tmpNode.cCode); mathVec.push_back(tmpNode.math);
engVec.push_back(tmpNode.english); averVec.push_back(tmpNode.aver);
nodeMap[tmpNode.id] = tmpNode;nodeTableMap[tmpNode.id] = ;
}
sort(averVec.begin(), averVec.end(), greater());
sort(cCodeVec.begin(), cCodeVec.end(), greater());
sort(mathVec.begin(), mathVec.end(), greater());
sort(engVec.begin(), engVec.end(), greater());
for(int i = ; i < M; ++ i) { cin >> tmpNode.id;
if(nodeTableMap[tmpNode.id] > )
getBestRank(tmpNode.id);
else
cout << "N/A" << endl;
}
return ;
}

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章