PAT基礎編程題目集--函數集4-(1~5)__PAT

來源:互聯網
上載者:User
4-1 簡單輸出整數   (10分)

本題要求實現一個函數,對給定的正整數N,列印從1到N的全部正整數。 函數介面定義:

void PrintN ( int N );

其中N是使用者傳入的參數。該函數必須將從1到N的全部正整數順序列印出來,每個數字佔1行。 裁判測試程式範例:

#include <stdio.h>void PrintN ( int N );int main (){    int N;    scanf("%d", &N);    PrintN( N );    return 0;}/* 你的代碼將被嵌在這裡 */
輸入範例:
3
輸出範例:
123

解答程式:

void PrintN ( int N )
{  
     int i;
     for(i=1;i<=N;i++)
     printf("%d\n",i);
}


——————————————————————————————————————————————————————————————————

4-2 多項式求值   (15分)

本題要求實現一個函數,計算階數為n,係數為a[0] ... a[n]的多項式f(x)=∑i=0n(a[i]×xi)f(x)=\sum_{i=0}^{n}(a[i]\times x^i)f(x)=∑i=0n(a[i]×xi) 在x點的值。 函數介面定義:

double f( int n, double a[], double x );

其中n是多項式的階數,a[]中儲存係數,x是給定點。函數須返回多項式f(x)的值。 裁判測試程式範例:

#include <stdio.h>#define MAXN 10double f( int n, double a[], double x );int main(){    int n, i;    double a[MAXN], x;    scanf("%d %lf", &n, &x);    for ( i=0; i<=n; i++ )        scanf(“%lf”, &a[i]);    printf("%.1f\n", f(n, a, x));    return 0;}/* 你的代碼將被嵌在這裡 */
輸入範例:
2 1.11 2.5 -38.7
輸出範例:
-43.1

解答程式:

double f( int n, double a[], double x )
{
    int i;
    double  mutlipy=1.0,sum=0;
    for(i=0;i<=n;i++)  {
         sum+=a[i]*mutlipy;
         mutlipy*=x;
     }
   return  sum;
}


——————————————————————————————————————————————————————————————————



4-3 簡單求和   (10分)

本題要求實現一個函數,求給定的N個整數的和。 函數介面定義:

int Sum ( int List[], int N );

其中給定整數存放在數組List[]中,正整數N是數組元素個數。該函數須返回N個List[]元素的和。 裁判測試程式範例:

#include <stdio.h>#define MAXN 10int Sum ( int List[], int N );int main (){    int List[MAXN], N, i;    scanf("%d", &N);    for ( i=0; i<N; i++ )        scanf("%d", &List[i]);    printf("%d\n", Sum(List, N));    return 0;}/* 你的代碼將被嵌在這裡 */
輸入範例:
312 34 -5
輸出範例:
41


解答程式:

int Sum ( int List[], int N )
{
     int i,sunshine=0;
     for(i=0;i<N;i++)
     {
          sunshine+=List[i];
      }
      
      return sunshine;
}


——————————————————————————————————————————————————————————————————


4-4 求自定類型元素的平均   (10分)

本題要求實現一個函數,求N個集合元素S[]的平均值,其中集合元素的類型為自訂的ElementType。 函數介面定義:

ElementType Average( ElementType S[], int N );

其中給定集合元素存放在數組S[]中,正整數N是數組元素個數。該函數須返回N個S[]元素的平均值,其值也必須是ElementType類型。 裁判測試程式範例:

#include <stdio.h>#define MAXN 10typedef float ElementType;ElementType Average( ElementType S[], int N );int main (){    ElementType S[MAXN];    int N, i;    scanf("%d", &N);    for ( i=0; i<N; i++ )        scanf("%f", &S[i]);    printf("%.2f\n", Average(S, N));    return 0;}/* 你的代碼將被嵌在這裡 */
輸入範例:
312.3 34 -5
輸出範例:
13.77


解答程式:

ElementType Average( ElementType S[], int N )
{
    ElementType    average=0;
    int i;
    for(i=0;i<N;i++)    {
        average+=S[i];
    }
    average/=N;
    return average;
 }

——————————————————————————————————————————————————————————————————


4-5 求自定類型元素的最大值   (10分)

本題要求實現一個函數,求N個集合元素S[]中的最大值,其中集合元素的類型為自訂的ElementType。 函數介面定義:

ElementType Max( ElementType S[], int N );

其中給定集合元素存放在數組S[]中,正整數N是數組元素個數。該函數須返回N個S[]元素中的最大值,其值也必須是ElementType類型。 裁判測試程式範例:

#include <stdio.h>#define MAXN 10typedef float ElementType;ElementType Max( ElementType S[], int N );int main (){    ElementType S[MAXN];    int N, i;    scanf("%d", &N);    for ( i=0; i<N; i++ )        scanf("%f", &S[i]);    printf("%.2f\n", Max(S, N));    return 0;}/* 你的代碼將被嵌在這裡 */
輸入範例:
312.3 34 -5
輸出範例:
34.00


解答程式:

ElementType Max( ElementType S[], int N )
{
    ElementType    max=S[0];      //注意,max不能習慣隨意賦值為0,如果輸入的數都為負數,則會出現錯誤的傳回值;
    int i;
    if(N>MAXN)    {
        N=MAXN;
    }
    for(i=0;i<N;i++)    {
        if(S[i]>max)    {
            max=S[i];
        }    
    }
    return max;
 }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.