“顶嵌杯”全国嵌入式系统C语言编程大赛初赛解题报告

http://acm.pku.edu.cn/JudgeOnline/showcontest?contest_id=1345

A题:位操作

import java.util.Scanner;

public class Main {

public static String trans(String[] s) {

Long l = Long.parseLong(s[0].trim(), 16);
String b = Long.toBinaryString(l);
// System.out.println(b);
b = "00000000000000000000000000000000" + b;
b = b.substring(b.length() - 32);
// System.out.println(b);
char[] ch = b.toCharArray();
// System.out.println(Integer.parseInt(String.valueOf(s[1].trim())));
ch[b.length() - 1 - Integer.parseInt(String.valueOf(s[1].trim()))] = '0';
int i = Integer.parseInt(String.valueOf(s[2].trim()));
// System.out.println(b.length());
ch[b.length() - 1 - i] = '1';
ch[b.length() - i] = '1';
ch[b.length() + 1 - i] = '0';

String str = new String(ch);
// System.out.println(str);
Long ll = Long.parseLong(str, 2);

return Long.toHexString(ll);
}

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

// while (sc.hasNextLine()) {
String str = sc.nextLine();
System.out.println(Main.trans(str.split(",")));
// }
}

}

B题:破译密码简单的置换,对着密码表

import java.util.Scanner;

public class Main {

private static final String s1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
private static final String s2 = "VWXYZABCDEFGHIJKLMNOPQRSTU";

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);
while (sc.hasNextLine()) {
String str = sc.nextLine();
if (str.equals("ENDOFINPUT")) {
break;
} else if (!str.equals("START") && !str.equals("END")) {
StringBuilder sb = new StringBuilder();
int len = str.length();
for (int i = 0; i < len; ++i) {
char c = str.charAt(i);
int index = s1.indexOf(c);
if (index > -1) {
char ch = s2.charAt(index);
sb.append(ch);
} else
sb.append(c);
}
System.out.println(sb.toString());
}
}

}
}
C题:小孩报数问题c语言书课后习题(赫赫)


import java.util.LinkedList; import java.util.Scanner; public class Main { public static void sloves(LinkedList<String> childList, int w, int s) { int sum = childList.size(); while (sum > 0) { int index = (w + s - 1 - 1) % sum; w = index + 1; //System.out.println("index = " + index); System.out.println(childList.get(index)); childList.remove(index); --sum; if (w > sum) w = 0; } } /** * @param args */ public static void main(String[] args) { Scanner sc = new Scanner(System.in); int testNum = sc.nextInt(); LinkedList<String> ch = new LinkedList<String>(); sc.nextLine(); for (int i = 0; i < testNum; ++i) { ch.add(sc.nextLine()); // System.out.println(ch.get(i)); } // sc.nextLine(); String s = sc.nextLine(); String[] ss = s.split(","); sloves(ch, Integer.parseInt(ss[0]), Integer.parseInt(ss[1])); } } D题:时间日期格式转换:需要注意的地方:注意中午和凌晨时间的特殊表示:00:00:00是早晨12:00:00 import java.util.Scanner; public class Main { /** * @param args */ public static void main(String[] args) { Scanner sc = new Scanner(System.in); int testNum = sc.nextInt(); sc.nextLine(); for (int i = 0; i < testNum; ++i) { String str = sc.nextLine(); int index1 = str.indexOf("/"); int index2 = str.indexOf("-"); String s1 = str.substring(index1 + 1, index2); s1 = s1 + "/" + str.substring(0, index1); String s2 = str.substring(index2 + 1); String hh = s2.substring(0, s2.indexOf(":")); int h = Integer.parseInt(s2.substring(0, s2.indexOf(":"))); if (h < 12 && h > 0) System.out.println(s1 + "-" + s2 + "am"); else if (h == 0) System.out.println(s1 + "-" + s2.replace("00", "12") + "am"); else { if (h != 12) h = h - 12; String temp = "00" + h; temp = temp.substring(temp.length() - 2); System.out.println(s1 + "-" + s2.replace(hh, temp) + "pm"); } // System.out.println(h); } } }

E题:字母旋转游戏(简单的模拟题)

import java.util.Scanner; public class Main { private static int sum = 0; private static int r = 0; private static int c = 0; private static int flag1 = 0; private static int flag2 = 0; private static char cc = 'A'; private static int leftCol = 0; private static int rightCol = 0; private static int topRow = 0; private static int bottomRow = 0; public static void sloves(char[][] ch, int n, int m) { // System.out.println(sum + "," + r + "," + c + "," + flag1 + "," + // flag2); if (flag1 == 0) { if (flag2 == 0) { while (c <= rightCol) { ch[r][c] = cc; if (cc == 'Z') cc = 'A'; else cc = (char) (cc + 1); c++; ++sum; } c--; r++; flag1 = 1; flag2 = 0; topRow++; // sloves(ch, r, c - 1, 1, 0, n, m, cc); } else { while (c >= leftCol) { ch[r][c] = cc; if (cc == 'Z') cc = 'A'; else cc = (char) (cc + 1); c--; ++sum; } c++; flag1 = 1; flag2 = 1; r--; bottomRow--; // sloves(ch, r, c + 1, 1, 1, n, m, cc); } } else { if (flag2 == 0) { while (r <= bottomRow) { ch[r][c] = cc; if (cc == 'Z') cc = 'A'; else cc = (char) (cc + 1); ++r; ++sum; } r--; flag1 = 0; flag2 = 1; c--; rightCol--; // sloves(ch, r - 1, c, 0, 1, n, m, cc); } else { while (r >= topRow) { ch[r][c] = cc; if (cc == 'Z') cc = 'A'; else cc = (char) (cc + 1); r--; ++sum; } r++; flag1 = flag2 = 0; c++; leftCol++; // sloves(ch, r + 1, c, 0, 0, n, m, cc); } } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNextInt()) { int m = sc.nextInt(); int n = sc.nextInt(); char[][] ch = new char[m][n]; // for (int i = 0; i < m; ++i) // Arrays.fill(ch[i], '0'); /********* 初始化全局变量 **********/ cc = 'A'; sum = 0; r = 0; c = 0; flag1 = 0; flag2 = 0; leftCol = 0; rightCol = n - 1; topRow = 0; bottomRow = m - 1; /****** 初始化完毕s ********/ int num = n * m; while (sum < num) sloves(ch, n, m); for (int i = 0; i < m; i++) { System.out.print(" "); for (int j = 0; j < n - 1; j++) { System.out.print(ch[i][j] + " "); } System.out.print(ch[i][n - 1]); System.out.println(); } } } }

注:成都大学ACM集训队训练题目

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值