C语言程序设计基础OJ练习题(实验六一维数组)

一、C语言实验——最值

Time Limit: 1000 ms Memory Limit: 65536 KiB

Submit Statistic

Problem Description

有一个长度为n的整数序列,其中最大值和最小值不会出现在序列的第一和最后一个位置。
请写一个程序,把序列中的最小值与第一个数交换,最大值与最后一个数交换。输出转换好的序列。

Input

输入包括两行。
第一行为正整数n(1≤n≤10)。
第二行为n个正整数组成的序列。

Output

输出转换好的序列。数据之间用空格隔开。

Sample Input

6
2 3 8 1 4 5

Sample Output

1 3 5 2 4 8

#include<stdio.h>
int main()
{
    int n,a[20],min,max,x,y;
    scanf("%d",&n);
    scanf("%d",&a[0]);
    min = a[0];
    max = a[0];
    for(int i=1; i<n; i++)
    {
        scanf("%d",&a[i]);
        if(a[i]>max)
        {
            max = a[i];
            x = i;
        }
        else if(a[i]<min)
        {
            min = a[i];
            y = i;
        }
    }
    a[x]=a[n-1];
    a[y]=a[0];
    a[n-1]=max;
    a[0]=min;
    for(int i = 0;i<n-1;i++)
    {
        printf("%d ",a[i]);
    }
    printf("%d\n",a[n-1]);
    return 0;
}

 

二、C语言实验——整数位

Time Limit: 1000 ms Memory Limit: 65536 KiB

Submit Statistic

Problem Description

输入一个不多于5位的正整数,要求:
(1)求出它是几位数;
(2)分别输出每一位数字;
(3)按逆序输出各位数字。

Input

输入一个不多于5位的正整数。

Output

输出数据有3行,第一行为正整数位数,第二行为各位数字,第三行为逆序的各位数字。

Sample Input

123

Sample Output

3
1 2 3
3 2 1

#include<stdio.h>
int main()
{
    int n,a[20],x;
    scanf("%d",&n);
    x = 0;
    while(n/10!=0)
    {
        a[x++]= n%10;
        n = n/10;
    }
    a[x++]=n;
    printf("%d\n",x);
    for(int i = x-1;i>=0;i--)
    {
        if(i==0)
            printf("%d\n",a[i]);
        else
            printf("%d ",a[i]);
    }
    for(int i = 0;i<x;i++)
    {
        if(i==x-1)
            printf("%d\n",a[i]);
        else
            printf("%d ",a[i]);
    }
    return 0;
}

 

三、小鑫数数儿

Time Limit: 1000 ms Memory Limit: 65536 KiB

Submit Statistic

Problem Description

某天小鑫忽然得到了许多的数字,他很好学,老师给他布置了一个任务,求出这些数字中,小于他们平均数、等于他们平均数、大于他们平均数的数字的数量是多少。(对于出现的平均数,保证都是整数,不会出现浮点数)

Input

 多组输入。

对于每次的输入,第一行一个整数N(1 <= N <= 10),代表数字的个数。

接下来的一行,输入N个整数M(0 <= M <= 100)。

Output

 输出包含三个数,第一个跟第二个数后面是空格,最后一个数后面是换行。

第一个数是这些数字中小于他们平均数的数字的个数,第二个数为等于他们平均数的数字的个数,第三个数为大于他们平均数的数字的个数。

Sample Input

3
1 2 3
5
2 4 4 5 5

Sample Output

1 1 1
1 2 2

#include<stdio.h>
int main()
{
    int n,a[20],s,ave,x,y,z;
    while(scanf("%d",&n)!=EOF)
    {
        s = 0;
        for(int i = 0; i<n; i++)
        {
            scanf("%d",&a[i]);
            s = s+a[i];
        }
        ave = s/n;
        x = y = z = 0;
        for(int i = 0; i<n; i++)
        {
            if(a[i]<ave)
                x++;
            else if(a[i]==ave)
                y++;
            else
                z++;
        }
        printf("%d %d %d\n",x,y,z);
    }
    return 0;
}

 

四、区间之和

Time Limit: 1000 ms Memory Limit: 65536 KiB

Submit Statistic

Problem Description

 

给定一个由 n 个整数组成的序列A1,A2,……, An 和两个整数L,R,你的任务是写一个程序来计算序列号在[L,R](这是一个闭区间) 这段位置区间内所有数的总和。

 

Input

 

输入只有一组测试数据:

测试数据的第一行为一个整数 n (1< n < 10000);

第二行为 n 个 int 类型的整数;

第三行为两个整数 L,R(0 < L < R <= n)。

Output

 

输出序列号在区间[L,R]内所有数的和,数据保证和在 int 类型范围内。

Sample Input

5
3 5 6 2 9
2 4

Sample Output

13

#include<stdio.h>
int main()
{
    int n,a[10001],s,x,y;
    scanf("%d",&n);
    s = 0;
    for(int i = 0; i<n; i++)
    {
        scanf("%d",&a[i]);
    }
    scanf("%d%d",&x,&y);
    for(int i = x-1; i<y; i++)
    {
        s = s+a[i];
    }
    printf("%d\n",s);
    return 0;
}

 

五、C语言实验——分割整数

Time Limit: 1000 ms Memory Limit: 65536 KiB

Submit Statistic

Problem Description

从键盘输入一个长整数(不超过10位),从高位开始逐位分割并输出。

Input

正整数n,不含前导零。

Output

分割的整数序列,各整数之间用空格格开。
注意,最后一个数字后面没有空格!

Sample Input

678123

Sample Output

6 7 8 1 2 3

#include<stdio.h>
int main()
{
    int n,a[20],x;
    scanf("%d",&n);
    x = 0;
    while(n/10!=0)
    {
        a[x++]= n%10;
        n = n/10;
    }
    a[x++]=n;
    for(int i = x-1; i>=0; i--)
    {
        if(i==0)
            printf("%d\n",a[i]);
        else
            printf("%d ",a[i]);
    }
    return 0;
}

 

六、众数

Time Limit: 1000 ms Memory Limit: 65536 KiB

Submit Statistic

Problem Description

众数是指在一组数据中,出现次数最多的数。例如:1, 1, 3 中出现次数最多的数为 1,则众数为 1。

给定一组数,你能求出众数吗?

Input

输入数据有多组(数据组数不超过 50),到 EOF 结束。

对于每组数据:

  • 第 1 行输入一个整数 n (1 <= n <= 10000),表示数的个数。
  • 第 2 行输入 n 个用空格隔开的整数 Ai (0 <= Ai <= 1000),依次表示每一个数。

Output

对于每组数据,在一行中输出一个整数,表示这组数据的众数。

数据保证有唯一的众数。

Sample Input

3
1 1 3
5
0 2 3 1 2

Sample Output

1
2

#include<stdio.h>
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        int a[10001],b[10001]={0},x,flag,flag1,max,q;
        flag = 0;
        for(int i = 0; i<n; i++)
        {
            scanf("%d",&x);
            flag1=0;
            for(int j =0; j<=flag; j++)
            {
                if(a[j]==x)
                {
                    b[j]++;
                    flag1=1;
                    break;
                }
            }
            if(flag1==0)
                a[flag++]=x;
        }
        max = 0;
        for(int i =0; i<=flag; i++)
        {
            if(b[i]>max)
            {
                max = b[i];
                q = i;
            }
        }
        printf("%d\n",a[q]);
    }
    return 0;
}

 

七、小鑫爱运动

Time Limit: 1000 ms Memory Limit: 65536 KiB

Submit Statistic

Problem Description

