Wednesday, July 19, 2017

PAT (Basic Level) Practise (中文) 1019

1019. 数字黑洞 (20)

时间限制
100 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
CHEN, Yue
给定任一个各位数字不完全相同的4位正整数,如果我们先把4个数字按非递增排序,再按非递减排序,然后用第1个数字减第2个数字,将得到一个新的数字。一直重复这样做,我们很快会停在有“数字黑洞”之称的6174,这个神奇的数字也叫Kaprekar常数。
例如,我们从6767开始,将得到
7766 - 6677 = 1089
9810 - 0189 = 9621
9621 - 1269 = 8352
8532 - 2358 = 6174
7641 - 1467 = 6174
... ...
现给定任意4位正整数,请编写程序演示到达黑洞的过程。
输入格式:
输入给出一个(0, 10000)区间内的正整数N。
输出格式:
如果N的4位数字全相等,则在一行内输出“N - N = 0000”;否则将计算的每一步在一行内输出,直到6174作为差出现,输出格式见样例。注意每个数字按4位数格式输出。
输入样例1:
6767
输出样例1:
7766 - 6677 = 1089
9810 - 0189 = 9621
9621 - 1269 = 8352
8532 - 2358 = 6174
输入样例2:
2222
输出样例2:
2222 - 2222 = 0000

//C++ Program
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

void print(vector<int> vec);
void handle(vector<int> vec);

int main(void) {

 int N;
 vector<int> v;
 cin >> N;

 for (int i = 0; i < 4; ++i) {
  v.push_back(N % 10);
  N /= 10;
 }
 sort(v.begin(), v.end());

 handle(v);

 return 0;

}

void print(vector<int> vec) {
 for (vector<int>::iterator it = vec.begin(); it != vec.end(); ++it) {
  cout << *it;
 }
}

//处理从小到大排好序的vector对象
void handle(vector<int> vec) {

 int num1, num2, num, num_temp;

 do {
  reverse(vec.begin(), vec.end());
  num1 = vec.at(0) * 1000 + vec.at(1) * 100 + vec.at(2) * 10 + vec.at(3);
  print(vec);
  cout << " - ";
  reverse(vec.begin(), vec.end());
  num2 = vec.at(0) * 1000 + vec.at(1) * 100 + vec.at(2) * 10 + vec.at(3);
  print(vec);
  num = num1 - num2;
  num_temp = num;
  vec.clear();
  for (int i = 0; i < 4; ++i) {
   vec.push_back(num % 10);
   num /= 10;
  }
  reverse(vec.begin(), vec.end());
  cout << " = ";
  print(vec);
  cout << endl;
  sort(vec.begin(), vec.end());
 } while (num_temp != 6174 && num_temp != 0);

}

PAT (Basic Level) Practise (中文) 1018

1018. 锤子剪刀布 (20)

时间限制
100 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
CHEN, Yue
大家应该都会玩“锤子剪刀布”的游戏:两人同时给出手势,胜负规则如图所示:
现给出两人的交锋记录,请统计双方的胜、平、负次数,并且给出双方分别出什么手势的胜算最大。
输入格式:
输入第1行给出正整数N(<=105),即双方交锋的次数。随后N行,每行给出一次交锋的信息,即甲、乙双方同时给出的的手势。C代表“锤子”、J代表“剪刀”、B代表“布”,第1个字母代表甲方,第2个代表乙方,中间有1个空格。
输出格式:
输出第1、2行分别给出甲、乙的胜、平、负次数,数字间以1个空格分隔。第3行给出两个字母,分别代表甲、乙获胜次数最多的手势,中间有1个空格。如果解不唯一,则输出按字母序最小的解。
输入样例:
10
C J
J B
C B
B B
B C
C C
C B
J B
B C
J J
输出样例:
5 3 2
2 3 5
B B

//C++ Program
#include <iostream>
using namespace std;

