# OpenJudge计算概论-排队游戏【这个用到了栈的思想】

+关注继续查看
/*========================================================================

((()(())())(()))

2 3
5 6
4 7
8 9
1 10
12 13
11 14
0 15

aaabbbbbbbaaaa

==========================================================================*/
#include<stdio.h>
struct stu
{
int number;
char xingBie;
};
int main()
{
char boy,ch;
int end=0,num=0;
struct stu a[55];
freopen("6.in","r",stdin);
scanf("%c",&boy);
a[0].xingBie=boy;
a[0].number=num;
while(scanf("%c",&ch)!=EOF)
{
if(ch==boy)
{
end++;
num++;
a[end].xingBie=ch;
a[end].number=num;

}
else
{
num++;
printf("%d %d\n",a[end].number,num);
end--;
if(end<0)
return 0;
}
}
return 0;
}

递归的代码：

 1 #include<stdio.h>
2 #include<string.h>
3
4 char str[110];
5 int len;
6 char boy;
7
8 int fun(int nowIndex);
9 int main(int argc, char *argv[])
10 {
11     freopen("data.in","r",stdin);
12     scanf("%s",str);
13
14     boy=str[0];
15     len=strlen(str);
16     fun(0);
17     return 0;
18 }
19 int fun(int nowIndex)
20 {
21     int girlIndex;
22     if(nowIndex!=len)
23     {
24         if(str[nowIndex]==boy)
25         {
26             girlIndex=fun(nowIndex+1);
27             if(girlIndex!=-1)
28             {
29                 printf("%d %d\n",nowIndex,girlIndex);
30                 return fun(girlIndex+1);//这一行的参数非常重要，一定要从girlIndex下一个位置继续扫描
31             }
32         }
33         else return nowIndex;//遇到的是代表小女孩的字符，返回其下标
34     }
35     return -1;
36 }

 1 #include <iostream>
2 using namespace std;
3 char man;
4 //参数;char str[],字符串
5 //int s,开始位置
6 //返回值：返回本层的右括号位置
7 int founction(char str[], int s)
8 {
9     int n;
10     if (str[s] != man)
11     {
12         return s;
13     }
14     else
15     {
16         n = founction(str, s + 1);
17         cout << s << ' ' << n << endl;
18         return founction(str, n + 1);
19     }
20 }
21 int main()
22 {
23     char str[101] = { 0 };
24     cin >> str;
25     man = str[0];
26     int n = founction(str, 1);
27     cout << "0 " << n << endl;
28     return 0;
29 }

28306 0

4259 0
Java数组排序基础算法,二维数组，排序时间计算，随机数产生
import java.util.Arrays; //包含Arrays import java.util.Random; public class HelloWorld { public static void main(String[] args){ // Scanner s = new Scanner(System.
910 0

20281 0

2291 0

3445 0
+关注

543

0

JS零基础入门教程（上册）