小鑫非常喜欢运动,有一次小鑫去参加110米栏的比赛,一共有10名比赛选手,小鑫是1号,由于跑的太专注,最后冲线的时候不知道自己是第几名,只知道每个人最后的成绩,聪明的你可不可以帮帮他?

Input

 多组输入。

先输入一个10,
然后每组输入10个整数,代表10个选手的110米栏成绩m,代表1号到N号的N个选手的成绩m,m范围是(0 < m < 100)。

Output

 输出只有一行,代表小鑫最后的名次是多少。

 因为小鑫长得丑,成绩相同时,他总是排在前面。

 

Sample Input

10
2 5 3 9 7 10 23 12 43 5
10
6 1 7 9 3 4 8 3 2 9

Sample Output

1
6

#include<stdio.h>
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        int s,flag,x;
        scanf("%d",&s);
        flag = 1;
        for(int i = 1; i<n; i++)
        {
            scanf("%d",&x);
            if(x<s)
                flag++;
        }
        printf("%d\n",flag);
    }
    return 0;
}

 

八、C语言实验——数日子

Time Limit: 1000 ms Memory Limit: 65536 KiB

Submit Statistic

Problem Description

光阴似箭,日月如梭,大学的时间真是宝贵,要抓紧时间AC^_^。你知道今天是这一年第几天吗,掐指一算还是要算好久,呵呵还是让计算机来做吧。这里的问题就是让你来写一个程序,输入某年某月某日,判断这一天是这一年的第几天?

Input

输入第一行是数据的组数n<100,下面n行是n组数据,每组数据由3个正整数组成,分别为年、月、日,我们保证每组数据都是有效的日期。

Output

输出所输入的日期是这一年的第几天。

Sample Input

2
2009 1 1
2008 1 3

Sample Output

1
3

#include<stdio.h>
int main()
{
    int n;
    int a[12]= {31,28,31,30,31,30,31,31,30,31,30,31};
    scanf("%d",&n);
    while(n--)
    {
        int year,month,day,s;
        scanf("%d%d%d",&year,&month,&day);
        if(month==1)
        {
            printf("%d\n",day);
        }
        else if(month==2)
        {
            printf("%d\n",day+31);
        }
        else
        {
            s = 0;
            for(int i=0; i<month-1; i++)
            {
                s = s + a[i];
            }
            s = s + day;
            if((year%4==0&&year%100!=0)||(year%400==0))
            {
                s = s+1;
            }
            printf("%d\n",s);
        }
    }
    return 0;
}

 

九、喵帕斯之平地摔

Time Limit: 1000 ms Memory Limit: 65536 KiB

Submit Statistic

Problem Description

 

平地摔可是莲酱的特技!莲酱在平地都可以跌倒,更不用说陡峭的地方了。

 

这里简化一下地面为一维数轴,a[i] 代表第 i 个位置的高度为 a[i]。

对于 a[i - 1] < a[i] > a[i + 1] 的地方被称为绊脚石,会让莲酱跌倒,注意第一个位置和最后一个位置不会成为绊脚石。

 

现在莲酱想知道他从 1 位置走到 n 位置至少要跌到多少次。

Input

多组输入直到EOF。(组数小于100)

 

对于每组数据,首先输入一行包含一个正整数 n 。(1 <= n <= 100)

接下来一行包含 n 个正整数 a[i]。(1 <= a[i] <= 100)

Output

对于每组数据输出一行,包含一个整数为莲酱要跌倒的次数。

Sample Input

9
1 7 8 2 9 3 9 9 3

Sample Output

2

Hint

输入的示例中,第三个位置 8 和第五个位置 9 是绊脚石,总共两个绊脚石。

Source

【2017级《程序设计基础(B)I》期末上机考试】Fish

#include<stdio.h>
int main()
{
    int n,a[101],flag;
    while(~scanf("%d",&n))
    {
        flag = 0;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
        }
        for(int i=1;i<n-1;i++)
        {
            if(a[i]>a[i-1]&&a[i]>a[i+1])
                flag++;
        }
        printf("%d\n",flag);
    }
    return 0;
}

 

