# Problem A

Sample Input 1:

1+1

Sample Output 1:

2

Sample Input 2:

3*4

Sample OutPut2:

12

## C语言解答

#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;
}


## Python解答

expr=input()
print(int(eval(expr)))


# Problem B

1行，1个数字n，表示图形的阶数$\left(0$(0

## C语言解答

#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;
}

# Problem C

2行，第1行有1个数字N，表示数列中数据的个数

1行，将N个数字从小到大排序后的结果，数字间用空格分隔。
Sample Input :

5
991 -31 -1 5 -10000000000000000000000000000000000000000000000000


Sample Output:

-10000000000000000000000000000000000000000000000000 -31 -1 5 991


## C语言解答

#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);
}


## Cpp解答：用到STL库与运算符重载

#include <cstdio>
#include <cstring>
#include <algorithm>
using std::sort;
const int MAXDIGIT=50;

struct numNode{
bool isPositive;
int digit;
char value[MAXDIGIT+2];

bool operator<(const numNode &b) const
{
if(isPositive==true &&b.isPositive==true)
{
if(digit!=b.digit)
return digit<b.digit;
else
return strcmp(value,b.value)<0;
}
else if( isPositive== false && b.isPositive==false)
{
if(digit!=b.digit)
return digit>b.digit;
else
return strcmp(value,b.value)>0;
}
else
return b.isPositive;
}
};

int main(void)
{
int i,N;
scanf("%d",&N);
numNode *s=new numNode [N];
for(i=0;i<N;i++)
{
scanf("%s",s[i].value);
s[i].digit=strlen(s[i].value);
if(s[i].value[0]=='-')
s[i].isPositive=false;
else
s[i].isPositive=true;
}

sort(s,s+N);

for (i=0;i<N;i++)
printf("%s ",s[i].value);
printf("\n");
delete [] s;
return 0;

}



# Problem D

2行，第1行有1个数字N$\left(0$(0，代表小组的人数

1行，N个数字，表示第i名成员的团队的规模
Sample Input:

0 1 2 1 2 2

Sample Output:

6 4 1 1 1 1

# Problem E

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

   1   2   3   4   5
16  17  18  19   6
15  24  25  20   7
14  23  22  21   8
13  12  11  10   9

1行，1个数字N $\left(1$(1< N<2×10^5)$

N行，第i行表示蛇矩阵第i行的总和。

## 直接法：列出矩阵进行加和

#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;

}


## 找到数学规律可覆盖所有范围，C++解答

#include <stdio.h>
int main(void)
{
int i,N;
long long spl,currD;
const int deepD=8;
scanf("%d",&N);

spl=N*(N+1)/2;
currD=4*(N-1)-1;
printf("%lld\n",spl);
for(i=N-1;i>0;i-=2)
{
spl+=currD*i+1;
printf("%lld\n",spl);
currD-=deepD;
}
currD=currD+deepD-10;

if(N%2==1)
{
for(i=1;i<N;i+=2)
{
spl+=currD*i;
printf("%lld\n", spl);
currD-=deepD;
}
}
else
{
for(i=2;i<N;i+=2)
{
spl+=currD*i;
printf("%lld\n", spl);
currD-=deepD;
}
}

return 0;
}


## python解答

N=int(input())
spl=int(N*(N+1)/2)  #sum per line
currD=4*(N-1)-1     #current distance
deepD=8             #2_order distance
print(spl)          #sum of the 1st line
for i in range(N-1,0,-2):
spl+=currD*i+1
print(spl)
currD-=deepD
currD=currD+deepD-10

if(N%2==1):
for i in range(1,N,2):
spl+=currD*i
print(spl)
currD-=deepD
else:
for i in range(2,N,2):
spl+=currD*i
print(spl)
currD-=deepD


• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120