T2000

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <stdio.h>

int main()
{
char ch[3], temp;
int i, j;
while((scanf("%s", ch)) != EOF)
{
for(j = 0; j < 2; j++)
{
for(i = 0; i < 2 - j; i++)
{
if(ch[i] > ch[i + 1])
temp = ch[i];
ch[i] = ch[i + 1];
ch[i + 1] = temp;
}
}
}
for(i = 0; i < 3; i++)
{
printf("%c", ch[i]);
if(ch[i + 1] != '\0')
printf(" ");
else printf("\n");
}
}
return 0;
}

T2001

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <stdio.h>
#include <math.h> //数学库,使用sqrt()
#define LEN 20

int main()
{
double set[4];
double d[LEN];
int i = 0, j;
while((scanf("%lf %lf %lf %lf", &set[0], &set[1], &set[2], &set[3])) == 4)
{
d[i] = sqrt((set[2] - set[0])*(set[2] - set[0])+ (set[3] - set[1])*(set[3] - set[1]));
i++;
}
j = i;
for(i = 0; i < j; i++)
{
printf("%.2lf\n", d[i]);
}
return 0;
}

T2002

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdio.h>
#define PI 3.1415927
#define LEN 20

int main()
{
double r;
double s[LEN];
int i = 0, j;
const double k = 4.00/3.00;
while((scanf("%lf", &r)) == 1)
{
s[i] = k*PI*r*r*r;
i++;
}
j = i;
for(i = 0; i < j; i++)
{
printf("%.3lf\n", s[i]);
}
return 0;
}

T2003

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <stdio.h>
#define LEN 20

int main()
{
double num, abs[LEN];
int i = 0, j;
while((scanf("%lf", &num)) == 1)
{
if(num >= 0)
abs[i] = num;
else abs[i] = -num;
i++;
}
j = i;
for(i = 0; i < j; i++)
{
printf("%.2lf\n", abs[i]);
}
return 0;
}

T2004

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <stdio.h>
#define F "Score is error!"
#define LEN 20

int main()
{
int score, i = 0, j = 0, temp, false[LEN];
char rank[LEN];
while((scanf("%d", &score)) == 1)
{
if(score >= 90 && score <= 100)
rank[i] = 'A';
else if(score >= 80 && score <= 89)
rank[i] = 'B';
else if(score >= 70 && score <= 79)
rank[i] = 'C';
else if(score >= 60 && score <= 69)
rank[i] = 'D';
else if(score >= 0 && score <= 59)
rank[i] = 'E';
else {
false[j] = i;
j++; //false[]记录错误值
}
i++;
}
temp = i;
for(i = 0, j = 0; i < temp; i++)
{
if( i != false[j])
printf("%c\n", rank[i]);
else{
printf("%s\n", F);
j++;
}
}
return 0;
}

T2005

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include<stdio.h>
int main() {
int day[12] = {31,0,31,30,31,30,31,31,30,31,30,31};
int year, month, date;
int total = 0;

while(scanf("%d/%d/%d", &year, &month, &date) != EOF) {
if((year % 400 == 0) || ((year % 100 != 0) && (year % 4 == 0))) {
day[1] = 29;
} else {
day[1] = 28;
}//输入技巧:scanf中包含'/',不用字符串读取,避免类型转换

int i = 0; //月份计数
while(i < month - 1) {
total += day[i];
i++;
}

total += date;
printf("%d\n",total);
total = 0;
}
return 0;

}

T2006

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <stdio.h>
#define ROW 20

int main()
{
int n, num, i, j = 0;
int mul[ROW];
while((scanf("%d", &n)) != EOF )
{
mul[j] = 1;
for(i = 0; i < n; i++)
{
scanf("%d", &num);
if(num % 2 == 1)
mul[j] *= num;
}
j++;
}
i = j;
for(j = 0; j < i; j++)
{
printf("%d\n", mul[j]);
}
return 0;

}

T2007

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <stdio.h>
#define LEN 20

int main()
{
int min, max, temp;
int i, j = 0;
long long int sum1[LEN], sum2[LEN];
while((scanf("%d %d", &min, &max)) != EOF)
{
sum1[j] = sum2[j] = 0;
if(min > max)//一定要记得大小比较!!!
{
temp = min;
min = max;
max = temp;
}
for(i = min; i <= max; i++)
{
if(i % 2 == 1)
sum1[j] += i * i * i;
else if(i % 2 == 0)
sum2[j] += i * i;
}
j++;
}
for(i = 0; i < j; i++)
{
printf("%lld %lld\n", sum2[i], sum1[i]);
}
return 0;
}

