PAT 乙级全部源码(已更新完http://blog.csdn.net/caicaiatnbu)

时间:2022-04-25 16:04:00

全部更新到 http://blog.csdn.net/caicaiatnbu
有部分题目采用STL中的vector,string,stack,queue等,我的博文C++ STL系列有对其详细介绍。
PAT 1001

#include<stdio.h>

int main()
{
//freopen("D://input.txt", "r", stdin);
int n;
while(scanf("%d", &n) != EOF)
{
int count = 0;//统计步数
while(n != 1)
{
if(n % 2 == 0)
n = n / 2;
else
n = (3*n + 1) / 2;

count ++;
}
printf("%d\n", count);
}
return 0;
}

PAT 1002

#include <stdio.h>
#include <string.h>
int main()
{
//freopen("D://input.txt", "r", stdin);
char s[101];
char numToChinese[10][6] = {"ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"};
while(scanf("%s", &s) != EOF)
{
//printf("%s\n", s);
//printf("%d\n", strlen(s));
int len = strlen(s);
int sum = 0;

for(int i = 0; i < len; i ++)
{
sum = sum + s[i] - '0';
}
//printf("%d\n", sum);

if(sum / 100 != 0)
printf("%s %s %s\n", numToChinese[sum/100], numToChinese[sum/10%10], numToChinese[sum%10]);
if(sum / 100 == 0 && sum / 10 % 10 != 0)
printf("%s %s", numToChinese[sum/10%10], numToChinese[sum%10]);
if(sum / 100 == 0 && sum / 10 % 10 == 0)
printf("%s\n", numToChinese[sum%10]);
}
return 0;
}

PAT 1003

//首先满足条件:所有的字符只能由P、A、T三个构成。
//用count_p表示:P之前A的个数,用count_a表示:P和T之间A的个数,用count_t表示:T之后A的个数,需要满足count_p*count_a == count_t并且count_a >= 1
#include<stdio.h>
#include<string.h>
int main()
{
freopen("D://input.txt", "r", stdin);
int n;
while(scanf("%d", &n) != EOF)
{
while(n --)
{
char s[102];
scanf("%s", s);
int pos_p = -1, pos_t = -1;
int count_p = 0, count_a = 0, count_t = 0;
bool flag_p = true, flag_t = true;
for(int i = 0; i < strlen(s); i ++)
{
if(s[i] == 'P' && flag_p)
{
pos_p = i;
flag_p = false;
}

if(s[i] == 'T' && flag_t)
{
pos_t = i;
flag_t = false;
}
}

if((pos_p == -1 || pos_t == -1) || pos_p > pos_t)
{
printf("NO\n");
continue;
}
else
{
for(int i = 0; i < pos_p; i ++)
{
if(s[i] == 'A')
count_p ++;
}

for(int i = pos_p+1; i < pos_t; i ++)
{
if(s[i] == 'A')
count_a ++;
}

for(int i = pos_t+1; i < strlen(s); i ++)
{
if(s[i] == 'A')
count_t ++;
}

if((count_p + count_a + count_t + 2) != strlen(s) || count_a == 0)
{
printf("NO\n");
continue;
}
}

if((count_p * count_a) == count_t)
printf("YES\n");
else
printf("NO\n");
}
}
return 0;
}

PAT 1004

//只需要遍历一遍,不需要进行排序。 
/*
#include<stdio.h>
#include<string.h>


int main()
{
//freopen("D://input.txt", "r", stdin);
int n;
while(scanf("%d", &n) != EOF)
{
char name_min[11], id_min[11];//保存分数最低学生的姓名和学号
char name_max[11], id_max[11];//保存分数最高学生的姓名和学号
int score_min = 100, score_max = 0;//保存分数最高学生的分数 和 分数最高学生的分数
while(n --)
{
char name[11], id[11];
int score;
scanf("%s %s %d", &name, &id, &score);
printf("%s %s %d\n", name, id, score);
if(score > score_max)
{
strcpy(name_max, name);
strcpy(id_max, id);
score_max = score;
}

if(score < score_min)
{
strcpy(name_min, name);
strcpy(id_min, id);
score_min = score;
}
}
printf("%s %s\n", name_max, id_max);
printf("%s %s\n", name_min, id_min);
}
return 0;
}

PAT 1005

// 用count[i] = 1表示此输入的i是关键数据,count[i] = 2表示此i不是关键数据,已经在被覆盖了
#include<stdio.h>
#include<string.h>

int count[102];

int main()
{
//freopen("D://input.txt", "r", stdin);
int n;
while(scanf("%d", &n) != EOF)
{
memset(count, 0, sizeof(count));

for(int i = 0; i < n; i ++)
{
int tmp;
scanf("%d", &tmp);
count[tmp] = 1;
}

for(int i = 1; i < 101; i ++)
{
if(count[i] != 1)
continue;

int tmp = i;

while(tmp != 1)
{
if(tmp % 2 == 0)
tmp = tmp / 2;
else
tmp = (3*tmp + 1) / 2;

if(tmp <= 100)
count[tmp] = 2;
}
}

bool flag = true;
for(int i = 100; i > 1; i --)
{
if(count[i] == 1)
{
if(flag)
{
printf("%d", i);
flag = false;
}
else
printf(" %d", i);
}
}
printf("\n");
}
return 0;
}

PAT 1006

#include<stdio.h>

int main()
{
//freopen("D://input.txt", "r", stdin);
int n;
while(scanf("%d", &n) != EOF)
{
int a = n / 100;
int b = n / 10 % 10;
int c = n % 10;

if(a > 0)
{
for(int i = 0; i < a; i ++)
printf("B");
}

if(b > 0)
{
for(int i = 0; i < b; i ++)
printf("S");
}

if(c > 0)
{
for(int i = 1; i <= c; i ++)
printf("%d", i);
}
printf("\n");
}
return 0;
}

PAT 1007

// 从第一个素数2开始,判断i和i+2是否同时为素数,如果同时为素数,则累加器count++,一直循环到i+2 <= n, 步长可选2
#include<stdio.h>
#include<math.h>

bool isPrime(int x)
{
for(int i = 2; i*i <= x; i ++)
{
if(x % i == 0)
return false;
}
return true;
}

int main()
{
//freopen("D://input.txt", "r", stdin);
int n;
while(scanf("%d", &n) != EOF)
{
int count = 0;
for(int i = 2; i <= n-2; i ++)
{
if(isPrime(i) && isPrime(i+2))
count ++;
}
printf("%d\n", count);
}


return 0;
}

PAT 1008

// 数组向右移动的值m可能大于数组的长度n,故需要对m = m % n;
// 假设数组里边有3个元素,依次为123,我们将数组变为:123123;这样向右移动遍历一遍就出来,从第n-m个打印到2*n-m
#include<stdio.h>

int main()
{
//freopen("D://input.txt", "r", stdin);

int buf[202];

int n, m;
while(scanf("%d", &n) != EOF)
{
scanf("%d", &m);
for(int i = 0; i < n; i ++)
{
scanf("%d", &buf[i]);
buf[n+i] = buf[i];
}

m = m % n;

for(int i = n-m; i < 2*n- m; i ++)
{
if(i == n-m)
printf("%d", buf[i]);
else
printf(" %d", buf[i]);
}

printf("\n");
}
return 0;
}

PAT 1009

#include<stdio.h>
#include<iostream>
#include<vector>
using namespace std;

int main()
{
//freopen("D://input.txt", "r", stdin);
string s;
vector<string> v;
while(cin>>s)
{
v.push_back(s);
}

for(int i = v.size()-1; i >= 0; i --)
if(i != 0)
cout<<v[i]<<" ";
else
cout<<v[i]<<endl;
return 0;
}

PAT 1010

// 如果某项中系数或者指数有一个为0,该项求导后为0,不需要显示;
// 同时,如果像 0 2 0 1 1 0 这样的输入,求完导整体为0,此时我们需要显示0 0,
// 这个case容易被忽视
#include<stdio.h>
#include<iostream>
#include<vector>
using namespace std;

int main()
{
freopen("D://input.txt", "r", stdin);
vector<int> v, res;
v.clear();
res.clear();
bool first = true;
int n, m;
while(scanf("%d", &n) != EOF)
{
v.push_back(n);
}


for(int i = 0; i < v.size(); i = i+2)
{
if(v[i] != 0 && v[i+1] != 0)
{
res.push_back(v[i]*v[i+1]);
res.push_back(v[i+1]-1);
}
}

if(res.size() == 0)
printf("0 0\n");
else
{
for(int i = 0; i < res.size(); i += 2)
{
if(i == 0)
printf("%d %d", res[i], res[i+1]);
else
printf(" %d %d", res[i], res[i+1]);
}
printf("\n");
}
return 0;
}

PAT 1011

// 要用long long数据类型,用int类型会导致数据溢出。
#include<stdio.h>

int main()
{
int n;
//freopen("D://input.txt", "r", stdin);
while(scanf("%d", &n) != EOF)
{
int i = 1;
while(n --)
{
long long a, b, c;
scanf("%lld %lld %lld", &a, &b, &c);
if(a + b > c)
printf("Case #%d: true\n", i);
else
printf("Case #%d: false\n", i);
i ++;
}
}
return 0;
}

PAT 1012

#include<stdio.h>

int main()
{
//freopen("D://input.txt", "r", stdin);
int n, buf[1002];
while(scanf("%d", &n) != EOF)
{
for(int i = 0; i < n; i ++)
scanf("%d", &buf[i]);

int A1 = 0, A2 = 0, A3 = 0, A5 = 0;
double A4 = 0.0;
int count1 = 0, count2 = 0, count3 = 0, count4 = 0, count5 = 0;
bool flag = true;
for(int i = 0; i < n; i ++)
{
if(buf[i] % 5 == 0 && buf[i] % 2 == 0)
{

A1 += buf[i];
count1 = 1;
}

if(buf[i] % 5 == 1)
{
if(flag)
A2 += buf[i];
else
A2 -= buf[i];

flag = !flag;
count2 = 1;
}

if(buf[i] % 5 == 2)
{
A3 ++;
count3 = 1;
}

if(buf[i] % 5 == 3)
{
A4 += buf[i];
count4 ++;
}

if(buf[i] % 5 == 4)
{
if(buf[i] > A5)
A5 = buf[i];
count5 = 1;
}
}

if(count1 == 0)
printf("N ");
else
printf("%d ", A1);
if(count2 == 0)
printf("N ");
else
printf("%d ", A2);
if(count3 == 0)
printf("N ");
else
printf("%d ", A3);

if(count4 == 0)
printf("N ");
else
printf("%.1f ", A4*1.0/count4);

if(count5 == 0)
printf("N\n");
else
printf("%d\n", A5);
}
return 0;
}

PAT 1013

#include<stdio.h>

bool isPrime(int x)
{
for(int i = 2; i*i <= x; i ++)
if(x % i == 0)
return false;
return true;
}

int main()
{
//freopen("D://input.txt", "r", stdin);
int m, n;
int buf[10002];
while(scanf("%d %d", &m, &n) != EOF)
{
int count = 1;
int i = 2;
while(count <= 10000)
{
if(isPrime(i))
{

buf[count] = i;
count ++;
}
i ++;
}

int len = 0;
for(i = m; i <= n; i ++)
{
if(len == 0)
printf("%d", buf[i]);
else
printf(" %d", buf[i]);
len ++;
if(len == 10)
{
len = 0;
printf("\n");
}
}
//printf("\n");
}
return 0;
}

PAT 1014

// 第一个字符串和第二个字符串,第一对相同的大写字母表示星期(A表示星期一,G表示星期天)
// 第二对相对的数字和大写字母表示小时(0-9依次表示0-9点,A-N依次表示10-23点)
// 在输出小时的时候,如果是8这样的,要输出成 08
// 第三个字符串和第四个串第一队相同的字母(A-Z,a—z)出现的位置就是分钟,就字符串角码。
// 也要输出成 02 这种格式
#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;

int main()
{
//freopen("D://input.txt", "r", stdin);
string Date[7] = {"MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"};
string s1, s2, s3, s4;
bool flag = 1;
while(cin>>s1>>s2>>s3>>s4)
{
char date ;
int hour ;
int minute ;
int flag = 1;
for(int i = 0; i < s1.length() && i < s2.length(); i ++)
{
if(s1[i] == s2[i] && s1[i] >= 'A' && s1[i] <= 'G' && flag == 1)
{
date = s1[i];
flag = 2;
continue;
//break;
}

if(s1[i] == s2[i] && ((s1[i] >= 'A' && s1[i] <= 'N') ||
(s1[i] >= '0' && s1[i] <= '9')) && flag == 2)
{
if(s1[i] >= 'A' && s1[i] <= 'N')
{
hour = s1[i] - 'A' + 10;
break;
}

if(s1[i] >= '0' && s1[i] <= '9')
{
hour = s1[i] - '0';
break;
}
}
}

for(int i = 0; i < s3.length() && i < s4.length(); i ++)
{
if(s3[i] == s4[i] && ((s3[i] >= 'A' && s4[i] <= 'Z') ||
(s3[i] >= 'a' && s4[i] <= 'z')))
{
minute = i;
}
}

cout<<Date[date-'A']<<" ";
printf("%02d:", hour);
printf("%02d\n", minute);
}
return 0;
}

PAT 1015

// 结构数组排序,总共有四类,开辟4个vector来依次保存这四类。依次压入到v1,v2,v3,v4
// 第二个考点就在cmp函数的书写,
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<vector>
using namespace std;

struct student
{
char ID[10];
int x;//德分
int y;//才分
};



bool cmp(const student &a, const student &b)
{
if((a.x+a.y) != (b.x+b.y))
return (a.x+a.y) > (b.x+b.y);
else if(a.x != b.x)
return a.x > b.x;
else
{
for(int i = 0; i < strlen(a.ID) && i < strlen(b.ID); i ++)
{
if(a.ID[i] != b.ID[i])
return a.ID[i] < b.ID[i];
}
}
}

int main()
{
//freopen("D://input.txt", "r", stdin);
int n, L, H;
vector<student> v1, v2, v3, v4;
student tmp;
while(scanf("%d", &n) != EOF)
{
v1.clear();
v2.clear();
v3.clear();
v4.clear();
scanf("%d %d", &L, &H);
for(int i = 0; i < n; i ++)
{
scanf("%s %d %d", tmp.ID, &tmp.x, &tmp.y);
if(tmp.x >= H && tmp.y >= H)
v1.push_back(tmp);
else if(tmp.x >= H && tmp.y >= L)
v2.push_back(tmp);
else if(tmp.x >= L && tmp.y >= L && tmp.x >= tmp.y)
v3.push_back(tmp);
else if(tmp.x >= L && tmp.y >= L)
v4.push_back(tmp);
}

sort(v1.begin(), v1.end(), cmp);
sort(v2.begin(), v2.end(), cmp);
sort(v3.begin(), v3.end(), cmp);
sort(v4.begin(), v4.end(), cmp);
printf("%d\n", v1.size() + v2.size() + v3.size() + v4.size());
for(int i = 0; i < v1.size(); i ++)
printf("%s %d %d\n", v1[i].ID, v1[i].x, v1[i].y);
for(int i = 0; i < v2.size(); i ++)
printf("%s %d %d\n", v2[i].ID, v2[i].x, v2[i].y);
for(int i = 0; i < v3.size(); i ++)
printf("%s %d %d\n", v3[i].ID, v3[i].x, v3[i].y);
for(int i = 0; i < v4.size(); i ++)
printf("%s %d %d\n", v4[i].ID, v4[i].x, v4[i].y);
}
return 0;
}

PAT 1016

#include<stdio.h>
#include<string.h>

int main()
{
//freopen("D://input.txt", "r", stdin);
char a[12], b[12];
int Da, Db;
while(scanf("%s %d %s %d", a, &Da, b, &Db) != EOF)
{
int sum1 = 0, sum2 = 0;
for(int i = 0; i < strlen(a); i ++)
{
if((a[i] - '0') == Da)
sum1 = sum1*10 + Da;
}

for(int i = 0; i < strlen(b); i ++)
{
if((b[i] - '0') == Db)
sum2 = sum2*10 + Db;
}

printf("%d\n", sum1+sum2);
}
return 0;
}

PAT 1017

// 因为除数b是一位数,被除数是a[],被除数从高位到底位逐位初以b,
// 举例123除以5时候,首先考虑第一位,此时结果1/5 = 0余数1,再考虑第二位,此时先加上第一位的余数,即变为12/5=2余数2,考虑第三位时候,此时应为23/5=4余数为3,所以结果为024.....4,所以从第一非0打印结果,用c保存余数。
#include<stdio.h>
#include<string.h>

int main()
{
//freopen("D://input.txt", "r" stdin);
char a[1002];
int res[10002];
int b;
while(scanf("%s %d", a, &b) != EOF)
{
//memset(res, 0, sizeof(res));
int c = 0;
if(strlen(a) == 1)
{
if((a[0]-'0') / b == 0)
printf("0 %d\n", (a[0]-'0') % b);
else
printf("%d %d\n", (a[0]-'0') / b, (a[0]-'0') % b);
continue;
}
for(int i = 0; i < strlen(a); i ++)
{
if((10*c+(a[i]-'0')) / b != 0)
{
res[i] = (10*c+(a[i]-'0')) / b;
if((10*c+(a[i]-'0')) % b != 0)
{
c = (10*c+(a[i]-'0')) % b;
}
else
c = 0;
}
else
{
res[i] = 0;
if((10*c+(a[i]-'0')) % b != 0)
{
c = (10*c+(a[i]-'0')) % b;
}
else
c = 0;

}
}

bool first = true;
for(int i = 0; i < strlen(a); i ++)
{
if(res[i] == 0 && first)
{
continue;
}
else
{
first = false;
printf("%d", res[i]);
}
}

printf(" %d\n", c);
}
return 0;
}

PAT 1018

// 甲赢得次数其实与乙输的次数相等,甲乙平手的次数相等。
#include<stdio.h>
#include<string.h>

int main()
{
//freopen("D://input.txt", "r", stdin);
char a[2], b[2];
int n;
while(scanf("%d", &n) != EOF)
{
int count_a[6];//count_a1中角码0表示甲胜的次数,1->甲平的次数,2->甲输的次数,3->甲赢出锤子,4->甲赢出布,5->甲赢出剪刀
int count_b[6];
memset(count_a, 0, sizeof(count_a));
memset(count_b, 0, sizeof(count_b));
for(int i = 0; i < n; i ++)
{

scanf("%s %s", a, b);
if((a[0] == 'C' && b[0] == 'C') || (a[0] == 'J' && b[0] == 'J') || (a[0] == 'B' && b[0] == 'B'))
{
count_a[1] ++;
count_b[1] ++;
}
else if((a[0] == 'B' && b[0] == 'C') || (a[0] == 'C' && b[0] == 'J') || (a[0] == 'J' && b[0] == 'B'))
{
count_a[0] ++;
count_b[2] ++;
if(a[0] == 'C')
count_a[3] ++;
else if(a[0] == 'B')
count_a[4] ++;
else
count_a[5] ++;
}
else
{
count_a[2] ++;
count_b[0] ++;

if(b[0] == 'C')
count_b[3] ++;
else if(b[0] == 'B')
count_b[4] ++;
else
count_b[5] ++;
}
}
printf("%d %d %d\n", count_a[0], count_a[1], count_a[2]);
printf("%d %d %d\n", count_b[0], count_b[1], count_b[2]);

if(count_a[4] >= count_a[3] && count_a[4] >= count_a[5])
printf("B ");
else if(count_a[3] >= count_a[4] && count_a[3] >= count_a[5])
printf("C ");
else if(count_a[5] >= count_a[3] && count_a[5] >= count_a[4])
printf("J ");


if(count_b[4] >= count_b[3] && count_b[4] >= count_b[5])
printf("B\n");
else if(count_b[3] >= count_b[4] && count_b[3] >= count_b[5])
printf("C\n");
else if(count_b[5] >= count_b[3] && count_b[5] >= count_b[4])
printf("J\n");
}
return 0;
}

PAT 1019

// 输入的范围是(0,10000),根据输入要生成最大数max和最小数min。输入不为4位数,我们再生成max和min要补0,补成4位数。
// 其最终结果肯定是6174。
#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
vector<int> v;

int max(int x)
{
int sum = 0;
v.clear();
while(x != 0)
{
v.push_back(x%10);
x = x / 10;
}
while(v.size() != 4)
v.push_back(0);
sort(v.begin(), v.end());
for(int i = v.size()-1; i >= 0; i --)
sum = sum*10 + v[i];
return sum;
}


int min(int x)
{
int sum = 0;
v.clear();
while(x != 0)
{
v.push_back(x%10);
x = x / 10;
}
while(v.size() != 4)
v.push_back(0);
sort(v.begin(), v.end());
for(int i = 0; i < v.size(); i ++)
sum = sum*10 + v[i];
return sum;
}

int main()
{
//freopen("D://input.txt", "r", stdin);
int n;
//while(scanf("%d", &n) != EOF)
scanf("%d", &n);
{
if(n == 6174)
{
printf("%04d - %04d = %04d\n", max(n), min(n), max(n)-min(n));
n = max(n) - min(n);
}
while(n != 6174)
{
if(max(n) == min(n))
{
printf("%04d - %04d = 0000\n", max(n), min(n));
break;
}
printf("%04d - %04d = %04d\n", max(n), min(n), max(n)-min(n));
n = max(n) - min(n);
}

}
return 0;
}

PAT 1020

// 结构数组排序,按照月饼价格从高到低排序,价格最高的先卖,直到卖到m
#include<stdio.h>
#include<algorithm>
using namespace std;


struct moon
{
double x;//总量
double y;//总价
double price;
};

bool cmp(const moon &a, const moon &b)
{
if(a.price != b.price)
return a.price > b.price;
else
return a.price > b.price;

}

int main()
{
// freopen("D://input.txt", "r", stdin);
int n, m;
moon buf[1002];
while(scanf("%d %d", &n, &m) != EOF)
{
for(int i = 0; i < n; i ++)
scanf("%lf", &buf[i].x);
for(int i = 0; i < n; i ++)
scanf("%lf", &buf[i].y);
for(int i = 0; i < n; i ++)
buf[i].price = buf[i].y/buf[i].x;
sort(buf, buf+n, cmp);

double sum = 0.0;

for(int i = 0; i < n; i ++)
{
if(m > buf[i].x)
{
sum += buf[i].y;
m = m - buf[i].x;
}
else
{
sum = sum + buf[i].price*m;
break;
}

}

printf("%.2f\n", sum);
}
return 0;
}

PAT 1021

#include<stdio.h>
#include<string.h>

int main()
{
//freopen("D://input.txt", "r", stdin);

char s[1002];
int count[11];
while(scanf("%s", s) != EOF)
{
memset(count, 0, sizeof(count));
for(int i = 0; i < strlen(s); i ++)
{
count[s[i]-'0'] ++;
}

for(int i = 0; i < 10; i ++)
{
if(count[i] != 0)
printf("%d:%d\n", i, count[i]);
}
}
return 0;
}

PAT 1022

// int类型就可以,a和b的和不会溢出。考点就是把一个十进制数转为另一进制,取余法。
#include<stdio.h>
#include<vector>
using namespace std;

int main()
{
//freopen("D://input.txt", "r", stdin);
int a, b, d;
while(scanf("%d %d %d", &a, &b, &d) != EOF)
{
int sum = a + b;
vector<int> v;
v.clear();
if(sum == 0)
printf("0");
while(sum != 0)
{
v.push_back(sum%d);
sum = sum / d;
}

for(int i = v.size()-1; i >=0; i --)
{
printf("%d",v[i]);
}
printf("\n");
}
return 0;
}

PAT 1023

#include<stdio.h>
#include<string.h>
int main()
{
int buf[10];
memset(buf, 0, sizeof(buf));
for(int i = 0; i < 10; i ++)
scanf("%d", &buf[i]);

for(int i = 1; i < 10; i ++)
{
if(buf[i] != 0)
{
printf("%d", i);
buf[i] --;
break;
}
}

for(int i = 0; i < 10; i ++)
{
for(int j = 0; j < buf[i]; j ++)
printf("%d", i);
}
printf("\n");
return 0;
}

PAT 1024

#include<stdio.h>
#include<string.h>


int main()
{
//freopen("D://input.txt", "r", stdin);
char s[10010];
while(scanf("%s", s) != EOF)
{
bool flag1, flag2;
if(s[0] == '+')
flag1 = true;
else
flag1 = false;

char *buf1, *buf2, *buf3;
const char *d = ".E";
buf1 = strtok(s, d);
buf2 = strtok(NULL, d);
buf3 = strtok(NULL, d);

if(buf3[0] == '+')
flag2 = true;
else
flag2 = false;

int count = 0;
for(int i = 1; i < strlen(buf3); i ++)
count = count*10 + (buf3[i] - '0');

// case +
if(!flag1)
printf("-");

if(count == 0)
{
for(int i = 1; i < strlen(buf1); i ++)
printf("%c", buf1[i]);
printf(".%s", buf2);
continue;
}

if(flag2)
{
for(int i = 1; i < strlen(buf1); i ++)
printf("%c", buf1[i]);

if(count < strlen(buf2))
{
for(int i = 0; i < strlen(buf2); i ++)
{
if(i < count)
printf("%c", buf2[i]);
else if(i == count)
{
printf(".%c", buf2[i]);
}
else
printf("%c", buf2[i]);
}
}
else
{
for(int i = 0; i < count; i ++)
{
if(i < strlen(buf2))
printf("%c", buf2[i]);
else
printf("0");
}
}
}
else
{
if(count < (strlen(buf1)-1))
{
for(int i = 1; i < strlen(buf1); i ++)
{
if((count + i) == strlen(buf1))
printf(".%c", buf1[i]);
else
printf("%c", buf1[i]);
}
printf("%s", buf2);
}
else
{
printf("0.");
for(int i = 0; i < (count+1-strlen(buf1)); i ++)
printf("0");

for(int i = 1; i < strlen(buf1); i ++)
printf("%c", buf1[i]);

printf("%s", buf2);
}
}
printf("\n");
}
return 0;
}

PAT 1025

// 注意输入的节点有可能不在链表上;
// 开辟一个buf[100002] 的结构数组,将data和next 根据其address保存到buf[address]中,然后根据firstaddress将链表上的节点加载到vector中,最后计算需要翻转的次数num = v.size()/k,用reverse(v.begin()+i*k, v.begin()+(i+1)*k)进行翻转,k从0到num
#include<stdio.h>
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

struct node
{
int address;
int data;
int next;
}buf[100002];

vector<node> v;


int main()
{
freopen("D://input.txt", "r", stdin);
int firstAddress, n, k;
while(scanf("%d %d %d", &firstAddress, &n, &k) != EOF)
{
for(int i = 0; i < n; i ++)
{
int index;
scanf("%d", &index);
buf[index].address = index;
scanf("%d %d", &buf[index].data, &buf[index].next);
}

if(firstAddress == -1)
printf("-1\n");
else
{
int p = firstAddress;
while(p != -1)
{
v.push_back(buf[p]);
p = buf[p].next;
}
int countNode = v.size();
int num = countNode / k;//反转次数 因为k<=n 所以num>=1
for(int i = 0; i < num; i ++)
std::reverse(v.begin() + i*k, v.begin() + (i+1)*k);

for(int i = 0; i < countNode; i ++)
{
if((i+1) != countNode)
printf("%05d %d %05d\n", v[i].address, v[i].data, v[i+1].address);
else
printf("%05d %d -1\n", v[i].address, v[i].data);
}
}
}
return 0;
}

PAT 1026

#include<stdio.h>

int main()
{
freopen("D://input.txt", "r", stdin);
int c1, c2;
while(scanf("%d %d", &c1, &c2) != EOF)
{
int ans = (c2-c1) / 100;
if((c2 - c1) % 100 >= 50)
ans += 1;
int h = ans / 3600;
int m = (ans - h*3600) / 60;
int s = ans % 60;

printf("%02d:%02d:%02d\n", h, m, s);
}
return 0;
}

PAT 1027

#include<stdio.h>

void print(int n, char s[])
{
for(int i = 0; i < n; i ++)
{
for(int j = 0; j < n; j ++)
{

if((j >= i && (i+j) <= (n-1)) || (i >= j) && (i+j) >= (n-1))
printf("%s", s);
else if(j < i && i <= n/2)
printf(" ");
else if((i+j) < (n-1) && i > n/2)
printf(" ");


}
printf("\n");
}
}

int main()
{
freopen("D://input.txt", "r", stdin);
int n;
char s[2];
while(scanf("%d %s", &n, s) != EOF)
{
int level = -1;
int count = 0;

for(int i = 1; n >=0; i += 2)
{
if(i == 1)
n = n - 1;
else
n = n - 2*i;
if(n >= 0)
{
level += 2;
count = n;
}
}
print(level, s);
printf("%d\n", count);
//printf("level = %d count = %d\n", level, count);

}
return 0;
}

PAT 1028

// 注意满足条件的可能有0个人
#include<stdio.h>
#include<string.h>

int main()
{
freopen("D://input.txt", "r", stdin);
int n;
char name[6],birthday[12];
while(scanf("%d", &n) != EOF)
{
char max[12] = "2014/09/06";
char min[12] = "1814/09/06";
char name1[6], name2[6];
char limit1[12] = "1814/09/06";
char limit2[12] = "2014/09/06";
int count = 0;
while(n --)
{
scanf("%s %s", name, birthday);
if(strcmp(birthday, limit1) < 0)
continue;
if(strcmp(birthday, limit2) > 0)
continue;
if(strcmp(birthday, max) < 0)
{
strcpy(name1, name);
strcpy(max, birthday);
}
if(strcmp(birthday, min) > 0)
{
strcpy(name2, name);
strcpy(min, birthday);
}
count ++;
}
if(count != 0)
printf("%d %s %s\n", count, name1, name2);
else
printf("0\n");
}
return 0;
}

PAT 1029

#include<stdio.h>
#include<string.h>
int main()
{
//freopen("D://input.txt", "r", stdin);
char s[82], ans[82];
int count[128];
while(scanf("%s %s", s, ans) != EOF)
{
memset(count, 0, sizeof(count));
char ch;
for(int i = 0; i < strlen(ans); i ++)
{
count[ans[i]] = 1;
if(ans[i] >= 'a' && ans[i] <= 'z')
count[ans[i]-'a'+'A'] = 1;
if(ans[i] >= 'A' && ans[i] <= 'Z')
count[ans[i]-'A'+'a'] = 1;
}

for(int i = 0; i< strlen(s); i ++)
{
if(count[s[i]] == 0)
{

count[s[i]] = 2;
if(s[i] >= 'a' && s[i] <= 'z')
{
printf("%c", s[i]-'a'+'A');
count[s[i]-'a'+'A'] = 2;
continue;
}

printf("%c", s[i]);
if(s[i] >= 'A' && s[i] <= 'Z')
count[s[i]-'A'+'a'] = 2;
}
}
printf("\n");
}
return 0;
}

PAT 1030

// 注意:P是float
#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
//freopen("D://input.txt", "r", stdin);

int n;
float p;
vector<int> v;
while(scanf("%d %f", &n, &p) != EOF)
{
int length = 1;
v.clear();
int tmp;
for(int i = 0; i < n; i ++)
{
scanf("%d", &tmp);
v.push_back(tmp);
}
sort(v.begin(), v.end());


for(int i = 0; i < v.size(); i ++)
{
for(int j = i+length-1; j < v.size(); j ++)
{
if(v[j] <= v[i]*p)
{
if(j-i+1 > length)
length = j - i + 1;
}
else
break;
}
}

printf("%d\n", length);
}
return 0;
}

PAT 1031

#include<stdio.h>
#include<string.h>

int main()
{
freopen("E://input.txt", "r", stdin);
int n;
char s[20];
char z[11] = {'1','0','X','9','8','7','6','5','4','3','2'};
int w[18] = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
while(scanf("%d", &n) != EOF)
{
int count = 0;

while(n --)
{
scanf("%s", s);
int sum = 0;
for(int i = 0; i < 17; i ++)
sum = sum + (s[i] - '0')*w[i];

sum = sum % 11;
if(s[17] != z[sum])
{
printf("%s\n", s);
count ++;
}
}

if(count == 0)
printf("All passed\n");
}
return 0;
}

PAT 1032

#include<stdio.h>
#include<string.h>
int main()
{
//freopen("D://input.txt", "r", stdin);
int n;
int buf[100002];
while(scanf("%d", &n) != EOF)
{
int index, score;
memset(buf, 0, sizeof(buf));
for(int i = 0; i < n; i ++)
{

scanf("%d %d", &index, &score);
buf[index] += score;
}

index = -1; score = 0;
for(int i = 0; i <= 100000; i ++)
{
if(buf[i] > score)
{
index = i;
score = buf[i];
}
}
printf("%d %d\n", index, score);
}
return 0;
}

PAT 1033

// 注意:第一行输入可能是为空,是一个空行,故需要用gets()来获取第一行的输入
#include<stdio.h>
#include<string.h>
int main()
{
//freopen("D://input.txt", "r", stdin);
char error[100002];
bool flag[128];
char ch;
gets(error);
memset(flag, 0, sizeof(flag));
for(int i = 0; i < strlen(error); i ++)
{
ch = error[i];
flag[ch] = 1;
if(ch >= 'A' && ch <= 'Z')
{
ch = ch - 'A' + 'a';
flag[ch] = 1;
}
}

while(scanf("%c", &ch) != EOF)
{
if(flag[ch] == 0)
{
if(ch >= 'A' && ch <= 'Z' && flag['+'] == 1)
continue;
printf("%c", ch);
}
}
return 0;
}

PAT 1034

// 注意在计算过程中,计算gcd和lcm可能会超出int表示的数据范围,所以得用long类型
#include<stdio.h>
#include<string.h>

long gcd(long x, long y)
{
return x == 0 ? y : gcd(y%x, x);
}
long lcm(long x, long y)
{
return x/gcd(x,y)*y;
}

void print(long x, long y)
{
bool flag = false;
if(x < 0 )
{
x = -x;
flag = true;
}

if(x / y != 0 && x % y != 0)
{
if(flag)
printf("(-%d %d/%d)", x / y, x%y/gcd(x%y,y), y/gcd(x%y,y));
else
printf("%d %d/%d", x / y, x%y/gcd(x%y,y), y/gcd(x%y,y));
}
else if(x / y == 0 && x % y != 0)
{
if(flag)
printf("(-%d/%d)", x/gcd(x,y), y/gcd(x,y));
else
printf("%d/%d", x/gcd(x,y), y/gcd(x,y));
}
else if(x / y != 0 && x % y == 0)
{
if(flag)
printf("(-%d)", x/y);
else
printf("%d", x/y);
}
else
printf("0");
}

int main()
{
freopen("D://input.txt", "r", stdin);

long a, b, c, d;
while(scanf("%ld/%ld %ld/%ld", &a, &b, &c, &d) != EOF)
{
// case +
long res1 = 0, res2 = 0;
print(a, b);
printf(" + ");
print(c, d);
printf(" = ");
res1 = a * lcm(b, d)/b + c * lcm(b, d)/d;
res2 = lcm(b, d);
print(res1, res2);
printf("\n");

// case -
print(a, b);
printf(" - ");
print(c, d);
printf(" = ");
res1 = a* lcm(b, d)/b - c* lcm(b, d)/d;
res2 = lcm(b, d);
print(res1, res2);
printf("\n");

//case *
print(a, b);
printf(" * ");
print(c, d);
printf(" = ");
res1 = a*c;
res2 = b*d;
print(res1, res2);
printf("\n");

// case /
print(a, b);
printf(" / ");
print(c, d);
printf(" = ");
if(c != 0)
{
if(c < 0)
{
d = -1*d;
c = -1*c;
}
res1 = a*d;
res2 = b*c;
print(res1, res2);
}
else
printf("Inf");

printf("\n");
}
return 0;
}