实验 18 问题回顾一 参考答案

OJ题目:问题回顾一

题目描述

有4个互不相同的数字,输出由其中三个不重复数字组成的排列。

输入要求

4个整数。

输出要求

所有排列。

假如输入

1 2 3 4

应当输出

1 2 3

1 3 2

2 1 3

2 3 1

3 1 2

3 2 1

1 2 4

1 4 2

2 1 4

2 4 1

4 1 2

4 2 1

1 3 4

1 4 3

3 1 4

3 4 1

4 1 3

4 3 1

2 3 4

2 4 3

3 2 4

3 4 2

4 2 3

#include<stdio.h>

main()

{

   int i,j,k,t,n,a[5];

   scanf("%d%d%d%d",&a[1],&a[2],&a[3],&a[4]);

   for(i=4;i>0;i--)

      for(j=1;j<=4;j++)

         if(i!=j)for(k=1;k<=4;k++)

            if(k!=i&&k!=j)for(t=1;t<=4;t++)

                if(t!=k&&t!=i&&t!=j)

                   printf("%d %d %d
",a[j],a[k],a[t]);

}

题目描述

输入N*N的矩阵,输出它的转置矩阵。

输入要求

第一行为整数N(1<=N<=10)。接着是一个N*N的矩阵。

输出要求

转置矩阵。

假如输入

2

1 2

1 2

应当输出

1 1

2 2

提示

 1 #include <string.h>
 2 #include <stdio.h>
 3 main()
 4 {
 5     int i,j,n,a[100][100];
 6     scanf("%d",&n);
 7     for(i=0;i<n;i++)
 8         for(j=0;j<n;j++)
 9             scanf("%d",&a[i][j]);
10     for(i=0;i<n;i++)
11         for(j=0;j<n;j++)
12         {
13             printf("%d",a[j][i]);
14             // 格式输出
15             if(j==n-1)         // 如果j==n-1代表是数组最后一个元素
16                 printf("
"); // 如果是最后一个元素换行
17             else
18                 printf(" ");  // 如果不是最后一个元素, 输出空格
19 
20         }
21 }

题目描述

编制程序,输入m,n(M>=n>=0)后,计算下列表达式的值并输出:

         m!         

n! (m-n)!

要求将计算阶乘运算的函数写为fact(n),函数返回值的类型为float

输入要求

m n

输出要求

对应表达式的值

假如输入

2 1

应当输出

2

提示

#include <stdio.h>

float fact(int n)

{

   if(n<=1)

      return 1.0;

   return n*fact(n-1);

}

int main()

{

   int m,n;

   scanf("%d%d",&m,&n);

   printf("%.0lf ",fact(m)/fact(n)/fact(m-n));

   return 0;

}

题目描述

编制程序,统计文本stdin中字符$出现的次数,并将结果写入文件stdout

输入要求

字符文本

输出要求

$次数

假如输入

as$dfkjhkjkjdhf

asdfkj$lskdfj

werijweirjo$wie

应当输出

3

#include <stdio.h>

int main()

{

   char tmp[1000];

   int sum = 0, i;

   while(gets_s(tmp)) // Ctrl+Z

   {

      i = 0;

      while(tmp[i])

      {

         sum += tmp[i++]=='$';

      }

   }

   printf("%d ",sum);

   return 0;

}

题目描述

编制程序,输入n个整数(n从键盘输入,n>0),输出它们的偶数和。

输入要求

n

n个整数

输出要求

其中偶数的和

假如输入

10

1 2 3 4 5 6 7 8 9 10

应当输出

30

#include<stdio.h>

int main()

{

   int n,x;

   int sum = 0;

   scanf("%d",&n);

   while(n--)

   {

      scanf("%d",&x);

      if(!(x&1))

         sum += x;

   }

   printf("%d ",sum);

   return 0;

}

题目描述

编制函数del_char

函数原型为 void del_char(char *,char),函数的功能是删除a指向的字符串中值为ch的字符,例如从字符串"AscADef"中删除'A'后,字符串为"scDef"。

输入要求

需要删除的字符ch

需要处理的字符串

输出要求

处理后的字符串

假如输入

A

AscADef

应当输出

scDef

#include <stdio.h>

void del_char(char *str,char ch)

{

   int i=0, j=0;

   while(str[i])

   {

      if(str[i]!=ch)

         str[j++] = str[i];

      i++;

   }

   str[j] = 0;

}

 

int main()

{

   char ch, str[1000];

   scanf("%c ",&ch);

   gets(str);

   del_char(str,ch);

   puts(str);

 

   return 0;

}

题目描述

编程,输入n后:输入n个数,根据下式计算并输出y值。

     / x2-sinx    x<-2

y={  2x+x       -2<=x<=2

     |    ___________

      √  X2+X+1                  x>2

* 输出保留两位小数  

输入要求

n

n个数

输出要求

y

假如输入

1

1

应当输出

3.00

#include<stdio.h>

#include <math.h>

 

int main()

{

   double x,y;

   int n;

   scanf("%d",&n);

   while(n--)

   {

      scanf("%lf",&x);

      if(x < -2) {

         y = pow(x,2.0)-sin(x);

      }

      else if(x <= 2) {

         y = pow(2.0,x)+x;

      }

      else {

         y = sqrt(pow(x,2.0)+x+1);

      }

      printf("%.2lf ",y);

   }

   return 0;

}

题目描述

编制函数,其功能是在float类型一维数组中查找最大值、最小值,并将它们返回到调用程序。

* 输出保留两位小数

输入要求

n

n个浮点数

输出要求

最大值 最小值

假如输入

10

1.0

2.0

3.0

4.0

5.0

6.0

7.0

8.0

9.0

10.0

应当输出

10.00 1.00

#include <stdio.h>

float Min(float * a,int n)

{

   int i;

   float fMin = a[0];

   for(i=1;i<n;i++)

   {

      if(fMin > a[i])

         fMin = a[i];

   }

 

   return fMin;

}

 

float Max(float * a,int n)

{

   int i;

   float fMax = a[0];

   for(i=1;i<n;i++)

   {

      if(fMax < a[i])

         fMax = a[i];

   }

 

   return fMax;

}

 

int main()

{

   int i,n;

   float a[1000];

   scanf("%d",&n);

   for(i=0;i<n;i++)

   {

      scanf("%f",&a[i]);

   }

   printf("%.2f %.2f ",Max(a,n),Min(a,n));

 

   return 0;

}

题目描述

输入一行字符,分别统计出其中英文字母、数字、空格和其他字符的个数。

输入要求

一行字符

输出要求

统计值

假如输入

aklsjflj123 sadf918u324 asdf91u32oasdf/.';123

应当输出

23 16 2 4

#include <stdio.h>

int main()

{

    int nEnglish=0,nSpace=0,nNumber=0,nOther=0;

    int i=0;

    char chString[1000];

    gets(chString);

    while('' != chString[i])

    {

        if(chString[i]>='A'&&chString[i]<='Z' || chString[i]>='a'&&chString[i]<='z')

        {

            nEnglish ++;

        }

        else if(chString[i] == ' ')

        {

            nSpace ++;

        }

        else if(chString[i]>='0' && chString[i]<='9')

        {

            nNumber ++;

        }

        else

        {

            nOther ++;

        }     

        i++;

    }

    printf("%d %d %d %d ",nEnglish,nNumber,nSpace,nOther);

 

    return 0;

}

题目描述

输入一个正整数n.求1+1/2!+1/3!+....+1/n!

要求定义并调用函数fact(n)计算n的阶乘,函数返回值的类型是点单精度浮点型。

* 输出保留4位小数

输入要求

正整数n

输出要求

数列之和

假如输入

2

应当输出

1.5000

#include <stdio.h>

 

float fact(int n)

{

   static float res = 1.0;

   res *= n;

 

   return res;

}

 

int main()

{

   int i,n;

   float res = 0;

   scanf("%d",&n);

   for(i=1;i<=n;i++)

   {

      res += 1.0/fact(i);

   }

   printf("%.4f ",res);

 

   return 0;

}

原文地址:https://www.cnblogs.com/jlxuqiang/p/3475799.html