PAT倒計時——3天!(字符串和數值篇)

還有三天就要考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;
}

 

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章