1.标题:跟奥巴马
美国总统奥巴马不仅呼吁所有人都学习编程,甚至以身作则编写代码,成为美国历史上首位编写计算机代码
的总统。2014年底,为庆祝“计算机科学教育周”正式启动,奥巴马编写了很简单的计算机代码:在屏幕上画
一个正方形。现在你也跟他一起画吧!
输入描述:
输入在一行中给出正方形边长N(3<=N<=20)和组成正方形边的某种字符C,间隔一个空格。
输出描述:
输出由给定字符C画出的正方形。但是注意到行间距比列间距大,所以为了让结果看上去更像正方形,我们输
出的行数实际上是列数的50%(四舍五入取整)。
public class Test13 {
private static void Solution(int num,char ch){
int row = 0;
if(num%2==0){
row = num/2;
}else {
row = (num/2)+1;
}
for(int i = 0;i<row;i++){
for(int j = 0;j<num;j++){
if(i==0||j==0||i==row-1||j==num-1){
System.out.print(ch);
}else {
System.out.print(" ");
}
}
System.out.println();
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
String[] sh = s.split(" ");
int num = Integer.parseInt(sh[0]);
char[] c = sh[1].toCharArray();
char ch = c[0];
Solution(num,ch);
}
}
//方法二
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
//输出的列数
String c = sc.next();
//输出的字符
for (int i = 1; i <= N; i++) {
//输出第一行
System.out.print(c);
}
System.out.println();
//第一行换行
for (int i = 1; i <= Math.ceil((double) N / 2) - 2; i++) {
//输出中间行
System.out.print(c);
//中间行第一个字符
for (int j = 2; j <= N - 1; j++) {
System.out.print(" ");
//中间行其它字符为空字符串
}
System.out.println(c);
//中间行最后一个字符
}
for (int i = 1; i <= N; i++) {
//输出最后一行
System.out.print(c);
}
}
}
2.超长正整数相加
import java.util.*;
import java.math.BigInteger;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
while (input.hasNext()) {
String s1 = input.next();
String s2 = input.next();
BigInteger num1 = new BigInteger(s1);
//采用BigInteger可以直接进行大整数进 行计算
BigInteger num2 = new BigInteger(s2);
System.out.println(num1.add(num2));
}
}
}