当前位置: 代码迷 >> 综合 >> PAT乙级 1017 A除以B (20分) 1018 锤子剪刀布 (20分) 1019 数字黑洞 (20分)
  详细解决方案

PAT乙级 1017 A除以B (20分) 1018 锤子剪刀布 (20分) 1019 数字黑洞 (20分)

热度:38   发布时间:2023-12-17 14:15:59.0

1017 A除以B (20分)

其实我觉得这题就不简单,可能是我没有刷过题目,使用正常的手算的方式来解题即可。这个位置是最关键的。把第i位或者第i-1位的余数作为被除数和除数相除。

int temp1 = temp / b;
aa[i] = temp1;//除数的结果保存到aa
temp = temp % b * 10 + (a[i + 1] - '0');
#include<iostream>
#include<vector>
#include<string>
using namespace std;int main()
{string a;int b = 0;int aa[1001] = { 0 };cin >> a >> b;int jieguo=0;int temp = (a[0]-'0');//保留余数,同时作为被除数的第一位if (a.size() == 1){jieguo = (a[0] - '0') / b;temp = (a[0] - '0') % b;cout << jieguo << " " << temp;return 0;}for (int i = 0; i < a.size(); i++){if (i != a.size() - 1)//当到a.size()时会数组越界需要执行else{int temp1 = temp / b;aa[i] = temp1;//除数的结果保存到aatemp = temp % b * 10 + (a[i + 1] - '0');}else{aa[i] = temp / b;temp = temp % b;}}if (aa[0] == 0)//使用上述方法输出结果会存在第一位为0的情况所以需要区分{for (int i = 1; i < a.size(); i++)cout << aa[i];cout << " " << temp;}else{for (int i = 0; i < a.size(); i++)cout << aa[i];cout << " " << temp;}return 0;
}

1018 锤子剪刀布 (20分)

如果你是测试点1没有通过说明你最后的输出获胜次数最多的手势有问题。我直接用最粗暴的方式确定手势,题目的意思是要是有相同的胜利次数就按照,bcj的顺序输出

    if (b[2] >= b[0] && b[2] >= b[1])maxjia = 2;if (b[0] > b[2] && b[0] >= b[1])maxjia = 0;if (b[1] > b[0] && b[1] > b[2])maxjia = 1;if (b[5] >= b[3] && b[5] >= b[4])maxyi = 5;if (b[3] > b[5] && b[3] >= b[4])maxyi = 3;if (b[4] > b[3] && b[4] > b[5])maxyi = 4;

全部代码:

#include<iostream>
#include<vector>
#include<string>
using namespace std;int main()
{int sum = 0;cin >> sum;int a[6] = { 0 };//前三个数字表示甲赢平输的个数012,后三个表示乙的345int b[6] = { 0 };//前三个表示甲用石头剪刀布胜利的个数012,后三个表示乙的345for (int i = 0; i < sum; i++)//c>j>b>c{char jia, yi;cin >> jia >> yi;if (jia == yi){a[1]++; a[4]++; continue;}if (jia == 'C'){if(yi=='J'){a[0]++; a[5]++; b[0]++; continue;}else{a[2]++; a[3]++; b[5]++; continue;}}if (jia == 'J'){if (yi == 'C'){a[3]++; a[2]++; b[3]++; continue;}else{a[0]++; a[5]++; b[1]++; continue;}}if (jia == 'B'){if (yi == 'C'){a[0]++; a[5]++; b[2]++; continue;}else{a[3]++; a[2]++; b[4]++; continue;}}}int maxjia=2,maxyi=5;cout << a[0] << " " << a[1] << " " << a[2] << "\n" << a[3] << " " << a[4] << " " << a[5] << "\n";if (b[2] >= b[0] && b[2] >= b[1])maxjia = 2;if (b[0] > b[2] && b[0] >= b[1])maxjia = 0;if (b[1] > b[0] && b[1] > b[2])maxjia = 1;if (b[5] >= b[3] && b[5] >= b[4])maxyi = 5;if (b[3] > b[5] && b[3] >= b[4])maxyi = 3;if (b[4] > b[3] && b[4] > b[5])maxyi = 4;if (maxjia == 0)cout << "C" << " ";if (maxjia == 1)cout << "J" << " ";if (maxjia == 2)cout << "B" << " ";if (maxyi == 3)cout << "C";if (maxyi == 4)cout << "J";if (maxyi == 5)cout << "B";return 0;
}

1019 数字黑洞 (20分)

测试点5需要输入是6174的时候输出一行,否则出错即:

如果忽略前缀为0的条件则测试点234错误即:0001,0023,0456

#include<iostream>
#include<vector>
#include<string>
using namespace std;
void swap(char* a, char* b) //交换两个变量
{char temp = *a;*a = *b;*b = temp;
}string up(string a)//从小到大
{for (int i = 0; i < 3; i++){int min = i;for (int j = i + 1; j < 4; j++)if (a[j] < a[min])min = j;swap(&a[min], &a[i]);}return a;
}
string down(string a)//从大到小
{for (int i = 0; i < 3; i++){int min = i;for (int j = i + 1; j < 4; j++)if (a[j] > a[min])min = j;swap(&a[min], &a[i]);}return a;
}int main()
{string a;cin >> a;if (a.length() == 3) {//出现输入只有123位数时补充位数,否则数组越界a = a.insert(0, "0");}else if (a.length() == 2) {a = a.insert(0, "00");}else if (a.length() == 1) {a = a.insert(0, "000");}while (true){if (a[0] == a[1] && a[1] == a[2] && a[2] == a[3]){printf("%04d - %04d = 0000", stoi(a), stoi(a));//cout << stoi(a) << " - " << stoi(a) << " = " << "0000";break;}else{int max = stoi(down(a)),min=stoi(up(a));printf("%04d - %04d = %04d\n", max,min,max-min);//cout << max << " - " << min << " = " << max-min<<"\n";a = to_string(max - min);if (a.length() == 3) {//出现输入只有123位数时补充位数,否则数组越界a = a.insert(0, "0");}else if (a.length() == 2) {a = a.insert(0, "00");}else if (a.length() == 1) {a = a.insert(0, "000");}if (a == "6174")break;}}return 0;
}