int main(void) {

 //num_s、num_p是甲的胜、平次数,num_s_c、num_s_j是甲以手势锤子、剪刀胜的次数,num_f_c、num_f_j是甲以手势锤子、剪刀负的次数,其余次数可以计算
 int N, num_s = 0, num_p = 0, num_s_c = 0, num_s_j = 0, num_f_c = 0, num_f_j = 0, num_f, num_s_b, num_f_b, num_s_max, num_f_max;
 char gesture1, gesture2, ges_s_max1, ges_s_max2;//ges_s_max1是甲获胜次数最多的手势,ges_s_max2是乙获胜次数最多的手势
 cin >> N;

 for (int i = 0; i < N; ++i) {
  cin >> gesture1 >> gesture2;
  if (gesture1 == gesture2) {
   ++num_p;
  }
  else if (gesture1 == 'C'&&gesture2 == 'J') {
   ++num_s;
   ++num_s_c;
  }
  else if (gesture1 == 'J'&&gesture2 == 'B') {
   ++num_s;
   ++num_s_j;
  }
  else if (gesture1 == 'B'&&gesture2 == 'C') {
   ++num_s;
  }
  else if (gesture1 == 'C'&&gesture2 == 'B') {
   ++num_f_c;
  }
  else if (gesture1 == 'J'&&gesture2 == 'C') {
   ++num_f_j;
  }
 }

 num_f = N - num_s - num_p;
 num_s_b = num_s - num_s_c - num_s_j;
 num_f_b = num_f - num_f_c - num_f_j;
 num_s_max = num_s_j > num_s_c ? num_s_j : num_s_c;
 ges_s_max1 = num_s_max > num_s_c ? 'J' : 'C';
 num_s_max = num_s_max > num_s_b ? num_s_max : num_s_b;
 ges_s_max1 = num_s_max > num_s_b ? ges_s_max1 : 'B';
 num_f_max = num_f_b > num_f_j ? num_f_b : num_f_j;
 ges_s_max2 = num_f_max > num_f_j ? 'J' : 'C';//注意甲、乙手势区别!
 num_f_max = num_f_max > num_f_c ? num_f_max : num_f_c;
 ges_s_max2 = num_f_max > num_f_c ? ges_s_max2 : 'B';

 cout << num_s << " " << num_p << " " << num_f << endl;
 cout << num_f << " " << num_p << " " << num_s << endl;
 cout << ges_s_max1 << " " << ges_s_max2 << endl;

 return 0;

}

PAT (Basic Level) Practise (中文) 1017

1017. A除以B (20)

时间限制
100 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
CHEN, Yue
本题要求计算A/B,其中A是不超过1000位的正整数,B是1位正整数。你需要输出商数Q和余数R,使得A = B * Q + R成立。
输入格式:
输入在1行中依次给出A和B,中间以1空格分隔。
输出格式:
在1行中依次输出Q和R,中间以1空格分隔。
输入样例:
123456789050987654321 7
输出样例:
17636684150141093474 3

//C++ Program
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main(void) {

 string A;
 int B, left = 0, temp;
 vector<int> v;
 cin >> A >> B;

 for (string::iterator it = A.begin(); it != A.end(); ++it) {
  temp = left * 10 + (*it) - '0';
  if (!(it == A.begin() && temp < B)) {
   v.push_back(temp / B);
  }
  left = temp%B;
 }

 if (v.empty()) {
  v.push_back(0);
 }

 for (vector<int>::iterator it = v.begin(); it != v.end(); ++it) {
  cout << *it;
 }
 cout << " " << left << endl;

 return 0;

}

PAT (Basic Level) Practise (中文) 1016

1016. 部分A+B (15)

时间限制
100 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
CHEN, Yue
正整数A的“DA(为1位整数)部分”定义为由A中所有DA组成的新整数PA。例如:给定A = 3862767,DA = 6,则A的“6部分”PA是66,因为A中有2个6。
现给定A、DA、B、DB,请编写程序计算PA + PB
输入格式:
输入在一行中依次给出A、DA、B、DB,中间以空格分隔,其中0 < A, B < 1010
输出格式:
在一行中输出PA + PB的值。
输入样例1:
3862767 6 13530293 3
输出样例1:
399
输入样例2:
3862767 1 13530293 8
输出样例2:
0