T2008

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <stdio.h>
#define LEN 20

int main()
{
int n, i, j = 0;
double a[LEN];
int c1[LEN] = {0}, c2[LEN] = {0}, c3[LEN] = {0};
while(1)
{
scanf("%d", &n);
if(n == 0)
break;
for(i = 0; i < n; i++){
scanf("%lf", &a[i]);
if(a[i] == 0)
c1[j]++;
if(a[i] < 0)
c2[j]++;
else if(a[i] > 0)
c3[j]++;
}
j++;
}
for(i = 0; i < j; i++){
printf("%d %d %d\n", c2[i], c1[i], c3[i]);
}
return 0;
}

T2009

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <stdio.h>
#include <math.h>
#define LEN 20

int main()
{
double sum[LEN] = {0};
double n;
int m;
int i, j = 0;
while((scanf("%lf %d", &n, &m)) != EOF)
{
for(i = 0; i < m; i++)
{
sum[j] += n;
n = sqrt(n);
}
j++;
}
for(i = 0; i < j; i++)
printf("%.2lf\n", sum[i]);
return 0;
}

T2010

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <stdio.h>
#include <stdlib.h>

int main()
{
int m, n, temp;
int i, g, s, b;
int count;
while((scanf("%d %d", &m, &n)) != EOF)
{
count = 0; //每次清零计数器
if(m > n){
temp = m;
m = n;
n = temp;
}
for(i = m; i <= n; i++)
{
g = i % 10;
s = (i / 10) % 10;
b = i / 100; //计算个、十、百位
if(i == b*b*b + s*s*s + g*g*g){
if(count >= 1)
printf(" ");
count++;
printf("%d", i);
}
}
if(count == 0)
printf("no\n");
else printf("\n");
}

return 0;

}

T2011

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<stdio.h>
#define LEN 100

int main()
{
int m, n[LEN], i, j, k = 0;
double result[LEN] = {0};
scanf("%d", &m);
for(i = 0; i < m; i++)
{
scanf("%d", &n[i]);
for(j = 1; j <= n[i]; j++){
if(j % 2 == 1)
result[k] += (double)(1.00/j);
else result[k] -= (double)(1.00/j);
}
k++;
}
j = k;
for(i = 0; i < j; i++)
printf("%.2lf\n", result[i]);
return 0;
}

T2012

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include<stdio.h>

int main()
{
int n, m, j, x, y, t;
while(scanf("%d %d", &x, &y) != EOF)
{
if(x == 0 && y == 0)
break;
else{
for(n = x; n <= y; n++){
m = n * n + n + 41;
for(j = 2; j < m; j++){ //判断素数
if(m % j != 0)
t = 1;
else{
t = 0;
break;
}
}
if(t == 0){
printf("Sorry\n");
break;
}
else
continue;
}
if(t != 0)
printf("OK\n");
}
}
return 0;
}

T2013

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <stdio.h>

int main()
{
int n, i;
int sum;
while((scanf("%d", &n)) != EOF)
{
sum = 1;
for(i = 1; i < n; i++)
{
sum = (sum + 1) * 2;
}
printf("%d\n", sum);
}
return 0;

}

T2014

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <stdio.h>
int main ()
{
int n,i,j;
float a[100],x;
while(scanf("%d",&n)!=EOF)
{
for(i=0;i<n;i++)
scanf("%f",&a[i]);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
x=a[i],a[i]=a[j],a[j]=x;
}
}
x=0;
for(i=1;i<n-1;i++)
{
x=x+a[i];
}
x=x/(n-2);
printf("%.2f\n",x);
}
return 0;
}

T2015

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <stdio.h>

int main()
{
int n, m, i;
int sum;
int av;
while((scanf("%d %d", &n, &m)) != EOF)
{
i = 0;
sum = 0;
for(i = 2; i <= 2 * n; i = i + 2)
{
sum += i;
if(((i / 2) % m) == 0)
{
av = sum / m;
sum = 0;
printf("%d", av);
if(n % m != 0)
printf(" "); //处理输出的形式,这里容易presentation error
if(n % m == 0)
{
if(i != 2 * n)
printf(" ");
else printf("\n"); //处理完后光标跳至下一行等待输入
}
}

}
sum = 0;
av = 0;
if(n % m != 0) //对最后不足m项的处理
{
for(i = 2 * (n - (n % m)) + 2; i <= 2 * n; i = i + 2)
{
sum += i;
}
printf("%d\n", sum/(n % m));
}
}
return 0;

}

T2016

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include<stdio.h>