十、排序

Time Limit: 1000 ms Memory Limit: 32678 KiB

Submit Statistic

Problem Description

    给你N(N<=100)个数,请你按照从小到大的顺序输出。

Input

    输入数据第一行是一个正整数N,第二行有N个整数。

Output

    输出一行,从小到大输出这N个数,中间用空格隔开。

Sample Input

5
1 4 3 2 5

Sample Output

1 2 3 4 5

#include<stdio.h>
int main()
{
    int n,a[101],flag;
    scanf("%d",&n);
    for(int i=0; i<n; i++)
    {
        scanf("%d",&a[i]);
    }
    for(int i=0; i<n-1; i++)
    {
        for(int j =i+1;j<n;j++)
        {
            if(a[i]>a[j])
            {
                flag = a[i];
                a[i] = a[j];
                a[j] = flag;
            }
        }
    }
    for(int i=0; i<n-1; i++)
    {
        printf("%d ",a[i]);
    }
    printf("%d\n",a[n-1]);
    return 0;
}

 

十一、排序问题

Time Limit: 1000 ms Memory Limit: 65536 KiB

Submit Statistic

Problem Description

输入10个整数,将它们从小到大排序后输出,并给出现在每个元素在原来序列中的位置。

Input

输入数据有一行,包含10个整数,用空格分开。

Output

输出数据有两行,第一行为排序后的序列,第二行为排序后各个元素在原来序列中的位置。

Sample Input

1 2 3 5 4 6 8 9 10 7

Sample Output

1 2 3 4 5 6 7 8 9 10
1 2 3 5 4 6 10 7 8 9

#include<stdio.h>
int main()
{
    int a[11],b[11],flag;
    for(int i=0; i<10; i++)
    {
        scanf("%d",&a[i]);
        b[i]=a[i];
    }
    for(int i=0; i<9; i++)
    {
        for(int j =i+1; j<10; j++)
        {
            if(a[i]>a[j])
            {
                flag = a[i];
                a[i] = a[j];
                a[j] = flag;
            }
        }
    }
    for(int i=0; i<9; i++)
    {
        printf("%d ",a[i]);
    }
    printf("%d\n",a[9]);
    for(int i=0; i<10; i++)
    {
        for(int j = 0; j<10; j++)
        {
            if(a[i]==b[j])
            {
                if(i!=9)
                {
                    printf("%d ",j+1);
                }
                else
                {
                    printf("%d\n",j+1);
                }
                break;
            }
        }
    }
    return 0;
}

 

十二、数列有序!

Time Limit: 1000 ms Memory Limit: 65536 KiB

Submit Statistic

Problem Description

有n(n<=100)个整数,已经按照从小到大顺序排列好,现在另外给一个整数m,请将该数插入到序列中,并使新的序列仍然有序。

Input

输入数据包含多个测试实例,每组数据由两行组成,第一行是n和m,第二行是已经有序的n个数的数列。n和m同时为0表示输入数据的结束,本行不做处理。

Output

对于每个测试实例,输出插入新的元素后的数列。

Sample Input

3 3
1 2 4
0 0

Sample Output

1 2 3 4

#include<stdio.h>
int main()
{
    int n,m,a[101];
    while(scanf("%d%d",&n,&m)!=EOF&&n!=0&&m!=0)
    {
        for(int i=0; i<n; i++)
        {
            scanf("%d",&a[i]);
        }
        if(m<=a[0])
        {
            printf("%d ",m);
            for(int i=0; i<n-1; i++)
            {
                printf("%d ",a[i]);
            }
            printf("%d\n",a[n-1]);
        }
        else if(m>=a[n-1])
        {
            for(int i=0; i<n; i++)
            {
                printf("%d ",a[i]);
            }
            printf("%d\n",m);
        }
        else
        {
            for(int i=0; i<n-1; i++)
            {
                printf("%d ",a[i]);
                if(m>=a[i]&&m<a[i+1])
                    printf("%d ",m);
            }
            printf("%d\n",a[n-1]);
        }
    }
    return 0;
}

 

