『ACM C++』 PTA 天梯賽練習集L1 | 007-011


  真的是忙頭暈了,學業、ACM打題、班級活動、自學新東西,哇這充實的大學~

 

------------------------------------------------L1-007----------------------------------------------------------

念數字

輸入一個整數,輸出每個數字對應的拼音。當整數為負數時,先輸出fu字。十個數字對應的拼音如下:

0: ling
1: yi
2: er
3: san
4: si
5: wu
6: liu
7: qi
8: ba
9: jiu

輸入格式:

輸入在一行中給出一個整數,如:1234

提示:整數包括負數、零和正數。

輸出格式:

在一行中輸出這個整數對應的拼音,每個數字的拼音之間用空格分開,行末沒有最后的空格。如 yi er san si

輸入樣例:

-600

輸出樣例:

fu liu ling ling

------------------------------------------------L1-007----------------------------------------------------------

注:水題,熟練使用字符指針數組就沒有問題了。如這里的:const char* proun[10] = { "ling", "yi", "er", "san","si","wu","liu","qi","ba","jiu" };

#include<stdio.h>
#include<string.h>
#include<string>
using namespace std;
const char* proun[10] = { "ling", "yi", "er", "san","si","wu","liu","qi","ba","jiu" };
char Input[999999];
int length;
int main()
{
    scanf("%s",&Input);
    length = strlen(Input);
    for(int i = 0;i<length;i++)
    {
        if(Input[i] == '-') printf("fu");
        else printf(proun[Input[i] - '0']);
        if(i!=length-1) printf(" ");
        else printf("\n");
    }
    return 0;
}

 

 

------------------------------------------------L1-008----------------------------------------------------------

求整數段和

給定兩個整數A和B,輸出從A到B的所有整數以及這些數的和。

輸入格式:

輸入在一行中給出2個整數A和B,其中100AB100,其間以空格分隔。

輸出格式:

首先順序輸出從A到B的所有整數,每5個數字占一行,每個數字占5個字符寬度,向右對齊。最后在一行中按Sum = X的格式輸出全部數字的和X

輸入樣例:

-3 8

輸出樣例:

   -3   -2   -1    0    1
    2    3    4    5    6
    7    8
Sum = 30

------------------------------------------------L1-008----------------------------------------------------------

注解:水題,一個循環帶過

#include<stdio.h>

int A,B,counter,sum;
int main()
{
    scanf("%d%d",&A,&B);
    counter = sum = 0;
    for(int i = A;i<=B;i++)
    {
        sum+=i;
        if(counter == 5)
        {
            counter = 0;
            printf("\n");
        }
        printf("%5d",i);
        counter++;
    }
    printf("\nSum = %d\n",sum);
    return 0;
}

 

 

 

------------------------------------------------L1-009----------------------------------------------------------

N個數求和

本題的要求很簡單,就是求N個數字的和。麻煩的是,這些數字是以有理數分子/分母的形式給出的,你輸出的和也必須是有理數的形式。

輸入格式:

輸入第一行給出一個正整數N≤100)。隨后一行按格式a1/b1 a2/b2 ...給出N個有理數。題目保證所有分子和分母都在長整型范圍內。另外,負數的符號一定出現在分子前面。

輸出格式:

輸出上述數字和的最簡形式 —— 即將結果寫成整數部分 分數部分,其中分數部分寫成分子/分母,要求分子小於分母,且它們沒有公因子。如果結果的整數部分為0,則只輸出分數部分。

輸入樣例1:

5
2/5 4/15 1/30 -2/60 8/3

輸出樣例1:

3 1/3

輸入樣例2:

2
4/3 2/3

輸出樣例2:

 2

輸入樣例3:

3
1/3 -1/6 1/8

輸出樣例3:

7/24

------------------------------------------------L1-009----------------------------------------------------------

注解:這題我居然WA了好一會,出現了兩個問題,一個是疊乘超出long long int的問題,一個就是出現了/0的情況。下面進行分析:

