PAT
文章目錄
- **PAT**
-
- 1001 A+B Format
-
- 正确代碼:
-
-
- 來自大佬的啟示
- 知識補充 · 格式化輸出
- **補充資料**
-
- 1002 A+B for Polynomials
-
- 正确代碼:
- 1005 Spell It Right
-
- 正确代碼:
- 1023 Have Fun with Numbers
-
- 正确代碼:
1001 A+B Format
Calculate a+b and output the sum in standard format – that is, the digits must be separated into groups of three by commas (unless there are less than four digits).
Input Specification:
Each input file contains one test case. Each case contains a pair of integers a and b where −1000000 ≤ a , b ≤ 1000000 . The numbers are separated by a space.
Output Specification:
For each test case, you should output the sum of a and b in one line. The sum must be written in the standard format.
Sample Input:
-1000000 9
Sample Output:
-999,991
錯誤原因:沒有考慮到值的補充
>輸入 1000 8
輸出 1,8
正确代碼:
#include <iostream>
using namespace std;
int main()
{
long a,b,d;
cin >>a>>b;
long c = a+b;
if(c<0){
cout<<"-";
c=-c;
}
if(c>=1000000){
d = c/1000000;
cout<< d<< ",";
c = c-d*1000000;d = c/1000;
cout.fill('0');cout.width(3);
cout<< d<< ",";
c = c-d*1000;
cout.fill('0');cout.width(3);
cout<<c;
}else if (c>=1000){
d = c/1000;
cout<< d<< ",";
c = c-d*1000;
cout.fill('0');cout.width(3);
cout<<c;
}else {
cout<<c;
}
return 0;
}
來自大佬的啟示
//連結:https://www.nowcoder.com/questionTerminal/30ebf35cea7f4e8398dd0f12c069dc76
//來源:牛客網
//堆棧
void slove(int x){
if(x==0) {cout<<"0"<<endl;return ;}
if(x<0){cout<<"-"; x=-x;}
stack<int>s;
while(x){
int t=x%10;
s.push(t);
x/=10;
}
while(!s.empty()){
cout<<s.top();
s.pop();
int len=s.size();
if(len%3==0&&len) cout<<",";
}
cout<<endl;
}
知識補充 · 格式化輸出
C語言
%d:按整型資料的實際長度輸出。
%md:m為指定的輸出字段的寬度。如果資料的位數小于m,則左端補以空格,若大于m,則按實際位數輸出。
%ld:輸出長整型資料。
C++
cout.fill(‘0’); //設定填充字元
cout.width(3); //設定域寬
cout.precision(1);//設定小數點保留位數
cout.setf(ios::fixed);
補充資料
C++中輸出入門級格式:前補0以及精确度
c語言printf()輸出格式大全
1002 A+B for Polynomials
This time, you are supposed to find A+B where A and B are two polynomials.
Input Specification:
Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial:
K N1 aN1 N2 aN2 … NK aNK, where K is the number of nonzero terms in the polynomial, Ni and aNi (i=1, 2, …, K) are the exponents and coefficients, respectively. It is given that 1 <= K <= 10,0 <= NK < … < N2 < N1 <=1000.
Output Specification:
For each test case you should output the sum of A and B in one line, with the same format as the input. Notice that there must be NO extra space at the end of each line. Please be accurate to 1 decimal place.
Sample Input:
2 1 2.4 0 3.2
2 2 1.5 1 0.5
Sample Output:
3 2 1.5 1 2.9 0 3.2
錯誤原因:數組設定過小
正确代碼:
#include <iostream>
using namespace std;
int main()
{
double poo[1020]={0},pot[1030]={0},fin[1000]={0};
double tem_num,x,z=0;
int o,t;
cin>>o;
for(int i=0;i<o;i++){
cin>>x>>tem_num;
int a =x;
poo[a] = tem_num;
}
cin>>t;
for(int i=0;i<t;i++){
cin>>x>>tem_num;
int a =x;
pot[a] = tem_num;
}
for(int i=0;i<=1000;i++){
fin[i] = poo[i]+pot[i];
if(fin[i]!=0){
z++;
}
}
cout<< z;
if(z==0){
return 0;
}else{
for(int i=1000;i>=0;i--){
if(fin[i]!=0){
cout<<" "<<i;
cout.precision(1);
cout.setf(ios::fixed);
cout<< " "<< fin[i];
}
}
}
return 0;
}
1005 Spell It Right
Given a non-negative integer N, your task is to compute the sum of all the digits of N, and output every digit of the sum in English.
Input Specification:
Each input file contains one test case. Each case occupies one line which contains an N (≤10^100 ).
Output Specification:
For each test case, output in one line the digits of the sum in English words. There must be one space between two consecutive words, but no extra space at the end of a line.
Sample Input:
12345
Sample Output:
one five
正确代碼:
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
void name(int );
int main(){
string s;
cin>>s;
int a=0;
int i;
for(i=0;i<s.length();i++)
{
a=a+s[i]-'0';
}
if(a>1000){
name(a/1000); a = a- 1000*(a/1000);
cout<<" ";
name(a/100); a = a- 100*(a/100);
cout<<" ";
name(a/10); a = a- 10*(a/10);
cout<<" ";
name(a);
}else if(1000>a&a>=100){
name(a/100); a = a- 100*(a/100);
cout<<" ";
name(a/10); a = a- 10*(a/10);
cout<<" ";
name(a);
}else if(100>a&a>=10){
name(a/10); a = a- 10*(a/10);
cout<<" ";
name(a);
}else{
name(a);
}
return 0;
}
void name(int x){
switch (x){
case 0:
cout<<"zero"; break;
case 1:
cout<<"one"; break;
case 2:
cout<<"two"; break;
case 3:
cout<<"three"; break;
case 4:
cout<<"four"; break;
case 5:
cout<<"five"; break;
case 6:
cout<<"six"; break;
case 7:
cout<<"seven"; break;
case 8:
cout<<"eight"; break;
case 9:
cout<<"nine";break;
}
}
1023 Have Fun with Numbers
Notice that the number 123456789 is a 9-digit number consisting exactly the numbers from 1 to 9, with no duplication. Double it we will obtain 246913578, which happens to be another 9-digit number consisting exactly the numbers from 1 to 9, only in a different permutation. Check to see the result if we double it again!
Now you are suppose to check if there are more numbers with this property. That is, double a given number with k digits, you are to tell if the resulting number consists of only a permutation of the digits in the original number.
Input Specification:
Each input contains one test case. Each case contains one positive integer with no more than 20 digits.
Output Specification:
For each test case, first print in a line “Yes” if doubling the input number gives a number that consists of only a permutation of the digits in the original number, or “No” if not. Then in the next line, print the doubled number.
Sample Input:
1234567899
Sample Output:
Yes
2469135798
正确代碼:
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
char num[22];
cin>>num;
int ty[10]={0};
int x = strlen(num);
for(int i=0;i<x;i++){
int temp = num[i]-'0';
ty[temp]++;
}
int flag =0;
for(int i=x-1;i>=0;i--){
int temp = num[i]-'0';
temp = temp*2;
temp = temp +flag;
flag = 0;
if(temp>=10){
temp = temp -10;
flag = 1;
}
num[i] = temp;
}
for(int i=0;i<x;i++){
int temp = num[i];
ty[temp]--;
}
if(flag ==1)
ty[1]--;
for(int i=0;i<10;i++){
if(ty[i]!=0){
cout<<"No"<<endl;
if(flag==1)
cout<<"1";
for(int i = 0;i<x;i++){
int temp = num[i];
cout<<temp;
}
return 0;
}
}
cout<<"Yes"<<endl;
if(flag==1)
cout<<"1";
for(int i = 0;i<x;i++){
int temp = num[i];
cout<<temp;
}
return 0;
}