int main()
{
int n;
while((scanf("%d", &n)) != EOF)
{
if(n == 0) //结束条件
break;
int m[100];
int min = 0;
for(int i = 0; i < n; i++)
{
scanf("%d", &m[i]);
if(m[min] > m[i]) //记录下标,方便直接交换
min = i;
}
int temp;
temp = m[min];
m[min] = m[0];
m[0] = temp;
for(int i = 0; i < n - 1; i++)
printf("%d ", m[i]); //防止presentation error
printf("%d\n", m[n - 1]);
}
return 0;
}

T2017

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <stdio.h>
#include <string.h> //使用strlen()

int main()
{
char ch[100];
int n;
int count;
scanf("%d", &n);
for(int i = 1; i <= n; i++)
{
count = 0;
scanf("%s", ch);
for(int j = 0; j < strlen(ch); j++)
{
if(ch[j] <= '9' && ch[j] >= '0') //数字对应ASCII码编号是自身,但要带''
count++;
}
printf("%d\n", count);
}
return 0;

}

T2018

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <stdio.h>

int fbnq4(int n); //声明函数,仿斐波那契

int main()
{
int n;
int count;
while((scanf("%d", &n)) != EOF) //主函数作为fbnq4()的驱动函数
{
if(n == 0)
break;
count = fbnq4(n);
printf("%d\n", count);
}
return 0;
}

int fbnq4(int n)
{
if(n <= 4)
return n;
else return fbnq4(n - 1) + fbnq4(n - 3); //仿斐波那契递归
}

T2019

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <stdio.h>

int main()
{
int a[100];
int n, m;
while((scanf("%d %d", &n, &m)) != EOF)
{
if(n == 0 && m == 0)
break;
for(int i = 0; i < n; i++){
scanf("%d", &a[i]);
}
for(int i = 0; i < n; i++)
{
if(a[i] < m)
continue;
else if(a[i] > m){
for(int j = n - 1; j >= i; j--) //每个值后移,同时数组扩大
a[j + 1] = a[j];
a[i] = m;
for(int i = 0; i < n + 1; i++){
if(i != n)
printf("%d ", a[i]);
else printf("%d\n", a[n]);
}
break; //直接break外层循环
}
}
}
return 0;
}

T2020

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <stdio.h>
#include <stdlib.h>

