算法筆記第三章練習題_A+B for polynomials,product of polynomials,考試座位號

 

#  include <cstdio>
const int max_n = 1111;
double p [max_n] = {0};
int main()
{

    int k,n,count = 0;
    double a;
    scanf("%d",&k);
    for (int i = 0;i < k;i++)
    {
        scanf("%d %lf",&n,&a);
        p[n] +=a;

    }
    scanf("%d",&k);
    for (int i = 0;i < k;i++)
    {
        scanf("%d %lf",&n,&a);
        p[n] += a;
    }
    for (int i = 0;i<max_n;i++)
    {
        if(p[i] != 0);
        {
            count ++;
        }
    }
    printf("%d",&count);
    for (int i = max_n-1;i>= 0;i--)
    {
       if (p[i]!= 0)
       {
           printf("%d %.f",i,p[i]);
       }
    }
    return 0;
}

# include <cstdio>
struct Ploy
{
    int exp;           //指數
    double  cof;       // 係數

} poly[1001];           //第一個多項式
double ans [2001] ;     //存放結果
int main ()
{

    int n,m,number = 0;
    scanf ("%d",&n);         //第一個多項式中非零係數的項數
    for (int i = 0;i <n;i++)
    {
        scanf("%d %lf",&poly[i].exp,&poly[i].cof) ;   //第一個多項式的指數和係數

    }
    scanf ("%d",&m);// 第二個多項式中非零係數的項數
    for (int i = 0;i<m;i++)
    {
        int exp;
        double cof;
        scanf("%d[ %lf",&exp,&cof);         //第二個多項式的指數和係數
        for (int j = 0;j < n;j++)     //與第一個多項式中的每一項相乘
        {

            ans[exp+poly[j].exp] += (cof*poly[j].cof);
        }
    }
    for (int i = 0;i <= 2000;i++)
    {
        if (ans[i] != 0.0)
        {
            number++ ;                 //累計非零係數的項數

        }
        printf("%d",number);
        for (int i =2000;i >= 0;i--)   //輸出
        {
            if (ans[i] != 0.0)
                {
                    printf("%d %.lf",i ,ans[i]);
                }
        }
    }
    return 0;
}

考試座位號題目描述:
每個PAT考生在參加考試時都會被分配兩個座位號:一個是試機座位:另一個是考試座 位,正常情況下,考生在入場時先得到試機座位號,入座進入試機狀態後,系統會顯示該考 生的考試座位號,考試時考生需要換到考試座位就座.但有些考生遲到了,試機已經結束, 他們只能拿卷領到的試機座位號求助於你,從後臺查出他們的考試座位號碼。
輸入格式
第一行給出一個正整數N(N<=1000);隨後N行,每行給出一個考生的信息:“准考證號 試機座位號 考試座位號”其中准考證號由14位數字組成,座位從1~N編號。輸入保證每個人的准考證號都不同,並且任何時候都不會把兩個人分配到同一個座位上。
在考生信息之後,給出一個正整數M (<=N),隨後一行中給出M個待査詢的試機座位號,以空格分隔
輸出格式
對應每個需要査詢的試機座位號,在一行中輸出對應考生的准考證兮和考試座位號,中間用1個空格分開

輸入:

輸出:

#  include <cstdio>
const int maxn = 1001;
struct Student
{
    long long id;          //准考證號
    int examSeat;          //考試座位號

} testSeat[maxn];           //以試機座位號作爲下標來記錄考生
int main()
{
    int n,m,seat,examSeat;
    long long id;
    scanf("%d",&n);           //考生人數
    for(int i = 0;i <n;i++)
{
    scanf("%lld %d %d",&id ,&seat,&examSeat);  //准考證號,試機座位號,考試座位號

    testSeat[seat].id = id ;           //試機座位號爲Seat考生的准考證號
    testSeat[seat].examSeat = examSeat ;  //試機座位號爲seat的考生的考試號

}
scanf("%d",&m);            //查詢個數
for(int i = 0;i < m;i++)
{
    scanf("%d",&seat);        //與查詢的試機座位號,以此爲下標直接查找考生
    printf("%lld %d\n",testSeat[seat].id,testSeat[seat].examSeat);
}
return 0;
}

 

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