· 代碼分塊:

  第一步:先套用獲取最小公倍數的模板:

long long gcd(long long m, long long n)
{
    return (m==0)?n:gcd(n%m, m);
}
long long lcm(long long a, long long b)
{
   return a / gcd(a, b) * b;
}

  第二步:得到化簡功能函數!!

void hj(long long int &a,long long int &b)
{ 
    long long int m,n,p;
    m=a;n=b;p=m%n;
    while(p!=0)
    {
        m=n;n=p;
        p=m%n;
    }
    if(n!=0) //化為最簡分式
    {
        a/=n;
        b/=n;
    }
    if(b<0) //處理分母為復數
    {
        a=-a;
        b=-b;
    }
}

  第三步:輸入的同時進行通分、加和,最重要的是一定要記得時刻化簡!!原本有一個test3測試點瘋狂的出錯,我看到了不超過長整形,所以用了long long int,但是突然想到,如何是加和乘積,是極有可能爆long long int的,所以要時刻進行化簡,就過了。

    for(int i = 1;i<times;i++)
    {
        scanf("%lld/%lld",&temp_up,&temp_down);
        long long int temp = lcm(temp_sumdown,temp_down);
        if(temp_sumdown<temp) temp_sumup*=(temp/temp_sumdown);
        if(temp_down<temp) temp_up*=(temp/temp_down);
        temp_sumdown = temp;
        temp_sumup+=temp_up;
        hj(temp_sumup,temp_sumdown);
    }

  第四步:分子包含了符號,也就說,對於分子為正的數,進行整數化處理:

    for(int i = 1;i<times;i++)
    {
        scanf("%lld/%lld",&temp_up,&temp_down);
        long long int temp = lcm(temp_sumdown,temp_down);
        if(temp_sumdown<temp) temp_sumup*=(temp/temp_sumdown);
        if(temp_down<temp) temp_up*=(temp/temp_down);
        temp_sumdown = temp;
        temp_sumup+=temp_up;
        hj(temp_sumup,temp_sumdown);
    }

  第五步:對輸入0進行單獨處理,這里是我之前沒想到的事情,一直test5出錯,加了下面這句話之后就過了。

else if(temp_sumup == 0) printf("0\n");

  第六步:對分子為負數的情況進行符號處理:

    else
    {
        temp_sumup*=-1;
        long long int intt = temp_sumup/temp_sumdown;
        long long int up = temp_sumup - intt*temp_sumdown;
        long long int down = temp_sumdown; 
        hj(up,down);
        if(intt>0)
        {
            printf("-%lld",intt);
            if(up>0) printf(" -%lld/%lld\n",up,down);
            else printf("\n");
        }
        else printf("-%lld/%lld\n",temp_sumup,temp_sumdown);
    }

 

· AC代碼:

#include<stdio.h>
#include<math.h>
using namespace std;
long long int times,temp_up,temp_down;
long long int temp_sumup,temp_sumdown;
void hj(long long int &a,long long int &b)
{ 
    long long int m,n,p;
    m=a;n=b;p=m%n;
    while(p!=0)
    {
        m=n;n=p;
        p=m%n;
    }
    if(n!=0) //化為最簡分式
    {
        a/=n;
        b/=n;
    }
    if(b<0) //處理分母為復數
    {
        a=-a;
        b=-b;
    }
}
long long gcd(long long m, long long n)
{
    return (m==0)?n:gcd(n%m, m);
}
long long lcm(long long a, long long b)
{
   return a / gcd(a, b) * b;
}
int main()
{
    scanf("%lld",&times);
    scanf("%lld/%lld",&temp_sumup,&temp_sumdown);
    for(int i = 1;i<times;i++)
    {
        scanf("%lld/%lld",&temp_up,&temp_down);
        long long int temp = lcm(temp_sumdown,temp_down);
        if(temp_sumdown<temp) temp_sumup*=(temp/temp_sumdown);
        if(temp_down<temp) temp_up*=(temp/temp_down);
        temp_sumdown = temp;
        temp_sumup+=temp_up;
        hj(temp_sumup,temp_sumdown);
    }
    if(temp_sumup >0)
    {
        long long int intt = temp_sumup/temp_sumdown;
        long long int up = temp_sumup - intt*temp_sumdown;
        long long int down = temp_sumdown; 
        hj(up,down);
        if(intt>0)
        {
            printf("%lld",intt);
            if(up>0) printf(" %lld/%lld\n",up,down);
            else printf("\n");
        }
        else printf("%lld/%lld\n",temp_sumup,temp_sumdown);
    }
    else if(temp_sumup == 0) printf("0\n");
    else
    {
        temp_sumup*=-1;
        long long int intt = temp_sumup/temp_sumdown;
        long long int up = temp_sumup - intt*temp_sumdown;
        long long int down = temp_sumdown; 
        hj(up,down);
        if(intt>0)
        {
            printf("-%lld",intt);
            if(up>0) printf(" -%lld/%lld\n",up,down);
            else printf("\n");
        }
        else printf("-%lld/%lld\n",temp_sumup,temp_sumdown);
    }

    return 0;
}

 

 