int main()
{
int n, a[100];
while(scanf("%d", &n) != EOF)
{
if(n == 0) break;

for(int i = 0;i < n; i++)
{
scanf("%d", &a[i]);
}

for(int i = 0; i < n-1; i++)
for(int j = 0; j < n-i-1; j++)
{
if(abs(a[j]) < abs(a[j+1]))
{
int temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}

for(int i = 0; i < n; i++)
{
if(i == n-1) {
printf("%d\n", a[i]);
break;
}
printf("%d ", a[i]);
}
}

return 0;
}

T2021

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include<stdio.h>
//典型贪心算法,从最大面额开始(话说给的示例工资也太少吧喂!
int count(int n){
int sum = 0;
int a[6] = {100,50,10,5,2,1}; //人民币面值种类
for(int i = 0; i<6; i++){
sum += n/a[i]; //人民币张数计算
n = n%a[i];
}
return sum;
}
int main()
{
int i,j,n;
int a[100];
while(scanf("%d",&n) != EOF) {
int k = 0; //更新k的值
if(n == 0)return 0;
for(i = 0; i < n; i++){
scanf("%d", &a[i]);
}
for(j = 0; j < n; j++){
k += count(a[j]);
}
printf("%d\n", k);
}
return 0;

}

T2022

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <stdio.h>
#include <stdlib.h>

//题干太长了吧!!!
int main()
{
int m, n;
int point[100][100]; //二维数组自带行与列
while((scanf("%d %d", &m, &n)) != EOF)
{
int max = 0, x = 0, y = 0;
for(int i = 0; i < m; i++) //两层循环处理输入
{
for(int j = 0; j < n; j++){
scanf("%d", &point[i][j]);
if(abs(max) < abs(point[i][j])){
max = point[i][j];
x = i + 1; //记录行
y = j + 1; //记录列
}
}
}
printf("%d %d %d\n", x, y, max);
}
}

T2023

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <stdio.h>
#include <stdlib.h>

int main()
{
int n, m;
int a[50][5];
int count;
int is_big = 0;
while((scanf("%d %d", &n, &m)) != EOF)
{
int i ,j, tot;
double b[5] = {0}; //有必要记录下每科的平均分
count = 0;
for(i = 0; i < n; i++)
{
for(j = 0; j < m; j++)
scanf("%d", &a[i][j]);
}
for(i = 0; i < n; i++) //第一行输出
{
tot = 0;
for(j = 0; j < m; j++)
{
tot += a[i][j];
}
if(i == n - 1)
printf("%.2lf\n", (double)tot/(double)m);
else printf("%.2lf ", (double)tot/(double)m); //被presentation error整怕了
}
for(j = 0; j < m; j++)
{
tot = 0;
for(i = 0; i < n; i++)
{
tot += a[i][j];
}
b[j] = (double)tot/(double)n; //需要记录的b的数量与科目数相同
if(j == m - 1)
printf("%.2lf\n", b[j]);
else printf("%.2lf ", b[j]);
}
for(i = 0; i < n; i++)
{
for(j = 0; j < m; j++)
{
if(a[i][j] >= b[j])
is_big = 1; //引入is_big判断
else is_big = 0;
}
if(is_big)
count++;
}
printf("%d\n", count);
printf("\n");
}
return 0;
}

T2024

好家伙,帮我复习了一下用的不多的两个函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include<stdio.h>
#include<ctype.h>

int main()
{
int a, b, i;
char ch[50];
while (scanf("%d", &a) != EOF)
{
getchar();
while (a--)
{
is_legal = 0;
gets(ch);
if (ch[0] != '_' && isalpha(ch[0]) == 0)
{
printf("no\n");
continue;
}
for (i = 1; ch[i] != '\0'; i++)
{
if (ch[i] != '_' && isalpha(ch[i]) == 0 && isalnum(ch[i]) == 0)
{
printf("no\n");
is_legal = 1;
break;
}
}
if (is_legal != 1)
printf("yes\n");
}
}
return 0;
}

T2025

本来用strchr()写了一个版本:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
char ch[100];
while(gets(ch))
{
char max = ch[0];
int idx[100];
for(int i = 1; i < strlen(ch); i++)
{
if((int)max < (int)ch[i])
max = ch[i];
}
char *p = strchr(ch, max);
int i = 0;
while(p != NULL)
{
idx[i] = p - ch;
p = strchr(p + 1, max);
i++;
}
int j;
for(i = 0, j = 0; i < strlen(ch); i++)
{
if(i != idx[j])
printf("%c", ch[i]);
else{
printf("%c(max)", ch[i]);
j++;
}
}
printf("\n");
}
return 0;
}

yysy,我把所有可能的输入都试遍了,都没有问题,但就是过不了oj,没办法写了一般版本:(不过此题用字符串函数是杀鸡焉用牛刀)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include<stdio.h>

int main(void)
{
char a[101];
int i, j, k;
while (gets(a))
{
j = 0;
k = 0;
for (i = 0; a[i] != '\0'; i++) //两种标记循环结束的方法
{
if (a[i] > j)
{
j = a[i];
k = i;
}
}
for (i = 0; a[i] != '\0'; i++)
{
if (a[i] == a[k])
printf("%c(max)", a[i]);
else
printf("%c", a[i]);
}
printf("\n");
}
return 0;
}

T2026

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
char sen[100];
while(gets(sen))
{
int i;
sen[0] -= 32;
for(i = 0; i < strlen(sen); i++)
{
if(sen[i] == ' ')
sen[i+1] -= 32;
}
puts(sen);
}
return 0;
}
//也可以用另一种解法,即include一个ctype.h使用toupper()直接转换大写

T2027

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
int n, j = 0;
char s[100];
scanf("%d", &n);
getchar(); //!一定要有,不然'\n'会被gets吃到
while(++j <= n)
{
gets(s);
int c1 = 0, c2 = 0, c3 = 0, c4 = 0, c5 = 0;
for(int i = 0; i < strlen(s); i++)
{
if(s[i] == 'a')
c1++;
if(s[i] == 'e')
c2++;
if(s[i] == 'u')
c3++;
if(s[i] == 'i')
c4++;
else if(s[i] == 'o')
c5++;
}
if(j <= n - 1)
printf("a:%d\ne:%d\ni:%d\no:%d\nu:%d\n\n", c1, c2, c4, c5, c3);
if(j == n)
printf("a:%d\ne:%d\ni:%d\no:%d\nu:%d\n", c1, c2, c4, c5, c3);
}
return 0;
}
//注意PE!搞心态orz
//当然switch更简洁

T2028

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <stdio.h>
#include <stdlib.h>

