mook 同时被 2 个专栏收录
3 篇文章 45 订阅
6 篇文章 4 订阅

PAT 习题集

# 02-0. 整数四则运算(10)

400 ms

65536 kB

8000 B

Standard

3 2


3 + 2 = 5
3 - 2 = 1
3 * 2 = 6
3 / 2 = 1
#include <stdio.h>

int main()
{
int A;
int B;
int X, Y, Z, W;
scanf("%d %d", &A, &B);
X = A + B;
Y = A - B;
Z = A * B;
W = A / B;
printf("%d + %d = %d\n" ,A, B, X);
printf("%d - %d = %d\n", A, B, Y);
printf("%d * %d = %d\n", A, B, Z);
printf("%d / %d = %d\n", A, B, W);

return 0;
} 

# 02-1. 厘米换算英尺英寸(15)

400 ms

65536 kB

8000 B

Standard

170


5 6
#include <stdio.h>

int main()
{
int cm;
scanf("%d", &cm);
int foot = cm / 30.48;
int inch = (cm/30.48 - foot) * 12;
printf("%d %d", foot, inch);
return 0;
} 

# 02-2. 然后是几点(15)

400 ms

65536 kB

8000 B

Standard

1120 110


1310
#include <stdio.h>
int main()
{
int i, j, x = 0, y, z;
scanf("%d%d", &i, &j);
if(i >= 1000)
{
x = i / 1000;
y = (i % 1000) / 100;
z = (i %1000) %100;
}
else
{
y = i / 100;
z = i % 100;
}
int s = (10*x + y) * 60 + z;
int n = s + j;
i = n / 60 *100 + (n - ((n / 60 )* 60));
printf("%d", i);
return 0;
} 

# 02-3. 逆序的三位数(10)

400 ms

65536 kB

8000 B

Standard

123


321

#include <stdio.h>

int main()
{
int i;
scanf("%d", &i);
int a = i % 100 %10;
int b = i / 100;
int c = i %100 / 10;
i = a*100 + b + c*10;
printf("%d", i);
return  0;
} 

# 02-4. BCD解密(10)

400 ms

65536 kB

8000 B

Standard

BCD数是用一个字节来表达两位十进制的数，每四个比特表示一位。所以如果一个BCD数的十六进制是0x12，它表达的就是十进制的12。但是小明没学过BCD，把所有的BCD数都当作二进制数转换成十进制输出了。于是BCD的0x12被输出成了十进制的18了！

18


12
 #include <stdio.h>

int main()
{
int i;
scanf("%d", &i);
int t = i % 16 + i / 16 * 10;
printf("%d", t);
return 0;
} 

# 03-0. 超速判断(10)

400 ms

65536 kB

8000 B

Standard

40


Speed: 40 - OK


75


Speed: 75 - Speeding
#include <stdio.h>

int main()
{
int i;
scanf("%d", &i);
if(i <= 60)
printf("Speed: %d - OK", i);
else
printf("Speed: %d - Speeding", i);
return 0;
} 

# 03-1. 三天打鱼两天晒网(15)

400 ms

65536 kB

8000 B

Standard

103


Fishing in day 103


34


Drying in day 34
#include <stdio.h>

int main()
{
int i;
scanf("%d", &i);
if(i%5 <= 3 && i%5 != 0)
printf("Fishing in day %d", i);
else
printf("Drying in day %d", i);
return 0;
}  

# 03-2. 用天平找小球(10)

400 ms

65536 kB

8000 B

Standard

1 1 2


C

# include <stdio.h>

int main()
{
int A, B, C;
scanf("%d%d%d",&A, &B, &C);
if(A != B && B ==C)
printf("A");
else if(B != A && A == C)
printf("B");
else if(C != A && A == B)
printf("C");
return 0;
} 

# 03-3. 12-24小时制(15)

400 ms

65536 kB

8000 B

Standard

21:11


9:11 PM

#include <stdio.h>

int main()
{
int i, j;
scanf("%d:%d", &i, &j);
if(i >= 0 && i < 12)
printf("%d:%d AM", i, j);
else if(i == 12)
printf("%d:%d PM", i, j);
else if(i == 24)
printf("%d:%d AM", i-24, j);
else
printf("%d:%d PM", i-12, j);

return 0;
} 

