1: Bracket Matching problem
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
Char s[110];
int len,d[110],t;
BOOL p[110];
int main ()
{while
((scanf ("%s", s+1) ==1)
{
printf ("%s\n", s+1);
memset (P,0,sizeof (P));
Len=strlen (s+1); t=0;
for (int i=1;i<=len;++i)
if (s[i]== ' (')
{
d[++t]=i; p[i]=1;
}
else
if (s[i]== ') ')
if (t) p[d[t--]]=0;
else p[i]=1;
for (int i=1;i<=len;++i)
if (P[i])
if (s[i]== ' (') printf ("$");
else printf ("?");
else printf ("");
printf ("\ n");
}
return 0;
}
2: Output binary complement
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
Char s[50];
int Num[40],tot;
Long long N;
int main ()
{
//freopen ("Int.txt", "R", stdin);
Freopen ("My.txt", "w", stdout);
int i;
while (1)
{
scanf ("%s", s);
if (s[0]<48| | s[0]>57) &&s[0]!= '-') break;
int Len=strlen (s);
memset (num,0,sizeof (num)); tot=0;
if (s[0]!= '-')
{for
(i=0;i<len;++i) n=n*10+s[i]-48;
while (n) num[++tot]=n%2,n/=2;
for (i=32;i>0;--i) printf ("%d", Num[i]);
printf ("\ n");
}
else
{for
(i=1;i<len;++i) n=n*10+s[i]-48;
while (n) num[++tot]=n%2,n/=2;
Tot=1;
for (i=32;i>0;--i) num[i]^=1;
while (Num[tot]) num[tot++]=0;
Num[tot]=1;
for (i=32;i>0;--i) printf ("%d", Num[i]);
printf ("\ n");}
}
return 0;
}
3: Hanoi Tower problem
#include <cstdio>
using namespace std;
Char ch[5];
int n;
void Dfs (int t,int a,int b,int c)
{
if (t==1)
{
printf ("%c->1->%c\n", Ch[a],ch[c]);
return;
}
DFS (T-1,A,C,B);
printf ("%c->%d->%c\n", Ch[a],t,ch[c]);
DFS (T-1,B,A,C);
}
int main ()
{
scanf ("%d%c%c%c", &n,&ch[1],&ch[2],&ch[3]);
DFS (n,1,3,2);
return 0;
}
4: Influenza infection
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
Char ch[110][110];
int d1[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
int d[10010][2],h,t,n,t;
int main ()
{
int i,j;
scanf ("%d", &n);
for (I=1;i<=n;++i)
{
getchar ();
for (j=1;j<=n;++j)
{
scanf ("%c", &ch[i][j]);
if (ch[i][j]== ' @ ')
{d[++t][0]=i; d[t][1]=j;
}}} scanf ("%d", &t); t--;
while (t--)
{
int last=t;
if (h>t) break;
Do
{
int x=d[++h][0],y=d[h][1];
for (j=0;j<4;++j)
{
int xx=x+d1[j][0],yy=y+d1[j][1];
if (xx>0&&xx<=n&&yy>0&&yy<=n&&ch[xx][yy]== '. ')
{
ch[xx][yy]= ' @ ';
D[++t][0]=xx; D[t][1]=yy;}}}
while (h<last);
}
printf ("%d\n", t);
return 0;
}