PAT乙級1001-1010
阿新 • • 發佈:2018-12-24
1001,3n+1猜想
注意main函式的入口引數
#include<iostream>
int main(int argc,char*argv[])
{
using namespace std;
int input=0;
int res=0;
cin>>input;
while(input!=1)
{
if(input%2==0)
input/=2;
else
input=(input*3+1)/2;
res++;
}
cout<<res;
return 0;
}
1002,寫出這個數
字串中字元與數字的轉換
#include<iostream> #include<string> using namespace std; int main(int argc,char*argv[]) { string str[10] = { "ling","yi","er","san","si","wu","liu","qi","ba","jiu" }; string input; int res = 0; cin >> input; int index = 0; while (input[index] != '\0') { res += input[index] - 48; index++; } string result; if (res == 0) result = "ling"; else { result = str[res % 10]; res /= 10; while (res != 0) { result = str[res % 10]+" " + result; res /= 10; } } cout << result; return 0; }
1003,我要通過(此題非原創)
注意判斷條件中前面每多一個A後面就多兩個A,中間的A任意多
1004,成績排名#include<iostream> #include<string> #include<vector> using namespace std; void isPat(string str); int main(int argc, char*argv[]) { int sum; cin >> sum; vector<string>test(sum); for (int i = 0; i < sum; i++) cin >> test[i]; for (int i = 0; i < sum; i++) isPat(test[i]); return 0; } void isPat(string str) { int countP = 0; int countT = 0; int localP = 0; int localT = 0; bool status = true; for (int i = 0; i < str.length(); i++) { if (str[i] != 'P'&&str[i] != 'A'&&str[i] != 'T') status = false; if(str[i]=='P') { countP++; localP = i; } if (str[i] == 'T') { countT++; localT = i; } } if (countP == 1 && countT == 1 && status && (localT - localP-1) >=1 && localP*(localT - localP - 1) == (str.length() - 1 - localT)) cout << "YES" << endl; else cout << "NO" << endl; }
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main(int argc, char*argv[])
{
int sum=0;
cin >> sum;
vector<string>name(sum);
vector<string>number(sum);
vector<int>score(sum);
for (int i = 0; i < sum; i++)
{
cin >> name[i] >> number[i] >> score[i];
}
int high = 0;
int low = 0;
int score_high = 0;
int score_low = 100;
for (int i = 0; i < sum; i++)
{
if (score[i] > score_high)
{
high = i; score_high = score[i];
}
if (score[i] < score_low)
{
low = i; score_low = score[i];
}
}
cout << name[high] << " " << number[high] << endl;
cout << name[low] << " " << number[low] << endl;
return 0;
}
1005,繼續3n+1猜想(非原創)
注意使用演算法庫中的sort函式
#include <iostream>
#include <algorithm> //sort
using namespace std;
int cmp(int a, int b) { return a > b; } // 降序排序
int main() {
int n(0);
cin >> n;
int *a = new int[n]; //C++
for (int i = 0; i<n; i++) {
cin >> a[i];
}
int t;
sort(a, a + n, cmp);
for (int i = 0; i<n; i++) {
t = a[i];
while (t != 1 && t != 999) {
if (t % 2 == 0) {
t = t / 2;
}
else {
t = (3 * t + 1) / 2;
}
for (int j = 0; j<n; j++) {
if (t == a[j] && j != i) {
a[j] = 999;
}
}
}
}
sort(a, a + n, cmp);
int temp(0);
for (int i = n - 1; i >= 0; i--) {
if (a[i] != 999) {
temp = i; // temp是第一個不等於999的下標
}
}
for (int i = temp; i<n - 1; i++) {
cout << a[i] << " ";
}
cout << a[n - 1];
delete[] a; // c++
return 0;
}
1006,換個格式輸出整數
字元與數字的轉換
#include<iostream>
#include<string>
using namespace std;
int main(int argc, char*argv[])
{
int num = 0;
cin >> num;
string out;
int tem = num % 10;
num /= 10;
for (int i = 0; i<tem; i++)
out += (char)(i + 49);
tem = num % 10;
num /= 10;
for (int i = 0; i<tem; i++)
out = 'S' + out;
for (int i = 0; i<num; i++)
out = 'B' + out;
cout << out << endl;
return 0;
}
1007,素數對猜想
注意判斷素數只需是否整除2到平方根之間的數,
#include<iostream>
#include<cmath>
#include<vector>
using namespace std;
bool is_prim(int a);
int main(int argc, char*argv[])
{
int num = 0;
cin >> num;
int count = 0;
vector<int>prims;
for (int i = 2; i <= num; i++)
if (is_prim(i))
prims.push_back(i);
for (int i = 0; i < prims.size() - 1; i++)
if ((prims[i + 1] - prims[i]) == 2)
count++;
cout << count << endl;
return 0;
}
bool is_prim(int a)
{
for (int i = 2; i<=sqrt(a); i++)
if (a%i == 0)
return false;
return true;
}
1008,連續移動陣列
每一個元素只需移動一次,使用了eculid演算法求最大公因子。
#include<iostream>
#include<string>
using namespace std;
int eculid(int a, int b);
int main(int argc, char*argv[])
{
int sum;
int move;
cin >> sum>>move;
int *arr = new int[sum];
for (int i = 0; i < sum; i++)
cin >> arr[i];
while (move > sum)
move -= sum;
int groups = eculid(sum, move);
int step = sum / groups;
for (int i = 0; i < groups; i++)
{
int s = 0;
int b = i;
int temp = arr[b];
while (s < step-1)
{
int a = b < move ? b + sum - move : b - move;
arr[b] = arr[a];
b = a;
s++;
}
arr[b] = temp;
}
for (int i = 0; i < sum-1; i++)
cout << arr[i] << " ";
cout << arr[sum - 1];
delete[]arr;
return 0;
}
int eculid(int a, int b)
{
while (b != 0)
{
int r = a%b;
a = b;
b = r;
}
return a;
}
1009,說反話
注意使用了stack
#include <iostream>
#include<string>
#include <stack>
using namespace std;
int main() {
stack<string> s;
string a;
cin >> a;
s.push(a);
while (cin.get() != '\n')
{
cin >> a;
s.push(a);
}
cout << s.top();
s.pop();
while (!s.empty()) {
cout << " " << s.top();
s.pop();
}
return 0;
}
1010,一元多項式求導
注意零多項式求導還是零
#include<iostream>
#include<vector>
using namespace std;
int main(int argc, char*argv[])
{
vector<int>num;
vector<int>index;
int a, b;
while (cin >> a >> b)
{
num.push_back(a);
index.push_back(b);
}
for (int i = 0; i < num.size(); i++)
{
num[i] *= index[i];
index[i]--;
}
if (index[0] == -1)
cout << "0 0";
else
cout << num[0] << " " << index[0];
for (int i = 1; i < num.size(); i++)
{
if (index[i] != -1)
cout << " " << num[i] << " " << index[i];
}
return 0;
}