# 03-4. 成绩转换(15)

400 ms

65536 kB

8000 B

Standard

• 大于等于90分为A；
• 小于90且大于等于80为B；
• 小于80且大于等于70为C；
• 小于70且大于等于60为D；
• 小于60为E。

输入格式：

输入在一行中给出1个整数的百分制成绩。

输出格式：

在一行中输出对应的五分制成绩。

输入样例：
90

输出样例：
A
• #include <stdio.h>

int main()
{
int i;
scanf("%d", &i);
if(i >= 90)
printf("A");
else if(i < 90 && i >= 80)
printf("B");
else if(i < 80 && i >= 70)
printf("C");
else if(i < 70 && i >= 60)
printf("D");
else if(i < 60)
printf("E");

return 0;
} 

# 04-0. 求符合给定条件的整数集(15)

400 ms

65536 kB

8000 B

Standard

2


234 235 243 245 253 254
324 325 342 345 352 354
423 425 432 435 452 453
523 524 532 534 542 543
#include <stdio.h>

int main()
{
int A, i, j, k, p = 0;
scanf("%d", &A);
for(i=A; i<A+4; ++i)
for(j=A; j<A+4; ++j)
for(k=A; k<A+4; ++k)
{
if(i != j && i != k && j != k)
{

printf("%d", i*100+10*j+k);
p++;
if(p%6 == 0)
printf("\n");
else
printf(" ");
}
}

return 0;
} 

# 04-1. 水仙花数(20)

2000 ms

65536 kB

8000 B

Standard

3


153
370
371
407
#include <stdio.h>

int main()
{
int n = 0, m = 0, l = 0, k = 0, roll = 0;
int min = 1, t = 0, sum = 0, sum1 = 0;
scanf("%d", &n);
m=n;
while(m > 1)   //built the minimum for n digit
{
min*=10;
m--;
}
t=min;
while(t < 10*min)  //all the number for n digit
{
roll = t;
m = n;
sum = 1;
while(m > 0)
{
sum = sum*(roll%10);
m--;
}
l = n;
while(l > 0)
{
roll = roll / 10;
k = roll % 10;
m = n;
sum1 = 1;
while(m > 0)
{
sum1 = sum1 * k;
m--;
}
sum = sum + sum1;
l--;
}
if(t == sum)
{
printf("%d\n", t);
}
t++;
}
return 0;
} 

# 04-2. 打印九九口诀表(15)

400 ms

65536 kB

8000 B

Standard

1*1=1
1*2=2   2*2=4
1*3=3   2*3=6   3*3=9
1*4=4   2*4=8   3*4=12  4*4=16
1*5=5   2*5=10  3*5=15  4*5=20  5*5=25
1*6=6   2*6=12  3*6=18  4*6=24  5*6=30  6*6=36
1*7=7   2*7=14  3*7=21  4*7=28  5*7=35  6*7=42  7*7=49
1*8=8   2*8=16  3*8=24  4*8=32  5*8=40  6*8=48  7*8=56  8*8=64
1*9=9   2*9=18  3*9=27  4*9=36  5*9=45  6*9=54  7*9=63  8*9=72  9*9=81


4


1*1=1
1*2=2   2*2=4
1*3=3   2*3=6   3*3=9
1*4=4   2*4=8   3*4=12  4*4=16
#include <stdio.h>

int main(void)
{
int n;
scanf("%d", &n);
int i, j;
for (i=1; i<=n; i++)
{
for (j=1; j<=i; j++)
printf("%d*%d=%-4d", j, i, i*j);
puts("");
}

return 0;
} 

# 04-3. 统计素数并求和(20)

400 ms

65536 kB

8000 B

Standard

10 31


7 143

#include <stdio.h>
#include <stdbool.h>
int nums(int x)
{
int i;
if(x ==1 ){
return 0;
}
for(i=2; i*i<=x; i++){
if(x%i == 0){
return 0;
}
}
return 1;
}
int main()
{
int m,n;
int j;
scanf("%d %d", &m, &n);
int count = 0;
int sum = 0;
for(j=m; j<=n; j++)
{
if(nums(j) == 1)
{
count++;
sum += j;
}
}
printf("%d %d", count, sum);

return 0;
}
 

