还有三天就要考PAT了,这几天写博客回顾一下常用的算法,临时抱一下佛脚哈哈哈~
第一次写,如有错误,请多指教哈~
字符串和数值篇
一般来说,pat的第一道或者第二道是字符串和数值处理,题目本身不难,但是经常会有各种坑orz,这里列出一些目前遇到的坑。
数值:
1、数值过大相乘或相加容易溢出
2、0和-0的输入和输出
3、-00001(做过的人应该懂吧~)
4、做除法判零
5、分数的四则运算分子取绝对值,符号单独判断
总感觉还有很多。。。想不起来了,先这样吧。
一、最小公约数和最大公倍数
辗转相除法,babab~
int gcd(int a,int b){
if(b==0)
return a;
return gcd(b,a%b);
}
int lcm(int a,int b){
return a/gcd(a,b)*b;
}
二、判断素数
bool isprime(int data){
if(data<=1)
return false;
for(int i=2;i<=sqrt(data);i++){
if(data%i==0)
return false;
}
return true;
}
三、字符串的插入,删除,截取,替换
void insertstr(){
string str="123456";
str.insert(3,"abc");
}
void erasestr(){
string str="123456";
str.erase(3,2);
}
string getsubstr(string input ,int index,int len){
return input.substr(index,len);
}
void replacestr(string input,int index,int len,string str){
input.replace(index,len,str);
}
四、各种输入输出
int main(){
string str;
getline(cin,str);//读一行字符串,遇回车停下
cout<<str<<endl;
char s[20];
char input[20];
cin.getline(input,20);//也是读一行,不过是只能读20个字符
sscanf(input,"%[^ ]",s);//读取str到s中,遇到空格停下
printf("%s\n",s);
sprintf(s,"%d",100);//将str中的整数取到s中
printf("%s\n",s);
////////////////////////////////////////////////////////////////
printf("%04d\n",1);//输出四位数,前面不足添零
printf("%.2f\n",1.2345);//输出精度为2的浮点数
int a=ceil(99.5);//向上取整
int b=floor(99.5);//向下取整
int c=round(99.5);//四舍五入
int d=round(99.49);
printf("%d %d %d %d\n",a,b,c,d);
int e=1234567%1000/100;//百
int f=1234567%100/10;//十
int g=1234567%10;//个
cout<<e<<" "<<f<<" "<<g<<endl;
}
测试数据:
pat 100
cx pat 100
结果:
pat 100
cx
100
0001
1.23
100 99 100 99
5 6 7
这一块内容真的没啥好讲的,其坑要到题目中去自己体会,具体问题具体分析,下面给出两道还算简单的字符串和数值分析题。
1081 Rational Sum (20 分)
Given N rational numbers in the form numerator/denominator
, you are supposed to calculate their sum.
Input Specification:
Each input file contains one test case. Each case starts with a positive integer N (≤100), followed in the next line N rational numbers a1/b1 a2/b2 ...
where all the numerators and denominators are in the range of long int. If there is a negative number, then the sign must appear in front of the numerator.
Output Specification:
For each test case, output the sum in the simplest form integer numerator/denominator
where integer
is the integer part of the sum, numerator
< denominator
, and the numerator and the denominator have no common factor. You must output only the fractional part if the integer part is 0.
Sample Input 1:
5
2/5 4/15 1/30 -2/60 8/3
Sample Output 1:
3 1/3
Sample Input 2:
2
4/3 2/3
Sample Output 2:
2
Sample Input 3:
3
1/3 -1/6 1/8
Sample Output 3:
7/24
#include<stdio.h>
#include<vector>
#include<algorithm>
#include<map>
#include<string>
#include<iostream>
#include<queue>
using namespace std;
int num;
long gcd(long a,long b){
if(b==0)
return a;
return gcd(b,a%b);
}
void getsimple(long &fenzi,long &fenmu){
long gong=gcd(abs(fenzi),abs(fenmu));
fenzi/=gong;
fenmu/=gong;
}
int main(){
cin>>num;
long fenzi,fenmu;
scanf("%ld/%ld",&fenzi,&fenmu);
getsimple(fenzi,fenmu);
for(int i=1;i<num;i++){
long tz,tm;
scanf("%ld/%ld",&tz,&tm);
getsimple(tz,tm);
fenzi=fenzi*tm+tz*fenmu;
fenmu*=tm;
getsimple(fenzi,fenmu);
}
if(fenzi==0){
cout<<0;
return 0;
}
if(abs(fenzi)>abs(fenmu)){
if(abs(fenzi)%fenmu!=0)
printf("%d %d/%d",fenzi/fenmu,abs(fenzi)%fenmu,fenmu);
else
cout<<fenzi/fenmu;
}else{
printf("%d/%d",fenzi,fenmu);
}
return 0;
}
1082 Read Number in Chinese (25 分)
Given an integer with no more than 9 digits, you are supposed to read it in the traditional Chinese way. Output Fu
first if it is negative. For example, -123456789 is read as Fu yi Yi er Qian san Bai si Shi wu Wan liu Qian qi Bai ba Shi jiu
. Note: zero (ling
) must be handled correctly according to the Chinese tradition. For example, 100800 is yi Shi Wan ling ba Bai
.
Input Specification:
Each input file contains one test case, which gives an integer with no more than 9 digits.
Output Specification:
For each test case, print in a line the Chinese way of reading the number. The characters are separated by a space and there must be no extra space at the end of the line.
Sample Input 1:
-123456789
Sample Output 1:
Fu yi Yi er Qian san Bai si Shi wu Wan liu Qian qi Bai ba Shi jiu
Sample Input 2:
100800
Sample Output 2:
yi Shi Wan ling ba Bai
#include<stdio.h>
#include<vector>
#include<algorithm>
#include<map>
#include<string>
#include<iostream>
#include<queue>
using namespace std;
map<int,string> getwei;
map<int,string> getshu;
string getstr(string input){
// cout<<input<<"input"<<endl;
string str="";
bool isling=false;
for(int i=0;i<input.size();i++){
int num=input[i]-'0';
if(num==0){
isling=true;
continue;
}
if(isling==true){
str+="ling ";
}
isling=false;
str+=getshu[num]+getwei[input.size()-i];
//cout<<"str:"<<str<<"i:"<<i<<"size:"<<input.size()<<endl;
if(i<input.size()-1)
str+=" ";
}
return str;
}
void init(){
getwei[1]="";
getwei[2]=" Shi";
getwei[3]=" Bai";
getwei[4]=" Qian";
getshu[0]="ling";
getshu[1]="yi";
getshu[2]="er";
getshu[3]="san";
getshu[4]="si";
getshu[5]="wu";
getshu[6]="liu";
getshu[7]="qi";
getshu[8]="ba";
getshu[9]="jiu";
}
int main(){
init();
string input;
cin>>input;
int cnt=input.size();
if((cnt==1&&input[0]=='0')||(cnt==2&&input[0]=='-'&&input[1]=='0')){
cout<<"ling";
return 0;
}
if(input[0]=='-'){
cout<<"Fu ";
input.erase(0,1);
cnt--;
}
if(cnt==9){
int num=input[0]-'0';
cout<<getshu[num];
cout<<" Yi ";
input.erase(0,1);
cnt--;
}
// cout<<"input:"<<input<<endl;
string result="";
if(cnt>4){
result+=getstr(input.substr(0,cnt-4));
for(int i=result.size()-1;i>=0;i--){
if(result[i]==' ')
result.erase(i,1);
else
break;
}
result+=" Wan "+getstr(input.substr(cnt-4,4));
}else{
result+=getstr(input.substr(0,cnt));
}
for(int i=result.size()-1;i>=0;i--){
if(result[i]==' ')
result.erase(i,1);
else
break;
}
cout<<result;
}