C language version
Question 1
#include <stdio.h>
int main(void)
{
char op,n1,n2;
int res;
n1=getchar();
op=getchar();
n2=getchar();
switch(op)
{
case '+':
res=(n1-'0')+(n2-'0');
break;
case '-':
res=(n1-'0')-(n2-'0');
break;
case '*':
res=(n1-'0')*(n2-'0');
break;
case '/':
res=(n1-'0')/(n2-'0');
break;
default:
break;
}
printf("%d\n",res);
return 0;
}
Question 2:
#include <stdio.h>
#include <math.h>
void Fractal(int n,int level)
{
int p=pow(3,n);
if(level==1)
{
printf("FD 1/%d\n",p);
printf("LD 60\n");
printf("FD 1/%d\n",p);
printf("LD 240\n");
printf("FD 1/%d\n",p);
printf("LD 60\n");
printf("FD 1/%d\n",p);
}
else
{
Fractal(n,level-1);
printf("LD 60\n");
Fractal(n,level-1);
printf("LD 240\n");
Fractal(n,level-1);
printf("LD 60\n");
Fractal(n,level-1);
}
}
void output(int n)
{
Fractal(n,n);
}
int main(void)
{
int n;
scanf("%d",&n);
output(n);
return 0;
}
Question 3:
#include <stdio.h>
#include <string.h>
#define MAXDIGIT 57
#define MAXN 200007
typedef struct numNode ElemType;
struct numNode
{
int isPositive;
int digit;
char value[MAXDIGIT];
};
ElemType a[MAXN];
int myCompare(ElemType a,ElemType b);
void sort(ElemType *a,int N);
int main(void)
{
int i,N;
scanf("%d",&N);
for(i=0;i<N;i++)
{
scanf("%s",a[i].value);
a[i].digit=strlen(a[i].value);
if(a[i].value[0]=='-')
a[i].isPositive=0;
else
a[i].isPositive=1;
}
sort(a,N);
for(i=0;i<N;i++)
printf("%s ",a[i].value);
printf("\n");
return 0;
}
int myCompare(ElemType a,ElemType b)
{
if(a.isPositive && b.isPositive == 1){
//both of a and b are positive
if(a.digit!=b.digit)
return a.digit<b.digit;
else
return strcmp(a.value,b.value)<0;
}
else if(a.isPositive==0 && b.isPositive==0){
if(a.digit!=b.digit)
return a.digit>b.digit;
else
return strcmp(a.value+1,b.value+1)>0;
}
else
return a.isPositive==0;
}
void QuickSort(ElemType *a,int lwbd,int upbd)
{
int i=lwbd,j=upbd;
if(i<j){
ElemType tmp=a[i];
while(i<j)
{
while(i<j && myCompare(tmp,a[j]))
j--;
if(i<j)
a[i++]=a[j];
while(i<j && myCompare(a[i],tmp))
i++;
if(i<j)
a[j--]=a[i];
}
a[i]=tmp;
QuickSort(a,lwbd,i-1);
QuickSort(a,i+1,upbd);
}
}
void sort(ElemType *a,int N)
{
QuickSort(a,0,N-1);
}
Question 5:
#include <stdio.h>
#define DEBUG 1
#define MAXN 100
// if MAXN reach the upperbound 2e+5, it must beyond the memory limit
int N;
/*
** N is the order of matrix
**
** all of the function will use it
**
** therefore, we defined N as globle variable
*/
int valid(int x,int y)
{
if(x>=0 && x<N & y>=0 && y<N)
return 1;
else
return 0;
}
typedef long long resType;
resType accumulate(resType *a)
{
int i;
resType s=0;
for (i=0;i<N;i++)
s+=a[i];
return s;
}
int main(void)
{
int i,k,x,y;
resType a[MAXN][MAXN];
for(x=0;x<MAXN;x++)
for(y=0;y<MAXN;y++)
a[x][y]=0;
scanf("%d",&N);
int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
a[0][0]=1;
k=0;x=0;y=0;
for(i=2;i<=N*N;i++)
{
while(!(valid(x+dir[k][0],y+dir[k][1]) && a[x+dir[k][0]][y+dir[k][1]]==0))
k=(k+1)%4;
x=x+dir[k][0];
y=y+dir[k][1];
a[x][y]=i;
}
#if DEBUG
// output the matrix
for(x=0;x<N;x++){
for(y=0;y<N;y++)
printf("%4d",a[x][y]);
printf("\n");
}
#endif
for(i=0;i<N;i++)
printf("%d\n",accumulate(a[i]));
return 0;
}