# 04-4. 猜数字游戏(15)

400 ms

65536 kB

8000 B

Standard

58 4
70
50
56
58
60
-2


Too big
Too small
Too small
Good Guess!

 #include <stdio.h>

int main()
{
int n, times;
int i, t = 0;
scanf("%d %d", &n, ×);
while(1)
{
scanf("%d", &i);
++t;
if(i == n && t == 1)
{
printf("Bingo!\n");
break;
}
else if(i < 0 || t > times )
{
printf("Game Over");
break;
}
else if(i == n && t > 1 && t <= 3 && t <= times)
{
printf("Lucky You!\n");
break;
}
else if(i > n)
{
printf("Too big\n");
}
else if(i < n && i > 0)
{
printf("Too small\n");
}
else if(i == n && t > 3 && t <= times)
{
printf("Good Guess!\n");
break;
}
}

return 0;
}
 

# 05-0. 求序列前N项和(15)

400 ms

65536 kB

8000 B

Standard

20


32.66
#include <stdio.h>

int main()
{
int n = 1, i;
double a = 2, b = 1, t, s = 0;
scanf("%d", &n);
for(i=1; i<=n; ++i)
{
t = a;
s += a/b;
a += b;
b = t;
}
printf("%.2lf", s);

return 0;
} 

# 05-1. 约分最简分式(15)

400 ms

65536 kB

8000 B

Standard

60/120


1/2
#include <stdio.h>

int main()
{
int i, j, n, k;
scanf("%d/%d", &i, &j);
loop:
if(i >= j)
n = j;
else
n = i;
for(k=2; k<=n; ++k)
{
if(j % k == 0 && i % k == 0)
{
j /= k;
i /= k;
goto loop;
}
}
printf("%d/%d", i, j);

return 0;
} 

# 05-2. 念数字(15)

400 ms

65536 kB

8000 B

Standard

0: ling
1: yi
2: er
3: san
4: si
5: wu
6: liu
7: qi
8: ba
9: jiu


-600


fu liu ling ling

#include <stdio.h>
#include <string.h>
char str[20];
const char *num[] = {"ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"};

int main()
{
int i, k;
scanf("%s", str);
k = strlen(str);
if ( str[0] == '-') printf("fu");
else
printf("%s", num[str[0]-'0']);
for ( i=1; i<k; i++ )
{
printf(" %s", num[str[i]-'0']);
}
printf("\n");

return 0;
} 

# 05-3. 求a的连续和(15)

400 ms

65536 kB

8000 B

Standard

2 4


2468

#include <stdio.h>
#include <math.h>

int main()
{
int a, n, i, s = 0;
scanf("%d%d", &a, &n);
for(i=1; i<=n; ++i)
{
s += (pow(10.0,i)-1) * a / 9;
}
printf("%d", s);

return 0;
} 

# 06-0. 混合类型数据格式化输入(5)

400 ms

65536 kB

8000 B

Standard

2.12 88 c 4.7


c 88 2.12 4.70

#include <stdio.h>

int main()
{
float f1, f2;
char ch;
int i;
scanf("%f %d %c %f", &f1, &i, &ch, &f2);
printf("%c %d %.2f %.2f", ch, i, f1, f2);

return 0;
} 

# 06-1. 简单计算器(20)

400 ms

65536 kB

8000 B

Standard

1+2*10-10/2=


10
#include <stdio.h>

int main()
{
char ch = '0';
int result, i, flag = 0;
scanf("%d", &result);
while( ch != '=' )
{
scanf("%c", &ch);
if(ch == '=')
break;
scanf("%d", &i);
if( ch == '+' )
result += i;
else if( ch == '-' )
result -= i;
else if( ch == '*' )
result *= i;
else if( ch == '/' )
{
if( i != 0 )
result /= i;
else
flag = 1;
}
else
flag = 1;
}
if(flag)
printf("ERROR\n");
else
printf("%d", result);

return 0;
} 

# 06-2. 字符串字母大小写转换(10)

400 ms