//C++ Program
#include <iostream>
using namespace std;

long long P(long long N, int Dn);

int main(void) {

 long long A, B;
 int Da, Db;

 cin >> A >> Da >> B >> Db;

 cout << P(A, Da) + P(B, Db) << endl;

 return 0;

}

long long P(long long N, int Dn) {
 int i, Pn = 0;
 do {
  i = N % 10;
  if (i == Dn) {
   Pn = Pn * 10 + Dn;
  }
  N /= 10;
 } while (N > 0);
 return Pn;
}

PAT (Basic Level) Practise (中文) 1015

1015. 德才论 (25)

时间限制
200 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
CHEN, Li
宋代史学家司马光在《资治通鉴》中有一段著名的“德才论”:“是故才德全尽谓之圣人,才德兼亡谓之愚人,德胜才谓之君子,才胜德谓之小人。凡取人之术,苟不得圣人,君子而与之,与其得小人,不若得愚人。”
现给出一批考生的德才分数,请根据司马光的理论给出录取排名。
输入格式:
输入第1行给出3个正整数,分别为:N(<=105),即考生总数;L(>=60),为录取最低分数线,即德分和才分均不低于L的考生才有资格被考虑录取;H(<100),为优先录取线——德分和才分均不低于此线的被定义为“才德全尽”,此类考生按德才总分从高到低排序;才分不到但德分到线的一类考生属于“德胜才”,也按总分排序,但排在第一类考生之后;德才分均低于H,但是德分不低于才分的考生属于“才德兼亡”但尚有“德胜才”者,按总分排序,但排在第二类考生之后;其他达到最低线L的考生也按总分排序,但排在第三类考生之后。
随后N行,每行给出一位考生的信息,包括:准考证号、德分、才分,其中准考证号为8位整数,德才分为区间[0, 100]内的整数。数字间以空格分隔。
输出格式:
输出第1行首先给出达到最低分数线的考生人数M,随后M行,每行按照输入格式输出一位考生的信息,考生按输入中说明的规则从高到低排序。当某类考生中有多人总分相同时,按其德分降序排列;若德分也并列,则按准考证号的升序输出。
输入样例:
14 60 80
10000001 64 90
10000002 90 60
10000011 85 80
10000003 85 80
10000004 80 85
10000005 82 77
10000006 83 76
10000007 90 78
10000008 75 79
10000009 59 90
10000010 88 45
10000012 80 100
10000013 90 99
10000014 66 60
输出样例:
12
10000013 90 99
10000012 80 100
10000003 85 80
10000011 85 80
10000004 80 85
10000007 90 78
10000006 83 76
10000005 82 77
10000002 90 60
10000014 66 60
10000008 75 79
10000001 64 90

//C++ Program
#include <cstdio>//为了效率!
//#include <iostream>
#include <list>
using namespace std;

//为了利用sort函数!
struct student {
 int number;
 int morality;
 int ability;
 int total;
};

bool compare(student stu1, student stu2);

int main(void) {

 int N, L, H, M = 0, type;
 scanf("%d %d %d", &N, &L, &H);
 //cin >> N >> L >> H;
 student stu;
 list<student> li[4];

 for (int i = 0; i < N; ++i) {
  scanf("%d %d %d", &stu.number, &stu.morality, &stu.ability);
  //cin >> stu.number >> stu.morality >> stu.ability;
  if (stu.morality >= L&&stu.ability >= L) {
   stu.total = stu.morality + stu.ability;//总分是算出来的!
   ++M;
   if (stu.morality >= H&&stu.ability >= H) {
    type = 0;
   }
   else if (stu.morality >= H&&stu.ability < H) {
    type = 1;
   }
   else if (stu.morality >= stu.ability) {
    type = 2;
   }
   else {
    type = 3;
   }
   li[type].push_back(stu);//将考生归类
  }
 }

 printf("%d\n", M);
 //cout << M << endl;
 for (int i = 0; i < 4; ++i) {
  li[i].sort(compare);//排序
  for (list<student>::iterator it = li[i].begin(); it != li[i].end(); ++it) {
   printf("%d %d %d\n", (*it).number, (*it).morality, (*it).ability);
   //cout << (*it).number << " " << (*it).morality << " " << (*it).ability << endl;
  }
 }

 return 0;

}