int main()
{
int n, is_times = 0;
while((scanf("%d", &n)) != EOF)
{
int a[100];
for(int i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
int j = 1;
while(j++) //枚举法找最大公倍数,时间复杂度为o(n)
{
for(int i = 0; i < n; i++)
{
if(j%a[i] == 0)
is_times = 1;
else {
is_times = 0;
break;
}
}
if(is_times){
printf("%d\n", j);
break;
}
}
}
return 0;
}

T2029

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
int n;
char s[100];
scanf("%d", &n);
getchar();
int count = 0, is_huiwen = 0;
while(count++ < n && gets(s))
{
for(int i = 0, j = strlen(s) - 1; i < strlen(s) / 2 ; i++, j--)
{
if(s[i] == s[j])
is_huiwen = 1;
else {
is_huiwen = 0;
break;
}
}
if(is_huiwen == 1)
printf("yes\n");
else if(is_huiwen == 0)
printf("no\n");
}
return 0;

}

T2030

**汉字机内码

//在做这题之前我也曾想过计算机如何储存汉字,但没有深究过,感谢2030题给我这个机会🌹🌹

GBK编码中,汉字占两个字节(两个char类型),且两个char类型对应编码由十六进制转换为十进制均为负数,若想用printf打印出汉字,必需要两个%c相连,否则就不知道会打印什么东西了。如:

1
2
3
4
5
6
7
8
9
10
11
char *str = "你好,世界!"
char *chr = str;
while(*chr != '\0')
{
if(*chr < 0)
{
printf("%c%c", *chr, *(chr+1));
chr += 2;
}
}
......

那么此题思路就很简单了:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdio.h>
#include <string.h>

int main()
{
int n;
scanf("%d", &n);
getchar();
char ch[10000];
while(n--)
{
gets(ch);
int count = 0;
for(int i = 0; i < strlen(ch); i++)
{
if(ch[i] < 0)
count++;
}
printf("%d\n", count / 2); //根据两个字节对应一个汉字的特性
}
return 0;
}

T2031

复习一下十进制转R进制原理:

十进制数除以R取余并记录余数,最后将每步得到的余数倒置即可。

T2032

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <stdio.h>
#include <stdlib.h>

int main()
{
int n;
int tri[30][30];

while((scanf("%d", &n)) != EOF)
{
tri[0][0] = 1;
tri[0][1] = 0;
for(int i = 0; i < n; i++)
{
for(int j = 0; j <= i; j++)
{
if(j == 0 && i != 0)
{
tri[i][j] = 1;
printf("%d ", tri[i][j]);
}
if(j == 0 && i == 0)
printf("%d", tri[i][j]);
if(j == i && i != 0)
{
tri[i][j] = 1;
printf("%d", tri[i][j]);
}
else if(j != 0 && j != i){
tri[i][j] = tri[i-1][j] + tri[i-1][j-1] ;
printf("%d ", tri[i][j]);
}
}
printf("\n");
}
printf("\n");
}

return 0;
}

调PR调了半天/恼,每行后面不让多出来一个空格orz

后来发现其实可以在给每行末尾添加一个0值、

网上的一个没有用二维数组的版本:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <stdio.h>
#include <string.h>
int main()
{
int i, j, n;
int yh[32];

while(scanf("%d",&n) != EOF)
{
memset(yh, 0, sizeof(yh));//初始化
yh[0]=1;
for(i = 0; i < n; i++)
{
printf("%d", 1);
for(j = i; j; j--)
yh[j] += yh[j-1];
for(j = 1; j <= i; j++)
printf(" %d", yh[j]);//注意空格
putchar('\n');
}
putchar('\n');
}
return 0;
}

相当于每次输出一行后,该一维数组的值即为该行,然后每个位置自身加上前面一个数即可得到新行。

T2033

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <stdio.h>
#include <string.h>

int main()
{
int n;
scanf("%d", &n);
int ah, am, as, bh, bm, bs, rh, rm, rs;
int flag = 0; //是否进位的标记,可以共用
while(n--)
{
scanf("%d %d %d %d %d %d", &ah, &am, &as, &bh, &bm, &bs);
if((bs + as) < 60){
rs = bs + as;
flag = 0;
}
else {
rs = bs + as - 60;
flag = 1;
}
if((bm + am) < 60 && flag == 0){
rm = bm + am;
flag = 0;
}
if((bm + am) < 60 && flag == 1){
rm = bm + am + 1;
flag = 0;
}
if((bm + am) > 60 && flag == 0){
rm = bm + am - 60;
flag = 1;
}
if((bm + am) > 60 && flag == 1){
rm = bm + am - 60 + 1;
flag = 1;
}
if(flag)
rh = ah + bh + 1;
else rh = ah + bh;
printf("%d %d %d\n", rh, rm, rs);
}
return 0;

}