zoukankan      html  css  js  c++  java
  • 计算机历年考研复试上机基础题(一)

    abc

    题目描述

    设a、b、c均是0到9之间的数字,abc、bcc是两个三位数,且有:abc+bcc=532。求满足条件的所有a、b、c的值。

    输入描述:

    题目没有任何输入。

    输出描述:

    请输出所有满足题目条件的a、b、c的值。
    a、b、c之间用空格隔开。
    每个输出占一行。
     1 #include <bits/stdc++.h>
     2 
     3 using namespace std;
     4 
     5 int main(){
     6     for(int i=1;i<=9;i++){
     7         for(int j=1;j<=9;j++){
     8             for(int k=0;k<=9;k++){
     9                 int num1=i*100+j*10+k;
    10                 int num2=j*100+k*10+k;
    11                 int tmp=num1+num2;
    12                 if(tmp==532){
    13                     cout<<i<<' '<<j<<' '<<k<<endl;
    14                 }
    15             }
    16         }
    17     }
    18     return 0;
    19 }

    最大公约数

    题目描述

    输入两个正整数,求其最大公约数。

    输入描述:

    测试数据有多组,每组输入两个正整数。

    输出描述:

    对于每组输入,请输出其最大公约数。
    示例1

    输入

    49 14
    

    输出

    7
     1 #include <bits/stdc++.h>
     2 
     3 using namespace std;
     4 
     5 //欧几里德算法 两个整数的最大公约数等于其中较小的那个数和两数相除余数的最大公约数
     6 int gcd(int x,int y){
     7          if( y==0)
     8                   return x;
     9          return gcd(y,x%y);
    10 }
    11 
    12 int main(){
    13          int x,y;
    14          while(scanf("%d%d",&x,&y)!=EOF){
    15                   int sum=gcd(x,y);
    16                   cout<<sum<<endl;
    17          }
    18 }

    吃糖果

    题目描述

    名名的妈妈从外地出差回来,带了一盒好吃又精美的巧克力给名名(盒内共有 N 块巧克力,20 > N >0)。 妈妈告诉名名每天可以吃一块或者两块巧克力。 假设名名每天都吃巧克力,问名名共有多少种不同的吃完巧克力的方案。 例如: 如果N=1,则名名第1天就吃掉它,共有1种方案; 如果N=2,则名名可以第1天吃1块,第2天吃1块,也可以第1天吃2块,共有2种方案; 如果N=3,则名名第1天可以吃1块,剩2块,也可以第1天吃2块剩1块,所以名名共有2+1=3种方案; 如果N=4,则名名可以第1天吃1块,剩3块,也可以第1天吃2块,剩2块,共有3+2=5种方案。 现在给定N,请你写程序求出名名吃巧克力的方案数目。

    输入描述:

    输入只有1行,即整数N。

    输出描述:

    可能有多组测试数据,对于每组数据,
    输出只有1行,即名名吃巧克力的方案数。
    示例1

    输入

    4
    

    输出

    5

    有点斐波那契数列的味道,emmm,
    比如有5块巧克力,
    那么,第一天,
      要不然吃一块,还剩4个,那么就是4块巧克力吃几天的问题,
    要不然吃两块,还剩3块,那么及时3块巧克力吃几天的问题,
    由题目可知,有1,2,3,4,5块巧克力能吃几天的数量已知,而N的上限不是很大,所以可按照斐波那契数列的思想迭代出来,所有N的情况(1<=N<=19)
     1 #include <bits/stdc++.h>
     2 
     3 using namespace std;
     4 int a[20]={0};
     5 
     6 int main(){
     7          int N;
     8          a[1]=1,a[2]=2;a[3]=3,a[4]=5;
     9          for(int i=5;i<=19;i++){
    10                            a[i]=a[i-1]+a[i-2];
    11         }
    12          while(scanf("%d",&N)!=EOF){
    13                   int tmp=a[N];
    14                   cout<<tmp<<endl;
    15          }
    16          return 0;
    17 }

    数字求和

    题目描述

    给定一个正整数a,以及另外的5个正整数,问题是:这5个整数中,小于a的整数的和是多少?

    输入描述:

    输入一行,只包括6个小于100的正整数,其中第一个正整数就是a。

    输出描述:

    可能有多组测试数据,对于每组数据,
    输出一行,给出一个正整数,是5个数中小于a的数的和。
    示例1

    输入

    10 1 2 3 4 11
    

    输出

    10
     1 #include <bits/stdc++.h>
     2 
     3 using namespace std;
     4 int main(){
     5     int tmp,num,sum=0;
     6     std::cout.sync_with_stdio(false);
     7     std::cin.sync_with_stdio(false);
     8         for(int i=0;i<6;i++){
     9             cin>>num;
    10             if(i==0){
    11                 tmp=num;
    12             }else if(num<tmp){
    13                 sum+=num;
    14             }
    15         }
    16        cout<<sum<<endl;
    17 }

    Fibonacci 

    题目描述

        The Fibonacci Numbers{0,1,1,2,3,5,8,13,21,34,55...} are defined by the recurrence:     F0=0 F1=1 Fn=Fn-1+Fn-2,n>=2     Write a program to calculate the Fibonacci Numbers.

    输入描述:

        Each case contains a number n and you are expected to calculate Fn.(0<=n<=30) 。

    输出描述:

       For each case, print a number Fn on a separate line,which means the nth Fibonacci Number.
    示例1

    输入

    1
    

    输出

    1
     1 #include <bits/stdc++.h>
     2 #include <stdio.h>
     3 using namespace std;
     4 int a[32]={0};
     5 int main(){
     6     a[0]=0,a[1]=1;a[2]=1;
     7     for(int i=3;i<=30;i++){
     8         a[i]=a[i-1]+a[i-2];
     9     }
    10     int n;
    11     cin>>n;
    12     cout<<a[n]<<endl;
    13     return 0;
    14 }

    三角形的边

    题目描述

    给定三个已知长度的边,确定是否能够构成一个三角形,这是一个简单的几何问题。我们都知道,这要求两边之和大于第三边。实际上,并不需要检验所有三种可能,只需要计算最短的两个边长之和是否大于最大那个就可以了。 这次的问题就是:给出三个正整数,计算最小的数加上次小的数与最大的数之差。

    输入描述:

    每一行包括三个数据a, b, c,并且都是正整数,均小于10000。

    输出描述:

    对于输入的每一行,在单独一行内输出结果s。s=min(a,b,c)+mid(a,b,c)-max(a,b,c)。上式中,min为最小值,mid为中间值,max为最大值。
    示例1

    输入

    1 2 3
    

    输出

    0
     1 #include <bits/stdc++.h>
     2 
     3 using namespace std;
     4 
     5 int main(){
     6     int a[3]={0};
     7     int sum=0;
     8     for(int i=0;i<3;i++){
     9         cin>>a[i];
    10         sum+=a[i];
    11     }
    12     int mmin=a[0];
    13     int mmax=a[0];
    14     for(int i=1;i<3;i++){
    15         if(mmin>a[i])
    16             mmin=a[i];
    17         if(mmax<a[i])
    18             mmax=a[i];
    19     }
    20     int mmid=sum-mmin-mmax;
    21     cout<<(mmin+mmid)-mmax<<endl;
    22     return 0;
    23 }

    数字之和

    题目描述

    对于给定的正整数 n,计算其十进制形式下所有位置数字之和,并计算其平方的各位数字之和。

    输入描述:

    每行输入数据包括一个正整数n(0<n<40000)

    输出描述:

    对于每个输入数据,计算其各位数字之和,以及其平方值的数字之和,输出在一行中,之间用一个空格分隔,但行末不要有空格。
    示例1

    输入

    4
    12
    97
    39999
    

    输出

    4 7
    3 9
    16 22
    39 36
     1 #include <bits/stdc++.h>
     2 
     3 using namespace std;
     4 
     5 int main(){
     6     long n,len=0;
     7     int a[20]={0};
     8     int b[20]={0};
     9     cin>>n;
    10     long num=n*n;
    11     while(n){
    12         len++;
    13         a[len]=n%10;
    14         n=(n-a[len])/10;
    15     }
    16     int sum1=0;
    17     for(int j=1;j<=len;j++){
    18         sum1+=a[j];
    19     }
    20     len=0;
    21     while(num){
    22         b[len]=num%10;
    23         num=(num-b[len])/10;
    24         len++;
    25     }
    26     int sum2=0;
    27     for(int j=0;j<=len;j++){
    28         sum2+=b[j];
    29     }
    30     cout<<sum1<<' '<<sum2<<endl;
    31     return 0;
    32 }

    求平均年龄

    题目描述

    班上有学生若干名,给出每名学生的年龄(整数),求班上所有学生的平均年龄,保留到小数点后两位。

    输入描述:

    第一行有一个整数n(1<= n <= 100),表示学生的人数。其后n行每行有1个整数,取值为15到25。

    输出描述:

    可能有多组测试数据,对于每组数据,
    输出一行,该行包含一个浮点数,为要求的平均年龄,保留到小数点后两位。
    
    要输出浮点数、双精度数小数点后2位数字,可以用下面这种形式: 
    printf("%.2f", num);
    示例1

    输入

    2
    18
    17
    

    输出

    17.50
     1 #include <bits/stdc++.h>
     2 #include <stdio.h>
     3 #include <stdlib.h>
     4 
     5 using namespace std;
     6 
     7 int main(){
     8     int n;
     9     scanf("%d", &n);
    10     int num=0;
    11     float sum=0;
    12     for(int i=1;i<=n;i++){
    13         scanf("%d", &num);
    14         sum+=num;
    15     }
    16     printf("%.2f", (sum/n));
    17     return 0;
    18 }

    Digital Roots

    题目描述

        The digital root of a positive integer is found by summing the digits of the integer. If the resulting value is a single digit then that digit is the digital root. If the resulting value contains two or more digits, those digits are summed and the process is repeated. This is continued as long as necessary to obtain a single digit.     For example, consider the positive integer 24. Adding the 2 and the 4 yields a value of 6. Since 6 is a single digit, 6 is the digital root of 24. Now consider the positive integer 39. Adding the 3 and the 9 yields 12. Since 12 is not a single digit, the process must be repeated. Adding the 1 and the 2 yeilds 3, a single digit and also the digital root of 39.

    输入描述:

        The input file will contain a list of positive integers, one per line. 
    The integer may consist of a large number of digits.

    输出描述:

        For each integer in the input, output its digital root on a separate line of the output.
    示例1

    输入

    24
    39
    

    输出

    6
    3
    思想就是,例如24 ,2+4=6, 6大于等于10,满足
           39 ,3+9=12 大于10, 然后就变成1+2=3, 3满足条件
     1 #include <stdio.h>
     2 #include <algorithm>
     3 #include <stdlib.h>
     4 #include <string>
     5 #include <string.h>
     6 #include <math.h>
     7 
     8 using namespace std;
     9 int a[12]={0};
    10 
    11 int fun(int n){
    12     int sum=0,i=0;
    13     memset(a,0,sizeof(a));
    14     while(n){
    15         a[i]=n%10;
    16         sum+=a[i];
    17         n=(n-a[i])/10;
    18         i++;
    19     }
    20     return sum;
    21 }
    22 
    23 int main(){
    24     int n;
    25     scanf("%d",&n);
    26     int tmp=fun(n);
    27     while(tmp>=10)
    28         tmp=fun(tmp);
    29     printf("%d
    ",tmp);
    30     return 0;
    31 }

    百鸡问题

    题目描述

        用小于等于n元去买100只鸡,大鸡5元/只,小鸡3元/只,还有1/3元每只的一种小鸡,分别记为x只,y只,z只。编程求解x,y,z所有可能解。

    输入描述:

        测试数据有多组,输入n。

    输出描述:

        对于每组输入,请输出x,y,z所有可行解,按照x,y,z依次增大的顺序输出。
    示例1

    输入

    40
    

    输出

    x=0,y=0,z=100
    x=0,y=1,z=99
    x=0,y=2,z=98
    x=1,y=0,z=99

     1 #include <stdio.h>
     2 #include <algorithm>
     3 #include <stdlib.h>
     4 #include <string>
     5 #include <string.h>
     6 #include <math.h>
     7 #define maxn 100
     8 using namespace std;
     9 
    10 int main(){
    11     int n;
    12     scanf("%d",&n);
    13     for(int x=0;x<=n/5;x++){
    14         for(int y=0;y<=(n-5*x)/3;y++){
    15             for(int z=0;z<=(n-5*x-3*y)*3;z++){
    16                 if(x+y+z==100){
    17                     printf("x=%d,y=%d,z=%d
    ",x,y,z);
    18                 }
    19             }
    20         }
    21     }
    22     return 0;
    23 }

    字符串排序

    题目描述

     输入一个长度不超过20的字符串,对所输入的字符串,按照ASCII码的大小从小到大进行排序,请输出排序后的结果

    输入描述:

     一个字符串,其长度n<=20

    输出描述:

     输入样例可能有多组,对于每组测试样例,
    按照ASCII码的大小对输入的字符串从小到大进行排序,输出排序后的结果
    示例1

    输入

    dcba
    

    输出

    abcd

    小析:理解字符和ASCII码的输出
     1 #include <stdio.h>
     2 #include <algorithm>
     3 #include <stdlib.h>
     4 #include <string>
     5 #include <string.h>
     6 #include <math.h>
     7 #include <iostream>
     8 #define maxn 22
     9 using namespace std;
    10 
    11 int a[maxn]={0};
    12 char str[22];
    13 int main(){
    14     scanf("%s",&str);
    15     for(int i=0;i<=20;i++){
    16         a[i]=str[i];
    17     }
    18     sort(a,a+20);
    19     for(int i=0;i<=20;i++){
    20         if(a[i])
    21             printf("%c",a[i]);
    22     }
    23     return 0;
    24 }

    xxx定律

    题目描述

        对于一个数n,如果是偶数,就把n砍掉一半;如果是奇数,把n变成 3*n+ 1后砍掉一半,直到该数变为1为止。     请计算需要经过几步才能将n变到1,具体可见样例。

    输入描述:

        测试包含多个用例,每个用例包含一个整数n,当n为0 时表示输入结束。(1<=n<=10000)

    输出描述:

        对于每组测试用例请输出一个数,表示需要经过的步数,每组输出占一行。
    示例1

    输入

    3
    1
    0
    

    输出

    5
    0
     1 #include <stdio.h>
     2 #include <algorithm>
     3 #include <stdlib.h>
     4 #include <string>
     5 #include <string.h>
     6 #include <math.h>
     7 #include <iostream>
     8 #define maxn 22
     9 using namespace std;
    10 
    11 int a[maxn]={0};
    12 char str[22];
    13 int main(){
    14     int n=0;
    15     while(scanf("%d",&n)!=EOF){
    16             int sum=0;
    17             if(n==0){
    18                 printf("0
    ");
    19                 return 0;
    20             }
    21         while(n!=1){
    22         if(n%2==0){
    23             n=n/2;
    24             sum++;
    25         }else{
    26             n=n*3+1;
    27             n=n/2;
    28             sum++;
    29         }
    30     }
    31     printf("%d
    ",sum);
    32     }
    33     return 0;
    34 }

    字符串内排序

    题目描述

    输入一个字符串,长度小于等于200,然后将输出按字符顺序升序排序后的字符串。

    输入描述:

    测试数据有多组,输入字符串。

    输出描述:

    对于每组输入,输出处理后的结果。
    示例1

    输入

    bacd
    

    输出

    abcd

    小析:和上面那题基本上没什么区别
     1 #include <stdio.h>
     2 #include <algorithm>
     3 #include <stdlib.h>
     4 #include <string>
     5 #include <string.h>
     6 #include <math.h>
     7 #include <iostream>
     8 #define maxn 220
     9 using namespace std;
    10 
    11 int a[maxn]={0};
    12 char str[220];
    13 int main(){
    14     scanf("%s",&str);
    15     for(int i=0;i<=200;i++){
    16         a[i]=str[i];
    17     }
    18     sort(a,a+200);
    19     for(int i=0;i<=200;i++){
    20         if(a[i])
    21             printf("%c",a[i]);
    22     }
    23     return 0;
    24 }

    神奇的口袋

    题目描述

    有一个神奇的口袋,总的容积是40,用这个口袋可以变出一些物品,这些物品的总体积必须是40。John现在有n个想要得到的物品,每个物品的体积分别是a1,a2……an。John可以从这些物品中选择一些,如果选出的物体的总体积是40,那么利用这个神奇的口袋,John就可以得到这些物品。现在的问题是,John有多少种不同的选择物品的方式。

    输入描述:

    输入的第一行是正整数n (1 <= n <= 20),表示不同的物品的数目。接下来的n行,每行有一个1到40之间的正整数,分别给出a1,a2……an的值。

    输出描述:

    输出不同的选择物品的方式的数目。
    示例1

    输入

    3
    20
    20
    20
    

    输出

    3
     1 #include <stdio.h>
     2 #include <algorithm>
     3 #include <stdlib.h>
     4 #include <string>
     5 #include <string.h>
     6 #include <math.h>
     7 #include <iostream>
     8 #define maxn 50
     9 using namespace std;
    10 
    11 int a[maxn]={0};
    12 int dp[100][43]; //数组大小
    13 
    14 int main(){
    15     int n=0;
    16     scanf("%d",&n);
    17     for(int i=1;i<=n;i++){
    18         scanf("%d",&a[i]);
    19         dp[i][0]=1;
    20     }
    21     dp[0][0]=1;
    22     for(int i=1;i<=n;i++){
    23         for(int j=1;j<=40;j++){
    24             dp[i][j]=dp[i-1][j];
    25             if(j>=a[i])
    26                 dp[i][j]+=dp[i-1][j-a[i]];
    27            //cout<<dp[i][j]<<" ";
    28         }
    29        // cout<<endl;
    30     }
    31     cout<<dp[n][40]<<endl;
    32     return 0;
    33 }

    统计同成绩的人数

    题目描述

    读入N名学生的成绩,将获得某一给定分数的学生人数输出。

    输入描述:

    测试输入包含若干测试用例,每个测试用例的格式为
    
    
    第1行:N
    第2行:N名学生的成绩,相邻两数字用一个空格间隔。
    第3行:给定分数
    
    当读到N=0时输入结束。其中N不超过1000,成绩分数为(包含)0到100之间的一个整数。

    输出描述:

    对每个测试用例,将获得给定分数的学生人数输出。
    示例1

    输入

    3
    80 60 90
    60
    2
    85 66
    0
    5
    60 75 90 55 75
    75
    0
    

    输出

    1
    0
    2
     1 #include <stdio.h>
     2 #include <algorithm>
     3 #include <stdlib.h>
     4 #include <string>
     5 #include <string.h>
     6 #include <math.h>
     7 #include <iostream>
     8 #define maxn 110
     9 using namespace std;
    10 
    11 int a[maxn]={0};
    12 
    13 int main(){
    14     int n=0;
    15     while(scanf("%d",&n)!=EOF){
    16             if(n==0) return 0;
    17             int tmp;
    18         for(int i=0;i<n;i++){
    19             scanf("%d",&tmp);
    20             a[tmp]++;
    21         }
    22         int key=0;
    23         scanf("%d",&key);
    24         printf("%d
    ",a[key]);
    25     }
    26     return 0;
    27 }

    数组逆置

    题目描述 

    输入一个字符串,长度小于等于200,然后将数组逆置输出。

    输入描述:

    测试数据有多组,每组输入一个字符串。

    输出描述:

    对于每组输入,请输出逆置后的结果。
    示例1

    输入

    hdssg
    

    输出

    gssdh
     1 #include <stdio.h>
     2 #include <algorithm>
     3 #include <stdlib.h>
     4 #include <string>
     5 #include <string.h>
     6 #include <math.h>
     7 #include <iostream>
     8 #define maxn 210
     9 using namespace std;
    10 
    11 char a[maxn]={'0'};
    12 
    13 int main(){
    14     scanf("%s",&a);
    15     int len=strlen(a);
    16     for(int i=len-1;i>=0;i--){
    17             printf("%c",a[i]);
    18     }
    19 
    20     return 0;
    21 }

    Skew数

    题目描述 

    在 skew binary表示中, 第 k 位的值xk表示xk*(2k+1-1)。 每个位上的可能数字是0 或 1,最后面一个非零位可以是2, 例如, 10120(skew) = 1*(25-1) + 0*(24-1) + 1*(23-1) + 2*(22-1) + 0*(21-1) = 31 + 0 + 7 + 6 + 0 = 44. 前十个skew数是 0、1、2、10、11、12、20、100、101、以及102。

    输入描述:

    输入包含一行或多行,每行包含一个整数n。如果 n = 0 表示输入结束,否则n是一个skew数

    输出描述:

    可能有多组测试数据,对于每一个输入,
    输出它的十进制表示。转换成十进制后, n 不超过 231-1 = 2147483647
    示例1

    输入

    10120
    200000000000000000000000000000
    10
    1000000000000000000000000000000
    11
    100
    11111000001110000101101102000
    0
    

    输出

    44
    2147483646
    3
    2147483647
    4
    7
    1041110737
    ,快速幂,然后按着给定的公式来,数字比较大,以字符形式接收,如a[1]='1',a[1]-'0'=1, 把字符1转换为了数字1,
    快速幂思想就是把幂二进制化,然后运用&运算取得最后一位二进制位,<<运算,将二进制数右移一位,


     1 #include <stdio.h>
     2 #include <algorithm>
     3 #include <stdlib.h>
     4 #include <string>
     5 #include <string.h>
     6 #include <math.h>
     7 #include <iostream>
     8 #define maxn 50
     9 using namespace std;
    10 
    11 char a[maxn]={'0'};
    12 long long pow(int a,int  b){
    13     long long sum=1;
    14     while(b){
    15         if(b&1)
    16             sum=sum*a;
    17         a*=a;
    18         b>>=1;
    19     }
    20     return sum;
    21 }
    22 
    23 int main(){
    24     scanf("%s",&a);
    25     int len=strlen(a);
    26     int k=len;
    27     long long sum=0,tmp=0;
    28     for(int i=0;i<len;i++){
    29         tmp=(a[i]-'0')*(pow(2,k)-1);
    30         sum+=tmp;
    31         k--;
    32     }
    33     cout<<sum<<endl;
    34     return 0;
    35 }

     

    放苹果

    题目描述

    把M个同样的苹果放在N个同样的盘子里,允许有的盘子空着不放,问共有多少种不同的分法?(用K表示)5,1,1和1,5,1 是同一种分法。

    输入描述:

    每行均包含二个整数M和N,以空格分开。1<=M,N<=10。

    输出描述:

    对输入的每组数据M和N,用一行输出相应的K。
    示例1

    输入

    复制
    7 3
    

    输出

    复制
    8

    苹果和盘子的故事
    对于两个都可变化数量的物体,要固定其中一个才好分析
    这里选择固定盘子(因为苹果可以不限制数目的放在盘子里) m个苹果,n个盘子,用递归的方法可以解决
    递归体
      当m>=n时,苹果数量多,盘子数量少,那么
           当空出一个盘子后(因为是递归,所以这里的“一”不是真正的一,n-1,n-2,n-3...)
              fun(m,n)=fun(m,n-1)
           不留空盘子,那么,此时花费(m-n)个苹果,
              fun(m,n)=fun(m-n,n);

      当m<n时,苹果数量少,空盘子对结果无增益,所以本题可忽略,那么就转化为在m个盘子里放置m个苹果的故事,
          fun(m,n)=fun(m,m)

      总计为:fun(m,n)=fun(m,n-1)+fun(m-n,n)
    结束条件

      当m==0,时,我们结束递归,return 1;
       当n==1,时,只剩下一个盘子啦,就只有一种情况,return 1;

     1 #include <stdio.h>
     2 #include <algorithm>
     3 #include <stdlib.h>
     4 #include <string>
     5 #include <string.h>
     6 #include <math.h>
     7 #include <iostream>
     8 #define maxn 50
     9 using namespace std;
    10 
    11 char a[maxn]={'0'};
    12 int fun(int m,int n){
    13     if(m==0||n==1) return 1;
    14     if(m<n)
    15         return fun(m,m);
    16     else
    17         return fun(m,n-1)+fun(m-n,n);
    18 }
    19 
    20 int main(){
    21     int m,n;
    22     scanf("%d%d",&m,&n);
    23     int ans=fun(m,n);
    24     printf("%d
    ",ans);
    25     return 0;
    26 }

    Zero-complexity

    题目描述 

    You are given a sequence of integer numbers. Zero-complexity transposition of the sequence is the reverse of this sequence. Your task is to write a program that prints zero-complexity transposition of the given sequence.

    输入描述:

    For each case, the first line of the input file contains one integer n-length of the sequence (0 < n ≤ 10 000). The second line contains n integers numbers-a1, a2, …, an (-1 000 000 000 000 000 ≤ ai ≤ 1 000 000 000 000 000).

    输出描述:

    For each case, on the first line of the output file print the sequence in the reverse order.
    示例1

    输入

    5
    -3 4 6 -8 9
    

    输出

    9 -8 6 4 -3

    数组的翻转

     1 #include <stdio.h>
     2 #include <algorithm>
     3 #include <stdlib.h>
     4 #include <string>
     5 #include <string.h>
     6 #include <math.h>
     7 #include <iostream>
     8 #define maxn 10010
     9 using namespace std;
    10 
    11 long long a[maxn];
    12 
    13 int main(){
    14     int n;
    15     scanf("%d",&n);
    16     for(int i=0;i<n;i++){
    17         scanf("%d",&a[i]);
    18     }
    19     for(int i=n-1;i>=0;i--){
    20         printf("%d ",a[i]);
    21     }
    22     return 0;
    23 }

    子串计算

    题目描述

    给出一个01字符串(长度不超过100),求其每一个子串出现的次数。

    输入描述:

    输入包含多行,每行一个字符串。

    输出描述:

    对每个字符串,输出它所有出现次数在1次以上的子串和这个子串出现的次数,输出按字典序排序。
    示例1

    输入

    10101
    

    输出

    0 2
    01 2
    1 3
    10 2
    101 2

    从左到右找出每一种组合,用map这种的键值对,来对每一种情况进行计数
     1 #include <stdio.h>
     2 #include <algorithm>
     3 #include <stdlib.h>
     4 #include <string>
     5 #include <string.h>
     6 #include <math.h>
     7 #include <iostream>
     8 #include <map>
     9 #define maxn 10010
    10 
    11 using namespace std;
    12 
    13 map<string,int> m;
    14 
    15 int main(){
    16     string str;
    17     cin>>str;
    18     int len=str.length();
    19     if(len==1)
    20         return 0;
    21     for(int i=0;i<len;i++){
    22         for(int j=1;j<=len-i;j++){
    23                 string tmp=str.substr(i,j);
    24                 //cout<<tmp<<endl;
    25                 m[tmp]++;
    26         }
    27         //cout<<"********************"<<endl;
    28     }
    29     map<string,int>::iterator it;
    30     it=m.begin();
    31     while(it!=m.end()){
    32             if(it->second>1){
    33                cout<<it->first<<" "<<it->second<<endl;
    34             }
    35         it++;
    36     }
    37 
    38     return 0;
    39 }
    View Code





      
      





  • 相关阅读:
    【2017-4-26】Winform 公共控件 菜单和工具栏
    【2017-4-24】Winform 简单了解 窗口属性
    【2017-4-21】ADO.NET 数据库操作类
    【2017-4-21】ADO.NET 防止字符串注入攻击
    【2017-4-19】ADO.NET 数据库链接,基础增删改
    Vue#条件渲染
    Vue#Class 与 Style 绑定
    Vue#计算属性
    Vue入门笔记#数据绑定语法
    Vue入门笔记#过渡
  • 原文地址:https://www.cnblogs.com/z-712/p/10465559.html
Copyright © 2011-2022 走看看