//比较函数,判断考生1是否在考生2之前,只需在同一类中进行比较
bool compare(student stu1, student stu2) {
 if (stu1.total > stu2.total) {
  return true;
 }
 if (stu1.total == stu2.total&&stu1.morality > stu2.morality) {
  return true;
 }
 if (stu1.total == stu2.total&&stu1.morality == stu2.morality) {
  return stu1.number < stu2.number;
 }
 return false;
}

PAT (Basic Level) Practise (中文) 1014

1014. 福尔摩斯的约会 (20)

时间限制
100 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
CHEN, Yue
大侦探福尔摩斯接到一张奇怪的字条:“我们约会吧! 3485djDkxh4hhGE 2984akDfkkkkggEdsb s&hgsfdk d&Hyscvnm”。大侦探很快就明白了,字条上奇怪的乱码实际上就是约会的时间“星期四 14:04”,因为前面两字符串中第1对相同的大写英文字母(大小写有区分)是第4个字母'D',代表星期四;第2对相同的字符是'E',那是第5个英文字母,代表一天里的第14个钟头(于是一天的0点到23点由数字0到9、以及大写字母A到N表示);后面两字符串第1对相同的英文字母's'出现在第4个位置(从0开始计数)上,代表第4分钟。现给定两对字符串,请帮助福尔摩斯解码得到约会的时间。
输入格式:
输入在4行中分别给出4个非空、不包含空格、且长度不超过60的字符串。
输出格式:
在一行中输出约会的时间,格式为“DAY HH:MM”,其中“DAY”是某星期的3字符缩写,即MON表示星期一,TUE表示星期二,WED表示星期三,THU表示星期四,FRI表示星期五,SAT表示星期六,SUN表示星期日。题目输入保证每个测试存在唯一解。
输入样例:
3485djDkxh4hhGE 
2984akDfkkkkggEdsb 
s&hgsfdk 
d&Hyscvnm
输出样例:
THU 14:04

//C++ Program
#include <iostream>
#include <string>
#include <cctype>
using namespace std;

int main(void) {

 string s[4];
 string day;
 int d, h, m, i;
 char ctemp;
 for (int i = 0; i < 4; ++i) {
  cin >> s[i];
 }

 for (i = 0; i < s[0].size(); ++i) {
  ctemp = s[0].at(i);
  //星期一到星期日,一共只有七种可能(最多到G)
  if (ctemp == s[1].at(i) && isupper(ctemp) && ctemp <= 'G') {
   d = ctemp - 'A' + 1;
   break;
  }
 }
 for (++i; i < s[0].size(); ++i) {
  ctemp = s[0].at(i);
  //0点到23点,最多到N
  if (ctemp == s[1].at(i) && (isdigit(ctemp) || (isupper(ctemp) && ctemp <= 'N'))) {
   if (isdigit(ctemp)) {
    h = ctemp - '0';
   }
   else {
    h = ctemp - 'A' + 10;
   }
   break;
  }
 }

 for (i = 0; i < s[2].size(); ++i) {
  ctemp = s[2].at(i);
  if (ctemp == s[3].at(i) && isalpha(ctemp)) {
   m = i;
   break;
  }
 }

 switch (d)
 {
 case 1:
  day = "MON";
  break;
 case 2:
  day = "TUE";
  break;
 case 3:
  day = "WED";
  break;
 case 4:
  day = "THU";
  break;
 case 5:
  day = "FRI";
  break;
 case 6:
  day = "SAT";
  break;
 case 7:
  day = "SUN";
  break;
 default:
  break;
 }

 cout << day << " ";
 cout.fill('0');
 cout.width(2);
 cout << h << ":";
 cout.fill('0');
 cout.width(2);
 cout << m << endl;

 return 0;

}

PAT (Basic Level) Practise (中文) 1013

1013. 数素数 (20)

时间限制
100 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
CHEN, Yue
令Pi表示第i个素数。现任给两个正整数M <= N <= 104,请输出PM到PN的所有素数。
输入格式:
输入在一行中给出M和N,其间以空格分隔。
输出格式:
输出从PM到PN的所有素数,每10个数字占1行,其间以空格分隔,但行末不得有多余空格。
输入样例:
5 27
输出样例:
11 13 17 19 23 29 31 37 41 43
47 53 59 61 67 71 73 79 83 89
97 101 103

//C++ Program
#include <iostream>
#include <cmath>
using namespace std;

bool is_prime(int n);

int main(void) {

 int M, N, num = 0;
 cin >> M >> N;

 for (int i = 2; num < N; ++i) {
  if (is_prime(i)) {
   ++num;
   if (num >= M) {
    cout << i;
    if ((num - M + 1) % 10 == 0) {
     cout << endl;
    } else if (num < N) {
     cout << " ";
    }
    else {
     cout << endl;
    }
   }
  }
  if (i > 2) {
   ++i;
  }
 }

 return 0;
}

bool is_prime(int n) {

 for (int i = 2; i <= sqrt(n); ++i) {
  if (n%i == 0) {
   return false;
  }
 }
 return true;

}

PAT (Basic Level) Practise (中文) 1012

1012. 数字分类 (20)

时间限制
100 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
CHEN, Yue
给定一系列正整数,请按要求对数字进行分类,并输出以下5个数字:
  • A1 = 能被5整除的数字中所有偶数的和;
  • A2 = 将被5除后余1的数字按给出顺序进行交错求和,即计算n1-n2+n3-n4...;
  • A3 = 被5除后余2的数字的个数;
  • A4 = 被5除后余3的数字的平均数,精确到小数点后1位;
  • A5 = 被5除后余4的数字中最大数字。
    输入格式:
    每个输入包含1个测试用例。每个测试用例先给出一个不超过1000的正整数N,随后给出N个不超过1000的待分类的正整数。数字间以空格分隔。
    输出格式:
    对给定的N个正整数,按题目要求计算A1~A5并在一行中顺序输出。数字间以空格分隔,但行末不得有多余空格。
    若其中某一类数字不存在,则在相应位置输出“N”。
    输入样例1:
    13 1 2 3 4 5 6 7 8 9 10 20 16 18
    
    输出样例1:
    30 11 2 9.7 9
    
    输入样例2:
    8 1 2 4 5 6 7 9 16
    
    输出样例2:
    N 11 2 N 9
  • 
    
  • //C++ Program
    #include <iostream>
    #include <cstring>
    #include <iomanip>
    using namespace std;
    
    int main(void) {
    
     int N, num, sign = -1, count = 0;
     float A[5] = { 0, 0, 0, 0, 0};//利用数组比较方便,下标与题目略有不同
     bool exist[5];//检测各类数字是否存在
     memset(exist, false, 5);//初始化
     cin >> N;
    
     for (int i = 0; i < N; ++i) {
      cin >> num;
      switch (num % 5) {
      case 0:
       if (num % 2 == 0) {
        if (!exist[0]) {
         exist[0] = true;
        }
        A[0] += num;
       }
       break;
      case 1:
       if (!exist[1]) {
        exist[1] = true;
       }
       sign *= -1;
       A[1] += (sign*num);
       break;
      case 2:
       if (!exist[2]) {
        exist[2] = true;
       }
       ++A[2];
       break;
      case 3:
       if (!exist[3]) {
        exist[3] = true;
       }
       ++count;
       A[3] += num;
       break;
      case 4:
       if (!exist[4]) {
        exist[4] = true;
       }
       if (num > A[4]) {
        A[4] = num;
       }
      }
     }
    
     A[3] /= count;
    
     for (int i = 0; i < 5; ++i) {
      if (i > 0) {
       cout << " ";
      }
      if (exist[i]) {
       if (i == 3) {
        cout << fixed<<setprecision(1);//精确到小数点后1位
       }
       else {
        cout << fixed << setprecision(0);
       }
       cout << A[i];
      }
      else {
       cout << "N";
      }
     }
    
     cout << endl;
    
     return 0;
    }