65536 kB

8000 B

Standard

Hello World! 123#


hELLO wORLD! 123
#include <stdio.h>

int main()
{
char c;
c = getchar();
while(c != '#')
{
if(c >= 'a' && c <= 'z')
{
c -= 32;
}
else if(c >= 'A' && c <= 'Z')
{
c += 32;
}
printf("%c", c);
c = getchar();
}

return 0;
} 

# 06-3. 单词长度(15)

400 ms

65536 kB

8000 B

Standard

It's great to see you here.


4 5 2 3 3 4
#include <stdio.h>

int main()
{
char s[100];
int i = 0;
do
{
scanf("%c", &s[i]);
i++;
} while(s[i-1] != '.');
int j;
int cnt = 0;
int k = 0;
for(j=0; j<i; j++)
{
if(s[j] != ' ')
{
cnt++;
if(k != 0 && s[j] != '.')
{
printf(" ");
k = 0;
}
if(s[j] == '.' && s[j - 1] != ' ' && cnt != 1)
{
printf("%d", cnt-1);
}
}
else if(cnt != 0)
{
printf("%d", cnt);
cnt = 0;
k = 1;
}
}

return 0;
} 

# 07-0. 写出这个数 (20)

400 ms

65536 kB

8000 B

Standard

CHEN, Yue

1234567890987654321123456789


yi san wu
#include <stdio.h>
#include <string.h>

const char *num[] = {"ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"};

int main()
{
char ch, number[10];
int s = 0, i;
while( (ch = getchar()) != '\n' )
{
s += ch-'0';
}
sprintf(number, "%d", s);
printf("%s", num[number[0]-'0']);
for ( i=1; i<strlen(number); i++ )
{
printf(" %s", num[number[i]-'0']);
}
printf("\n");

return 0;
} 

# 07-1. 换个格式输出整数 (15)

400 ms

65536 kB

8000 B

Standard

CHEN, Yue

234


BBSSS1234


23


SS123

#include <stdio.h>

int main()
{
int i, j, B, S;
scanf("%d", &i);
if(i >= 1 && i < 1000)
{
if(i >= 1 && i < 10)
{
for(j=1; j<=i; ++j)
{
printf("%d", j);
}
}
if(i >= 10 && i < 100)
{
S = i / 10;
for(j=1; j<=S; ++j)
{
printf("S");
}
i = i % 10;
for(j=1; j<=i; ++j)
{
printf("%d", j);
}
}
if(i >= 100 && i < 1000)
{
B = i / 100;
for(j=1; j<=B; ++j)
{
printf("B");
}
S = i /10 % 10;
for(j=1; j<=S; ++j)
{
printf("S");
}
i = i % 10;
for(j=1; j<=i; ++j)
{
printf("%d", j);
}
}
}

return 0;
} 

# 07-2. A+B和C (15)

50 ms

65536 kB

8000 B

Standard

HOU, Qiming

4
1 2 3
2 3 4
2147483647 0 2147483646
0 -2147483648 -2147483647


Case #1: false
Case #2: true
Case #3: true
Case #4: false

#include <stdio.h>

int main()
{
int t, i;
long a, j, k;
scanf("%d", &t);
if(t >= 1 && t <= 10)
{
for(i=1; i<=t; ++i)
{
scanf("%ld%ld%ld", &a, &j, &k);
if(a + j > k)
{
printf("Case #%d: true\n", i);
}
else
{
printf("Case #%d: false\n", i);
}
}
}

return 0;
} 

# 07-3. 数素数 (20)

100 ms

65536 kB

8000 B

Standard

CHEN, Yue

5 27


11 13 17 19 23 29 31 37 41 43
47 53 59 61 67 71 73 79 83 89
97 101 103

#include <stdio.h>

int a[10001];

int main()
{
int i, c = 0, j, p, m, n;
a[0] = 2;
for ( i=3; ;i+=2 )
{
p = 1;
for ( j=2; j*j<=i; j++ )
{
if ( i % j == 0 )
{
p = 0;
break;
}
}
if ( p )
{
a[++c] = i;
}
if ( c == 10000 ) break;
}
c = 0;
scanf("%d %d", &m, &n);
for ( i=m; i<=n; i++ )
{
c++;
printf("%d", a[i-1]);
if ( c % 10 == 0 )
printf("\n");
else if ( i < n )
printf(" ");
}
if ( c % 5 != 0 )
printf("\n");

return 0;
} 

