BASIC-13 数列排序
题目
题解
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
int n = reader.nextInt();
final int MAX = n;
int arry[] = new int[MAX];
for(int i = 0; i < n; ++i) {
arry[i] = reader.nextInt();
}
Arrays.sort(arry);
for(int i = 0; i < n; ++i) {
System.out.print(arry[i]+" ");
}
}
}
|
BASIC-12 十六进制转八进制
题目
题解
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
|
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int n=in.nextInt();
String [] result=new String[11];
for(int i=0;i<n;i++){
String h=in.next();
StringBuilder tempB=new StringBuilder();
for(int m=0;m<h.length();m++){
char numH=h.charAt(m);
String b=Integer.toBinaryString(Integer.valueOf(String.valueOf(numH), 16));
for(int k=b.length();k<4;k++){
b='0'+b;
}
tempB.append(b);
}
StringBuilder tempO=new StringBuilder();
int addZero=3-tempB.length()%3;
for(int p=0;p<addZero;p++){
tempB=new StringBuilder("0").append(tempB);
}
for(int m=0;m<tempB.length();m+=3){
String numB=tempB.substring(m, m+3);
String o=Integer.toOctalString(Integer.valueOf(String.valueOf(numB), 2));
tempO.append(o);
}
result[i]=tempO.toString().replaceAll("^(0+)", "");
}
for(int i=0;i<n;i++){
System.out.println(result[i]);
}
}
}
|
BASIC-11 十六进制转十进制
题目
题解
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
|
int main()
{
int index = 0, strLength = 0;
long long dec = 0;
char * hex = (char *) malloc ( sizeof(char) );
scanf("%s", hex);
strLength = strlen(hex);
for (index = 0; index < strLength; ++index)
{
switch (*(hex+index))
{
case '0': dec += 0 * pow(16, (strLength-index-1));break;
case '1': dec += 1 * pow(16, (strLength-index-1));break;
case '2': dec += 2 * pow(16, (strLength-index-1));break;
case '3': dec += 3 * pow(16, (strLength-index-1));break;
case '4': dec += 4 * pow(16, (strLength-index-1));break;
case '5': dec += 5 * pow(16, (strLength-index-1));break;
case '6': dec += 6 * pow(16, (strLength-index-1));break;
case '7': dec += 7 * pow(16, (strLength-index-1));break;
case '8': dec += 8 * pow(16, (strLength-index-1));break;
case '9': dec += 9 * pow(16, (strLength-index-1));break;
case 'A': dec += 10 * pow(16, (strLength-index-1));break;
case 'B': dec += 11 * pow(16, (strLength-index-1));break;
case 'C': dec += 12 * pow(16, (strLength-index-1));break;
case 'D': dec += 13 * pow(16, (strLength-index-1));break;
case 'E': dec += 14 * pow(16, (strLength-index-1));break;
case 'F': dec += 15 * pow(16, (strLength-index-1));break;
default:printf("Error hex data.\n"); break;
}
}
printf("%lld\n", dec);
return 0;
}
|
BASIC-10 十进制转十六进制
题目
题解
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
|
int main()
{
int res = 0, mod = 0, dec = 0, index = 0;
char * hex = (char *) malloc (sizeof(char));
scanf("%d", &dec);
res = dec;
do {
mod = res % 16;
switch(mod)
{
case 0: *(hex+index) = '0'; break;
case 1: *(hex+index) = '1'; break;
case 2: *(hex+index) = '2'; break;
case 3: *(hex+index) = '3'; break;
case 4: *(hex+index) = '4'; break;
case 5: *(hex+index) = '5'; break;
case 6: *(hex+index) = '6'; break;
case 7: *(hex+index) = '7'; break;
case 8: *(hex+index) = '8'; break;
case 9: *(hex+index) = '9'; break;
case 10: *(hex+index) = 'A'; break;
case 11: *(hex+index) = 'B'; break;
case 12: *(hex+index) = 'C'; break;
case 13: *(hex+index) = 'D'; break;
case 14: *(hex+index) = 'E'; break;
case 15: *(hex+index) = 'F'; break;
default:break;
}
++index;
} while ( (res = res / 16) != 0 );
for (index = index-1; index >= 0; --index)
{
printf("%c", *(hex+index));
}
return 0;
}
|
BASIC-9 特殊回文数
题目
题解
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
|
int main()
{
unsigned int n = 0;
int a, b, c;
scanf("%d", &n);
for (a = 1; a <= 9; ++a)
{
for (b = 0; b <= 9; ++b)
{
for (c = 0; c <= 9; ++c)
{
if ( n == (a+b)*2 + c )
{
printf("%d\n", (a*10000+b*1000+c*100+b*10+a));
}
}
}
}
// 没有把循环合并在一起的原因是题目要求 从小到大 输出
for (a = 1; a <= 9; ++a)
{
for (b = 0; b <= 9; ++b)
{
for (c = 0; c <= 9; ++c)
{
if ( n == (a+b+c) * 2 )
{
printf("%d\n", (a*100000+b*10000+c*1000+c*100+b*10+a));
}
}
}
}
return 0;
}
|
BASIC-8 回文数
题目
题解
1
2
3
4
5
6
7
8
9
10
11
12
13
|
int main()
{
int a, b;
for (a = 1; a <= 9; ++a)
{
for (b = 0; b <= 9; ++b)
{
printf("%d\n", (a*1000+b*100+b*10+a));
}
}
return 0;
}
|
BASIC-7 特殊的数字
题目
题解
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
int main()
{
int a, b, c;
for (a = 1; a <= 9; ++a)
{
for (b = 0; b <= 9; ++b)
{
for (c = 0; c <= 9; ++c)
{
if ((a*100+b*10+c) == pow(a, 3) + pow(b, 3) + pow(c, 3))
{
printf("%d\n", (a*100+b*10+c));
}
}
}
}
return 0;
}
|
BASIC-6 杨辉三角形
题目
题解
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
|
int main()
{
int n = 0;
int row = 0, colunm = 0;
int array[MAXSIZE][MAXSIZE] = { 0 };
scanf("%d", &n); // 输入行数
// 初始化 1
for (row = 0; row < n; ++row)
{
colunm = row;
array[row][0] = 1;
array[row][colunm] = 1;
}
for (row = 2; row < n; ++row)
{
for (colunm = 1; colunm < n; ++colunm)
{
array[row][colunm] = array[row - 1][colunm] + array[row-1][colunm-1];
}
}
for (row = 0; row < n; ++row)
{
for (colunm = 0; colunm < n; ++colunm)
{
if (row >= colunm)
printf("%d ", array[row][colunm]);
}
printf("\n");
}
return 0;
}
|
BASIC-5 查找整数
题目
题解
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
|
int main()
{
unsigned int n;
unsigned int num[MAXSIZE];
int i, j;
int a;
scanf("%d", &n);
for (i = 0; i < n; ++i)
{
scanf("%d", &num[i]);
}
scanf("%d", &a);
for (j = 0; j < n; ++j)
{
if (a == num[j])
{
printf("%d\n", j+1);
return 0;
}
}
printf("%d\n", -1);
return 0;
}
|
BASIC-4 数列特征
题目
题解
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
|
int main()
{
int i;
int num[MAXSIZE];
int max = -10001, min = 10001, sum = 0;
unsigned int n;
scanf("%d", &n);
for (i = 0; i < n; ++i)
{
scanf("%d", &num[i]);
if (num[i] > max)
{
max = num[i];
}
if (num[i] < min)
{
min = num[i];
}
sum += num[i];
}
printf("%d\n%d\n%d\n", max, min, sum);
return 0;
}
|
BASIC-3 字母图形
题目
题解
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
// 你也可以把 A 的前后分为两部分
int main(void)
{
unsigned int n, m; // n 行, m 字符/行
int row, colum;
scanf("%d %d", &n, &m);
for (row = 1; row <= n; ++row)
{
for (colum = 1; colum <= m; ++colum)
printf("%c", (65 + (int)fabs(row - colum)));
printf("\n");
}
return 0;
}
|
BASIC-2 01 字串
题目
题解
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
int main(void)
{
int a,b,c,d,e;
for (a = 0; a <= 1; ++a)
{
for (b = 0; b <= 1; ++b)
{
for (c = 0; c <= 1; ++c)
{
for (d = 0; d <= 1; ++d)
{
for (e = 0; e <=1; ++e)
{
printf("%d%d%d%d%d\n", a, b, c, d, e);
}
}
}
}
}
return 0;
}
|
BASIC-1 闰年判断
题目
题解
1
2
3
4
5
6
7
8
9
|
int main(void)
{
int year;
scanf("%d", &year);
if ( (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0) ) printf("yes");
else printf("no");
return 0;
}
|
</div>