十三、中位数

Time Limit: 1000 ms Memory Limit: 65536 KiB

Submit Statistic

Problem Description

中位数是指在一组数据中,按数值大小排序后处于中间位置的数。例如:1, 5, 3 排序后为 1, 3, 5,则其中位数为 3。特别地,当数的个数 N 为偶数时,中位数取位置居中的两个数 (N/2 和 N/2+1) 的平均值,例如:1, 2, 3, 4,中位数为 (2+3)/2 = 2.5。

给定一组数,你能求出中位数吗?

Input

输入数据有多组(数据组数不超过 100),到 EOF 结束。

对于每组数据:

  • 第 1 行输入一个整数 n (3 <= n <= 100),表示数的个数。
  • 第 2 行输入 n 个用空格隔开的整数 Ai (0 <= Ai <= 1000),依次表示每一个数,保证互不重复。

Output

对于每组数据,在一行中输出一个实数(保留 1 位小数),表示这组数据的中位数。

Sample Input

3
1 5 3
4
1 2 3 4

Sample Output

3.0
2.5

Hint

Source

【2016级《程序设计基础(B)I》期末上机考试-第二场】bLue

#include<stdio.h>
int main()
{
    int n;
    double s,a[101];
    while(scanf("%d",&n)!=EOF)
    {
        int flag;
        for(int i=0; i<n; i++)
        {
            scanf("%lf",&a[i]);
        }
        for(int i=0; i<n-1; i++)
        {
            for(int j=i+1; j<n; j++)
            {
                if(a[j]>a[i])
                {
                    flag = a[j];
                    a[j] = a[i];
                    a[i] = flag;
                }
            }
        }
        if(n%2==0)
        {
            s = (a[n/2]+a[n/2-1])/2;
        }
        else
        {
            s = a[n/2];
        }
        printf("%.1lf\n",s);
    }
    return 0;
}

 

十四、C语言实验——各位数字之和排序

Time Limit: 1000 ms Memory Limit: 65536 KiB

Submit Statistic

Problem Description

给定n个正整数,根据各位数字之和从小到大进行排序。

Input

输入数据有多组,每组数据占一行,每行的第一个数正整数n,表示整数个数,后面接n个正整数。当n为0时,不作任何处理,输入结束。n<=10

Output

输出每组排序的结果。

Sample Input

3 230 59 110
5 199 220 108 235 120
0

Sample Output

110 230 59
120 220 108 235 199

#include<stdio.h>
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF&&n!=0)
    {
        int s,a[11]={0},b[11],c[11],flag;
        for(int i=0; i<n; i++)
        {
            scanf("%d",&s);
            b[i]=s;
            while(s!=0)
            {
                a[i]=a[i]+s%10;
                s = s/10;
            }
            c[i]=a[i];
        }
        for(int i=0; i<n-1; i++)
        {
            for(int j=i+1; j<n; j++)
            {
                if(a[j]<a[i])
                {
                    flag = a[j];
                    a[j] = a[i];
                    a[i] = flag;
                }
            }
        }
        for(int i=0; i<n; i++)
        {
            for(int j=0;j<n;j++)
            {
                if(a[i]==c[j])
                {
                    if(i!=n-1)
                        printf("%d ",b[j]);
                    else
                        printf("%d\n",b[j]);
                    break;
                }
            }
        }
    }
    return 0;
}

 

十五、期末考试之排名次

Time Limit: 1000 ms Memory Limit: 65536 KiB

Submit Statistic

Problem Description