# 08-0. 查找整数(10)

400 ms

65536 kB

8000 B

Standard

5 7
3 5 7 1 9


2


5 7
3 5 8 1 9


Not Found

#include <stdio.h>
#define N 1000

int a[N];

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

return 0;
} 

# 08-1. 求一批整数中出现最多的个位数字(20)

400 ms

65536 kB

8000 B

Standard

3
1234 2345 3456


3: 3 4

#include <stdio.h>
#include <string.h>

int a[1002];

int main()
{
int t, i, max = 0;
int count[10];
scanf("%d", &t);
for ( i=0; i<10; i++ )
{
count[i] = 0;
}
for ( i=0; i<t; i++ )
{
scanf("%d", &a[i]);
}
for ( i=0; i<t; i++ )
{
while (a[i])
{
count[a[i]%10]++;
a[i] /= 10;
}
}
for ( i=0; i<10; i++ )
{
if ( max < count[i] )
{
max = count[i];
}
}
printf("%d:", max);
for ( i=0; i<10; i++ )
{
if ( count[i] == max )
{
printf(" %d", i);
}
}
printf("\n");

return 0;
} 

# 08-2. 求矩阵的局部极大值(15)

400 ms

65536 kB

8000 B

Standard

4 5
1 1 1 1 1
1 3 9 3 1
1 5 3 5 1
1 1 1 1 1


9 2 3
5 3 2
5 3 4


3 5
1 1 1 1 1
9 3 9 9 1
1 5 3 5 1


None 3 5

#include <stdio.h>

int a[21][21];

int main()
{
int m, n, i, j, p = 0;
scanf("%d %d", &m, &n);
for ( i=0; i<m; i++ )
{
for ( j=0; j<n; j++ )
{
scanf("%d", &a[i][j]);
}
}
for ( i=1; i<m-1; i++ )
{
for ( j=1; j<n-1; j++ )
{
if ( a[i][j] > a[i][j+1] && a[i][j] > a[i][j-1] &&
a[i][j] > a[i+1][j] && a[i][j] > a[i-1][j] )
{
printf("%d %d %d\n", a[i][j], i+1, j+1);
p = 1;
}
}
}
if ( !p )
{
printf("None %d %d\n", m, n);
}

return 0;
} 

# 08-3. 组个最小数 (20)

100 ms

65536 kB

8000 B

Standard

CAO, Peng

2 2 0 0 0 3 0 0 1 0


10015558

#include <stdio.h>

int b[10];

int main()
{
int i, j;
for ( i=0; i<10; i++ )
{
b[i] = 0;
}
for ( i=0; i<10; i++ )
{
scanf("%d", &b[i]);
}
for ( i=1; i<10; i++ )
{
if (b[i])
{
printf("%d", i);
b[i]--;
break;
}
}
for ( i=0; i<10; i++ )
{
for ( j=0; j<b[i]; j++ )
{
printf("%d", i);
}
}
printf("\n");

return 0;
} 

# 10-0. 说反话 (20)

400 ms

65536 kB

8000 B

Standard

CHEN, Yue

Hello World Here I Come


Come I Here World Hello

#include <stdio.h>
#include <string.h>

char str[81];

int main()
{
int i, k;
char *p;
gets(str);
k = strlen(str);
p = str + k;
while (1)
{
if ( p == str )
{
printf("%s\n", p);
break;
}
if (*p == ' ' && *(p+1) != ' ')
{
*p = '\0';
printf("%s ", p+1);
}
p--;
}
return 0;
} 

# 10-1. 在字符串中查找指定字符(15)

400 ms

65536 kB

8000 B

Standard

It is a black box
b


black box


It is a black box
B


Not found

#include <string.h>
#include <stdio.h>

char str[180];

