PAT (Basic Level) Practice (中文)C/C++練習(一)15分整理
阿新 • • 發佈:2018-12-16
本文為博主練習基礎C語言時,在PTA平臺上做的簡單練習,答案僅僅能通過測試,不一定沒有錯誤。按分值順序排列。
PTA(Basic Level) Practice (中文):https://pintia.cn/problem-sets/994805260223102976/problems
1001 害死人不償命的(3n+1)猜想 (15 分)
#include<iostream> using namespace std; int main() { int n, count=0; cin >> n; if (n > 1000) return -1; while (n != 1) { if (n % 2 == 0) { n /= 2; } else { n = (3 * n + 1) / 2; } count++; } cout << count; return 0; }
1006 換個格式輸出整數 (15 分)
#include<iostream> using namespace std; int main() { int n,i,j,k; //數,百,十,個 cin >> n; if (n > 999) { return -1; } else if (n > 99) //三位數 { i = n / 100; j = n / 10 % 10; k = n % 10; for (int l = 0; l < i; l++) { cout << 'B'; } for (int l = 0; l < j; l++) { cout << 'S'; } } else if (n > 9) //兩位數 { j = n / 10; k = n % 10; for (int l = 0; l < j; l++) { cout << 'S'; } } else if (n > -1) //個位數 { k = n; } else return -1; for (int l = 1; l <= k; l++) { cout << l; } return 0; }
1011 A+B 和 C (15 分)
#include<iostream> using namespace std; #define SIZE 10 int main() { int T; long A[SIZE],B[SIZE],C[SIZE]; cin >> T; if (T <= 0) return 0; for (int i = 0; i < T; i++) { cin >> A[i] >> B[i] >> C[i]; } for (int i = 0; i < T; i++) { if (A[i] + B[i] > C[i]) { if(i>0) cout << endl; cout << "Case #" << i+1 << ": true"; } else { if (i > 0) cout << endl; cout << "Case #" << i+1 << ": false"; } } return 0; }
1016 部分A+B (15 分)
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
char A[11], B[11];
char Da, Db;
int count = 0;
int Pa = 0, Pb = 0;
cin >> A >> Da >> B >> Db;
for (int i = 0; A[i] != '\0'; i++)
{
if (Da == A[i])
{
Pa += (Da - '0') * pow(10, count);
count++;
}
}
count = 0;
for (int i = 0; B[i] != '\0'; i++)
{
if (Db == B[i])
{
Pb += (Db - '0') * pow(10, count);
count++;
}
}
cout << Pa + Pb;
return 0;
}
1021 個位數統計 (15 分)
#include<iostream>
using namespace std;
int main()
{
char N[1001];
char D[10] = { '0','1', '2', '3', '4', '5', '6', '7', '8', '9' };
int M[10] = { 0 };
cin >> N;
for (int i = 0; N[i] != '\0'; i++)
{
for (int j = 0; j < 10; j++)
{
if (D[j] == N[i])
{
M[j]++;
break;
}
}
}
for (int i = 0; i < 10; i++)
{
if (M[i] != 0)
cout << D[i] << ":" << M[i] << endl;
}
return 0;
}
1026 程式執行時間 (15 分)
#include<iostream>
using namespace std;
int main()
{
long C1, C2;
long hours, mintinues,seconds ,sum1;
double sum2;
cin >> C1 >> C2;
sum1 = (C2 - C1) / 100;
sum2 = (C2 - C1) / 100.0;
if (sum1 + 0.5 <= sum2)
sum1++;
hours = sum1 / 3600;
mintinues = (sum1 - hours * 3600) / 60;
seconds = sum1 - hours * 3600 - mintinues * 60;
//second = sum1%60;
if (hours <= 9)
cout << '0' << hours << ":";
else
cout << hours << ":";
if (mintinues <= 9)
cout << '0' << mintinues << ":";
else
cout << mintinues << ":";
if (seconds <= 9)
cout << '0' << seconds;
else
cout << seconds;
return 0;
}
1031 查驗身份證 (15 分)
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
int N=0;
char idCard[105][20] = {'0'};
int weight[20] = { 7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2 };
char M[12] = { '1', '0', 'X', '9', '8' ,'7', '6', '5', '4' ,'3' ,'2' };
int sum = 0;
int output[105] = { 0 };
int flag = 0;
scanf("%d", &N);
getchar();
for (int i = 1; i <= N; i++)
{
gets(idCard[i]);
}
for (int i = 1; i <= N; i++)
{
sum = 0;
for (int j = 0; j < 17; j++)
{
if (idCard[i][j] < '0' || idCard[i][j] > '9')
{
output[i] = 0;
flag = 1;
break;
}
sum += (idCard[i][j] - '0') * weight[j];
}
if (flag == 1)
{
flag = 0;
continue;
}
//sum %= 11;
if (M[sum%11] == idCard[i][17])
{
output[i] = i;
}
}
for (int i = 1; i <= N; i++)
{
if (output[i] == 0)
{
puts(idCard[i]);
flag = 1;
}
}
if (flag == 0)
{
printf("All passed");
}
return 0;
}
1036 跟奧巴馬一起程式設計 (15 分)
#include<stdio.h>
int main()
{
int N, count = 1;
char C;
scanf("%d %c", &N, &C);
for (int i = 1; i <= (N % 2 == 0 ? N / 2 : (N + 1) / 2); i++)
{
if (count == 1 || count == (N % 2 == 0 ? N / 2 : (N + 1) / 2))
{
for (int j = 1; j <= N; j++)
{
printf("%c",C);
}
printf("\n");
count++;
}
else
{
printf("%c", C);
for (int j = 2; j <= N-1; j++)
{
printf(" ");
}
printf("%c\n", C);
count++;
}
}
return 0;
}
1041 考試座位號 (15 分)
#include <stdio.h>
#include <math.h>
#define MAX 1000
struct student
{
char id[15];
int a;
int b;
}stu[MAX];
int main()
{
int N,M;
int num[MAX];
scanf("%d", &N);
for (int i = 1; i <= N; i++)
{
scanf("%s %d %d", &stu[i].id, &stu[i].a, &stu[i].b);
}
/***********************安全監測部分***********************/
for (int i = 1; i < N; i++)
{
for (int j = 0; stu[i].id[j] != '\0'; j++)//准考證號由 14 位數字組成,座位從 1 到 N 編號。
{
if (!isdigit(stu[i].id[j]))
{
return 0;
}
}
if (!strcmp(stu[i].id, stu[i - 1].id))//輸入保證每個人的准考證號都不同
{
return 0;
}
if (stu[i].a == stu[i - 1].a || stu[i].b == stu[i - 1].b)//任何時候都不會把兩個人分配到同一個座位上。
{
return 0;
}
}
/***********************安全監測部分***********************/
scanf("%d", &M);
for (int i = 1; i <= M; i++)
{
scanf("%d",&num[i]);
}
for (int i = 1; i <= M; i++)
{
for (int j = 1; j <= N; j++)
{
if (num[i] == stu[j].a)
{
printf("%s %d\n", stu[j].id, stu[j].b);
}
}
}
return 0;
}
1046 划拳 (15 分)
#include <stdio.h>
#define MAX 101
int main()
{
int N;
int Speak_a, Do_a, Speak_b, Do_b, Count_a = 0, Count_b = 0;
scanf("%d", &N);
for (int i = 0; i < N; i++)
{
scanf("%d %d %d %d", &Speak_a, &Do_a, &Speak_b, &Do_b);
if ((Do_a == Speak_a + Speak_b) && (Do_b != Speak_a + Speak_b))
{
Count_b++;
}
if ((Do_b == Speak_a + Speak_b) && (Do_a != Speak_a + Speak_b))
{
Count_a++;
}
}
printf("%d %d", Count_a, Count_b);
return 0;
}
1051 複數乘法 (15 分)
#include <stdio.h>
#include <math.h>
#define MAX 101
int main()
{
double r1, p1, r2, p2;
double a, b;
scanf("%lf%lf%lf%lf", &r1, &p1, &r2, &p2);
a = r1 * r2*(cos(p1)*cos(p2) - sin(p1)*sin(p2));
b = r1 * r2*(sin(p2)*cos(p1) + sin(p1)*cos(p2));
if (a > -0.005&&a < 0)
printf("0.00");
else
printf("%.2lf",a);
if (b > -0.005&&b < 0)
printf("+0.00i");
else if(b<=-0.005)
printf("%.2lfi",b);
else
printf("+%.2lfi",b);
return 0;
}
1056 組合數的和 (15 分)
#include <stdio.h>
#include <math.h>
#define MAX 101
int main()
{
int N = 0;
int t = 0, count = 0, sum = 0;
int num[MAX] = { 0 }; //原始資料
int sort_num[MAX] = { 0 }; //排序後每個資料只出現一次
scanf("%d", &N);
for (int i = 0; i < N; i++)
{
scanf("%d", &num[i]);
}
for (int i = 0; i < N - 1; i++)
for (int j = i+1; j < N; j++)
{
if (num[i] > num[j])
{
t = num[i];
num[i] = num[j];
num[j] = t;
}
}
/*for(int i=0;i<N;i++)
printf("%d", num[i]);
printf("\n");*/
for (int i = 1; i <= N; i++)
{
t = num[i - 1];
if (num[i] != t)
{
sort_num[count] = t;
count++;
}
}
/*for (int i = 0; i < count; i++)
printf("%d", sort_num[i]);*/
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
if(i!=j)
sum += sort_num[i] * 10 + sort_num[j];
}
}
printf("%d", sum);
return 0;
}
1061 判斷題 (15 分)
#include <stdio.h>
#include <math.h>
#define MAX 101
int main()
{
int N = 0;
int M = 0;
int t = 0;
int score[MAX] = { 0 };
int answer[MAX] = { 0 };
int student[MAX] = { 0 };
scanf("%d %d", &N, &M);
for (int i = 0; i < M; i++)
{
scanf("%d", &score[i]);
if (score[i] > 5 || score[i] < 0)
return 0;
}
for (int i = 0; i < M; i++)
{
scanf("%d", &answer[i]);
}
for (int i = 0; i < N; i++)
{
for (int j = 0; j < M; j++)
{
scanf("%d", &t);
if (t == answer[j])
{
student[i] += score[j];
}
}
}
for (int i = 0; i < N; i++)
{
printf("%d\n", student[i]);
}
return 0;
}
1066 影象過濾 (15 分)
#include <stdio.h>
#include <math.h>
int main()
{
int M = 0;
int N = 0;
int A = 0, B = 0;
int t = 0;
int values = 0;
scanf("%d%d%d%d%d", &M, &N, &A, &B, &t);
for (int i = 0; i < M; i++)
{
for (int j = 0; j < N; j++)
{
scanf("%d", &values);
if (values >= A && values <= B)
values = t;
if (j != 0)
printf(" ");
printf("%03d", values);
}
printf("\n");
}
return 0;
}
1071 小賭怡情 (15 分)
#include<bits/stdc++.h>
using namespace std;
int s,k,i,n1,n2,t,b;
int main()
{
cin>>s>>k;
for(i=1;i<=k;i++)
{
cin>>n1>>b>>t>>n2;
if(s==0)
{
printf("Game Over.");
return 0;
}
if(s>=t)
{
if((n2<n1&&b==0)||(n2>n1&&b==1))
{
s+=t;
printf("Win %d! Total = %d.\n",t,s);
}
else
{
s-=t;
printf("Lose %d. Total = %d.\n",t,s);
}
}
else if(s<t)
{
printf("Not enough tokens. Total = %d.\n",s);
}
}
return 0;
}
1076 Wifi密碼 (15 分)
/*
#include<iostream>
using namespace std;
int main() {
int n;
cin >> n;
string str;
for (int i = 0; i < n * 4; i++) {
cin >> str;
if (str[2] == 'T') cout << (int)str[0] - 'A' + 1;
}
return 0;
}*/
#include <stdio.h>
#define MAX 501
#define SIZE 5
int main()
{
int N = 0;
char str[MAX][SIZE];
scanf("%d", &N);
if (N > 100 || N < 0)
return 0;
for (int i = 0; i < 4 * N; i++)
{
scanf("%s", str[i]);
if (str[i][2] == 'T')
{
if (str[i][0] == 'A')
putchar('1');
else if (str[i][0] == 'B')
putchar('2');
else if (str[i][0] == 'C')
putchar('3');
else if (str[i][0] == 'D')
putchar('4');
}
}
return 0;
}
1081 檢查密碼 (15 分)
#include<stdio.h>
#include<string.h>
int main()
{
int N=0;
int i=0,j=0,k=0;
char str[100];
int digit=0,letter=0,dot=0,other=0;
scanf("%d",&N);
getchar();
for(i=0;i<N;i++)
{
digit=0;letter=0;dot=0;other=0;
gets(str);
if(strlen(str)<6)
{
printf("Your password is tai duan le.\n");
}
else
{
for(j=0;str[j]!='\0';j++)
{
if(str[j]>='0'&&str[j]<='9')
{
digit = 1;
}
else if((str[j]>='A'&&str[j]<='Z')||(str[j]>='a'&&str[j]<='z'))
{
letter = 1;
}
else if(str[j]=='.')
{
dot = 1;
}
else
{
other = 1;
}
}
if(other==1)
{
printf("Your password is tai luan le.\n");
}
else
{
if(digit==1&&letter==0)
{
printf("Your password needs zi mu.\n");
}
else if(digit==0&&letter==1)
{
printf("Your password needs shu zi.\n");
}
else
printf("Your password is wan mei.\n");
}
}
}
return 0;
}
1086 就不告訴你 (15 分)
#include <stdio.h>
#include <math.h>
#define MAX 8
int main()
{
int A = 0, B = 0;
int t = 0;
int wei[MAX] = { 0 }; //0-6:個 十 百 千 萬 十萬 百萬
int result = 0;
scanf("%d%d",&A, &B);
if (A > 1000 || B > 1000)
return 0;
t = A * B;
wei[0] = t % 10;
wei[1] = t / 10 % 10;
wei[2] = t / 100 % 10;
wei[3] = t / 1000 % 10;
wei[4] = t / 10000 % 10;
wei[5] = t / 100000 % 10;
if (0 <= t&&t < 10)
result = wei[0];
if (10 <= t&&t < 100)
result = wei[0] * 10 + wei[1];
if (100 <= t&&t < 1000)
result = wei[0] * 100 + wei[1] * 10 + wei[2];
if (1000 <= t&&t < 10000)
result = wei[0] * 1000 + wei[1] * 100 + wei[2] * 10 + wei[3];
if (10000 <= t&&t < 100000)
result = wei[0] * 10000 + wei[1] * 1000 + wei[2] * 100 + wei[3] * 10 + wei[4];
if (100000 <= t&&t < 1000000)
result = wei[0] * 100000 + wei[1] * 10000 + wei[2] * 1000 + wei[3] * 100 + wei[4] * 10 + wei[5];
printf("%d", result);
return 0;
}
1091 N-自守數 (15 分)
#include<stdio.h>
#include<string.h>
int main()
{
int M = 0, K = 0, N = 0, t = 0;
scanf("%d", &M);
for (int i = 0; i < M; i++)
{
scanf("%d", &K);
for (int j = 1; j < 10; j++)
{
t = K * K * j;
if (0 < K&&K < 10 && t % 10 == K)//個位數
{
printf("%d %d\n", j, t);
break;
}
else if (10 <= K&&K < 100 && t % 100 == K)//個位數
{
printf("%d %d\n", j, t);
break;
}
else if (100 <= K && K < 1000 && t % 1000 == K)//個位數
{
printf("%d %d\n", j, t);
break;
}
if (j == 9)
{
printf("No\n");
}
}
}
return 0;
}