期末考试结束了,童鞋们的成绩也出来的了,可是为了排名次可忙坏了老师,因为学生太多了。这时,老师把这个任务交给了你,希望你能帮老师完成。作为IT人,你当然不能用笨笨的人工方法了,编程解决才是好办法。
共有三门课,语文、数学和英语,要求根据学生的各科成绩计算出其总成绩,并根据总成绩从高到低排序。

Input

第一行一个整数N(N<=100),代表学生的人数。
接下来的N行数据,每行有三个整数,C,M,E分别代表一个学生语文、数学和英语的成绩。

Output

一共N行,每行一个数,从大到小,分别代表各个学生的总成绩。

Sample Input

3
70 80 90
59 59 59
100 100 100

Sample Output

300
240
177

#include<stdio.h>
int main()
{
    int n,s[101],flag;
    scanf("%d",&n);
    for(int i=0; i<n; i++)
    {
        int a,b,c;
        scanf("%d%d%d",&a,&b,&c);
        s[i]= a + b + c;
    }
    for(int i=0; i<n-1; i++)
    {
        for(int j=i+1; j<n; j++)
        {
            if(s[j]>s[i])
            {
                flag = s[j];
                s[j] = s[i];
                s[i] = flag;
            }
        }
    }
    for(int i=0; i<n; i++)
    {
        printf("%d\n",s[i]);
    }
    return 0;
}

 

十六、次大和次小

Time Limit: 1000 ms Memory Limit: 65536 KiB

Submit Statistic

Problem Description

对于一个数组,次大的数指数组中第二大的数,相似地,次小的数指数组中第二小的数。

给定一个含有 n 个数的数组(数组中的数互不相同),求其中次大的数和次小的数。

Input

首先输入一个整数 T (1 <= T <= 200),表示数据组数。

对于每组数据:

  • 第 1 行输入一个整数 n (2 <= n <= 1000),表示数组中元素的数量。
  • 第 2 行输入 n 个用空格隔开的整数 Ai (-10000 <= Ai <= 10000),表示数组中每一个元素的值。

Output

对于每组数据,输出一行,包含 2 个整数 a, b,分别表示次大和次小的数。

Sample Input

1
5
3 1 2 4 5

Sample Output

4 2

#include<stdio.h>
int main()
{
    int T,n,s[1001],flag;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        for(int i=0; i<n; i++)
        {
            scanf("%d",&s[i]);
        }
        for(int i=0; i<n-1; i++)
        {
            for(int j=i+1; j<n; j++)
            {
                if(s[j]>s[i])
                {
                    flag = s[j];
                    s[j] = s[i];
                    s[i] = flag;
                }
            }
        }
        printf("%d %d\n",s[1],s[n-2]);
    }
    return 0;
}

 

十七、冒泡排序中数据交换的次数

Time Limit: 1000 ms Memory Limit: 65536 KiB

Submit Statistic

Problem Description

听说过冒泡排序么?一种很暴力的排序方法。今天我们不希望你用它来排序,而是希望你能算出从小到大冒泡排序的过程中一共进行了多少次数据交换。

Input

输入数据的第一行为一个正整数 T ,表示有 T 组测试数据。
接下来T行,每行第一个整数N, 然后有N个整数,无序。0<N <= 100

Output

输出共 T 行。
每行一个整数,代表本行数据从小到大冒泡排序所进行的交换次数。

Sample Input

3
5 1 2 3 4 5
4 5 3 7 1
2 2 1

Sample Output

0
4
1

#include<stdio.h>
int main()
{
    int T,n,s[101],flag,sum;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        sum = 0;
        for(int i=0; i<n; i++)
        {
            scanf("%d",&s[i]);
        }
        for(int i=0; i<n-1; i++)
        {
            for(int j = 0; j<n-1-i; j++)
            {
                if(s[j]>s[j+1])
                {
                    flag = s[j];
                    s[j] = s[j+1];
                    s[j+1] = flag;
                    sum++;
                }
            }
        }
        printf("%d\n",sum);
    }
    return 0;
}

 

十八、小金追呀追不上妹子

Time Limit: 1000 ms Memory Limit: 65536 KiB

Submit Statistic

Problem Description

众所周知,C语言的学习是我们程序设计基础的重点和主要内容。
小金知道他喜欢的妹子最喜欢的水果是苹果,但是小金是种玉米的哪!所以他为了讨好妹子的欢心,他会从收获的n个玉米中挑选出m个最大的玉米去抠脚大汉那里换苹果,问题是,他这m个玉米的价值有多大?

Input

多组输入。
每行开始输入两个整数分别为n,m。代表含义如题目所述。
接下来一行有n个整数,代表每个玉米的价值。
1 < = m < n < = 1000 

Output

输出小金m个最大玉米的总价值。
输出占一行,保证数据合法。

Sample Input

10 4
1 2 3 4 5 6 7 8 9 10
5 3
1 2 3 4 5

Sample Output

34
12

#include<stdio.h>
int main()
{
    int n,m,s[1001],flag,sum;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        sum = 0;
        for(int i=0; i<n; i++)
        {
            scanf("%d",&s[i]);
        }
        for(int i=0; i<n-1; i++)
        {
            for(int j=i+1; j<n; j++)
            {
                if(s[j]>s[i])
                {
                    flag = s[j];
                    s[j] = s[i];
                    s[i] = flag;
                }
            }
        }
        for(int i=0; i<m; i++)
        {
            sum = sum+s[i];
        }
        printf("%d\n",sum);
    }
    return 0;
}

 

十九、C语言实验——数组逆序

Time Limit: 1000 ms Memory Limit: 65536 KiB

Submit Statistic

Problem Description

有n个整数,使其最后m个数变成最前面的m个数,其他各数顺序向后移m(m < n < 100)个位置。

Input

输入数据有2行,第一行的第一个数为n,后面是n个整数,第二行整数m。

Output

按先后顺序输出n个整数。

Sample Input

5 1 2 3 4 5
2

Sample Output

4 5 1 2 3

#include<stdio.h>
int main()
{
    int n,m,s[101];
    scanf("%d",&n);
    for(int i=0; i<n; i++)
    {
        scanf("%d",&s[i]);
    }
    scanf("%d",&m);
    for(int i=n-m; i<n; i++)
    {
        printf("%d ",s[i]);
    }
    for(int i=0; i<n-m-1; i++)
    {
        printf("%d ",s[i]);
    }
    printf("%d\n",s[n-m-1]);
    return 0;
}

 

二十、矩阵输出

Time Limit: 1000 ms Memory Limit: 65536 KiB

Submit Statistic

Problem Description

输入n个整数,输出由这些整数组成的n行矩阵。

Input

第一行输入一个正整数N(N<=20),表示后面要输入的整数个数。
下面依次输入N个整数。

Output

以输入的整数为基础,输出有规律的n行数据。

Sample Input

5
3 6 2 5 8

Sample Output

3 6 2 5 8
8 3 6 2 5
5 8 3 6 2
2 5 8 3 6
6 2 5 8 3

#include<stdio.h>
int main()
{
    int n,s[101],flag;
    scanf("%d",&n);
    for(int i=0; i<n; i++)
    {
        scanf("%d",&s[i]);
    }
    for(int i=0;i<n-1;i++)
    {
        printf("%d ",s[i]);
    }
    printf("%d\n",s[n-1]);
    for(int i=0;i<n-1;i++)
    {
        flag = s[n-1];
        for(int j =n-1;j>0;j--)
        {
            s[j]=s[j-1];
        }
        s[0]=flag;
        for(int j =0;j<n-1;j++)
        {
            printf("%d ",s[j]);
        }
        printf("%d\n",s[n-1]);
    }
    return 0;
}

 

  • 23
    点赞
  • 115
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值