int main()
{
char ch;
int i, k, p = 0;
gets(str);
k = strlen(str);
scanf("%c", &ch);
for ( i=0; i<k; i++ )
{
if ( str[i] == ch )
{
p = 1;
}
if ( p )
{
printf("%c", str[i]);
}
}
if ( !p )
{
}
else
{
printf("\n");
}

return 0;
} 

# 10-2. 删除字符串中的子串(20)

400 ms

65536 kB

8000 B

Standard

Tomcat is a male ccatat
cat


Tom is a male
#include <stdio.h>
#include <string.h>

char s1[81], s2[81];

int main()
{
int i, k;
gets(s1);
gets(s2);
char *p = strstr(s1, s2);
while(1)
{
char *p = strstr(s1, s2);
if ( p )
{
for ( i=0; i<strlen(p) - strlen(s2); i++ )
{
p[i] = p[strlen(s2) + i];
}
p[i] = '\0';
}
else
{
puts(s1);
break;
}
}

return 0;
}

# 10-3. 字符串逆序(15)

400 ms

65536 kB

8000 B

Standard

Hello World!


!dlroW olleH

#include <stdio.h>
#include <string.h>

int main()
{
char str[81];
int k;
gets(str);
k = strlen(str);
k -= 1;
for ( k; k>=0; k-- )
{
printf("%c", str[k]);
}
printf("\n");

return 0;
} 

# 10-4. 字符串循环左移(20)

400 ms

65536 kB

8000 B

Standard

Hello World!
2


llo World!He

#include <stdio.h>
#include <string.h>

char str[103];

int main()
{
int i, k, n;
gets(str);
k = strlen(str);
scanf("%d", &n);
if ( n % k == 0 )
puts(str);
else
{
for ( i=n%k; i<k; i++ )
{
printf("%c", str[i]);
}
for ( i=0; i<n%k; i++ )
{
printf("%c", str[i]);
}
printf("\n");
}

return 0;
} 

# 11-0. 平面向量加法(10)

400 ms

65536 kB

8000 B

Standard

3.5 -2.7 -13.9 8.7


(-10.4, 6.0)

#include <stdio.h>
#include <math.h>
#define EPSILON 0.05

struct Vector {
double x;
double y;
};

int main(void)
{
struct Vector v1, v2, v3;
scanf("%lf%lf%lf%lf", &v1.x, &v1.y, &v2.x, &v2.y);
v3.x = v1.x + v2.x;
v3.y = v1.y + v2.y;
if(fabs(v3.x) < EPSILON)
v3.x = fabs(v3.x);
if(fabs(v3.y) < EPSILON)
v3.y = fabs(v3.y);
printf("(%.1f, %.1f)\n", v3.x, v3.y);
return 0;
} 

# 11-1. 通讯录的录入与显示(10)

400 ms

65536 kB

8000 B

Standard

3
Chris 1984/03/10 F +86181779452 13707010007
LaoLao 1967/11/30 F 057187951100 +8618618623333
QiaoLin 1980/01/01 M 84172333 10086
2 1 7


LaoLao 057187951100 +8618618623333 F 1967/11/30
#include <stdio.h>
#define N 10

struct Person {
char name[11];
char birthday[11];
char sex;
char fixed[17];
char mobile[17];
};

int main(void)
{
struct Person p[N];
int num[N];
int i, n, k;
scanf("%d", &n);
for(i = 0; i < n; ++i)
scanf("%s %s %c %s %s", p[i].name, p[i].birthday,
&p[i].sex, p[i].fixed, p[i].mobile);
scanf("%d", &k);
for(i = 0; i < k; ++i)
scanf("%d", &num[i]);
for(i = 0; i < k; ++i) {
if(num[i] >= 0 && num[i] < n)
printf("%s %s %s %c %s\n", p[num[i]].name,
p[num[i]].fixed, p[num[i]].mobile, p[num[i]].sex, p[num[i]].birthday);
else
}
return 0;
} 

# 此汇总由http://blog.csdn.net/fjinhao编辑整理，转载请注明来源，代码书写有不规范或有错误之处，欢迎批评指教，有任何问题请联系fjinhao@qq.com。

08-22 2万+

03-30 8384
07-08 722
07-12 255
02-05 3053
06-22 803
02-22 358
06-22 979
06-22 741
©️2021 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。