·AC截圖:

 

· 解后反思:

  這里提到了三個模板需要熟記於心,一個是化簡模板、一個是gcd、還有一個lcm模板,可能之后要經常用到~然后就是要時刻注意加減乘除四種運算是否會爆long long int的情況,以及對特殊情況的處理

 

------------------------------------------------L1-010----------------------------------------------------------

比較大小

本題要求將輸入的任意3個整數從小到大輸出。

輸入格式:

輸入在一行中給出3個整數,其間以空格分隔。

輸出格式:

在一行中將3個整數從小到大輸出,其間以“->”相連。

輸入樣例:

4 2 8

輸出樣例:

2->4->8

------------------------------------------------L1-010----------------------------------------------------------

注解:水題,一個sort即可解。

#include<stdio.h>
#include<algorithm>
using namespace std;
int temp[3];
int main()
{
    scanf("%d %d %d",&temp[0],&temp[1],&temp[2]);
    sort(temp,temp+3);
    printf("%d->%d->%d\n",temp[0],temp[1],temp[2]);
    return 0;
}

 

 

 

------------------------------------------------L1-011----------------------------------------------------------

A-B 

本題要求你計算AB。不過麻煩的是,A和B都是字符串 —— 即從字符串A中把字符串B所包含的字符全刪掉,剩下的字符組成的就是字符串AB。

輸入格式:

輸入在2行中先后給出字符串A和B。兩字符串的長度都不超過104​​,並且保證每個字符串都是由可見的ASCII碼和空白字符組成,最后以換行符結束。

輸出格式:

在一行中打印出AB的結果字符串。

輸入樣例:

I love GPLT!  It's a fun game!
aeiou

輸出樣例:

I lv GPLT!  It's  fn gm!

------------------------------------------------L1-011----------------------------------------------------------

注解:這道題我WA了好一會,總感覺思路很正確沒毛病,結果出錯在輸入的函數選擇上:

#include<iostream>
#include<cstring>
using namespace std;
string a,b;
bool vis[10005];
int main()
{
    getline(cin,a);
    getline(cin,b);
    for(int i=0;i<b.length();i++) vis[b[i]+5000]=1;
    for(int i=0;i<a.length();i++) if(vis[a[i]+5000] == 0) cout<<a[i];
    cout<<endl;
    return 0;
}

疑問:使用cin.getline對char數組輸入是會WA的,至今不知道為什么,而使用getline(cin,string)對string類輸入卻能直接AC,希望有大神能夠留言幫我解決這個問題~ 萬分感謝。同時耶告訴我以后再這方面需要注意,對於空格和換行符的輸入的時候,多使用幾種輸入方法,可能就能通過AC了~

 

 

注:如果有更好的解法,真心希望您能夠評論留言貼上您的代碼呢~互相幫助互相鼓勵才能成長鴨~~


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM