計算機歷年考研復試上機基礎題(一)


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\n",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\n",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\n");
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\n",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\n",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\n",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

 






  
  






免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM