L1-025 正整數A+B (15 分)
題的目標很簡單,就是求兩個正整數A
和B
的和,其中A
和B
都在區間[1,1000]。稍微有點麻煩的是,輸入並不保證是兩個正整數。
輸入格式:
輸入在一行給出A
和B
,其間以空格分開。問題是A
和B
不一定是滿足要求的正整數,有時候可能是超出範圍的數字、負數、帶小數點的實數、甚至是一堆亂碼。
注意:我們把輸入中出現的第1個空格認爲是A
和B
的分隔。題目保證至少存在一個空格,並且B
不是一個空字符串。
輸出格式:
如果輸入的確是兩個正整數,則按格式A + B = 和
輸出。如果某個輸入不合要求,則在相應位置輸出?
,顯然此時和也是?
。
輸入樣例1:
123 456
輸出樣例1:
123 + 456 = 579
輸入樣例2:
22. 18
輸出樣例2:
? + 18 = ?
輸入樣例3:
-100 blabla bla...33
輸出樣例3:
? + ? = ?
import java.util.Scanner;
public class Main {
static Scanner sc = new Scanner(System.in);
public static void main(String[]args)
{
String s = sc.next();
String t = sc.nextLine();
t = t.substring(1);//因爲第一個是空格
int flag1 = 0, flag2 = 0;
for(int i = 0 ; i < s.length(); i ++)
{
if(s.charAt(i) < '0' || s.charAt(i) > '9' || s.length() > 4 )
flag1 = 1;
}
for(int i = 0 ; i < t.length(); i ++)
{
if(t.charAt(i) < '0' || t.charAt(i) > '9' || t.length() > 4 )
flag2 = 1;
}
int a = 0, b = 0;
if(flag1 == 0)
a = Integer.parseInt(s);
if(flag2 == 0)
b = Integer.parseInt(t);
if(a < 1 || a > 1000)
flag1 = 1 ;
if(b < 1 || b > 1000)
flag2 = 1 ;
if(flag1 == 1)
System.out.print("?");
else
System.out.print(a);
System.out.print(" + ");
if(flag2 == 1)
System.out.print("?");
else
System.out.print(b);
System.out.print(" = ");
if(flag1 == 1 || flag2 == 1)
System.out.println("?");
else
System.out.println(a+b);
}
}
L1-026 I Love GPLT (5 分)
這道超級簡單的題目沒有任何輸入。
你只需要把這句很重要的話 —— “I Love GPLT”——豎着輸出就可以了。
所謂“豎着輸出”,是指每個字符佔一行(包括空格),即每行只能有1個字符和回車。
輸入樣例:
無
輸出樣例:
I
L
o
v
e
G
P
L
T
注意:輸出的兩個空行中各有一個空格。
import java.util.Scanner;
public class Main {
static Scanner sc = new Scanner(System.in);
public static void main(String[]args)
{
String s = new String("I Love GPLT");
for(int i = 0 ; i < s.length(); i ++)
System.out.println(s.charAt(i));
}
}
L1-027 出租 (20 分)
下面是新浪微博上曾經很火的一張圖:
一時間網上一片求救聲,急問這個怎麼破。其實這段代碼很簡單,index
數組就是arr
數組的下標,index[0]=2
對應 arr[2]=1
,index[1]=0
對應 arr[0]=8
,index[2]=3
對應 arr[3]=0
,以此類推…… 很容易得到電話號碼是18013820100
。
本題要求你編寫一個程序,爲任何一個電話號碼生成這段代碼 —— 事實上,只要生成最前面兩行就可以了,後面內容是不變的。
輸入格式:
輸入在一行中給出一個由11位數字組成的手機號碼。
輸出格式:
爲輸入的號碼生成代碼的前兩行,其中arr
中的數字必須按遞減順序給出。
輸入樣例:
18013820100
輸出樣例:
int[] arr = new int[]{8,3,2,1,0};
int[] index = new int[]{3,0,4,3,1,0,2,4,3,4,4};
import java.util.Scanner;
import java.util.Vector;
public class Main {
static Scanner sc = new Scanner(System.in);
public static void main(String[]args)
{
String s = sc.next();
int []a = new int[15];
for(int i = 0 ; i < 11; i ++)
a[s.charAt(i) - '0'] = 1;
int cnt = 0;
for(int i = 0 ; i <= 9 ; i ++)
if(a[i] == 1)
cnt ++;
System.out.print("int[] arr = new int[]{");
Vector<Integer>vec = new Vector<Integer>();
for(int i = 9; i >= 0; i --)
{
if(a[i] == 1)
{
System.out.print(i);
vec.add(i);
cnt--;
if(cnt != 0)
System.out.print(",");
else
System.out.println("};");
}
}
System.out.print("int[] index = new int[]{");
for(int i = 0 ; i < 11; i ++)
{
for(int j = 0 ; j < vec.size(); j ++)
{
if(s.charAt(i) - '0' == vec.get(j))
System.out.print(j);
}
if(i != 10)
System.out.print(",");
else
System.out.println("};");
}
}
}
L1-028 判斷素數 (10 分)
本題的目標很簡單,就是判斷一個給定的正整數是否素數。
輸入格式:
輸入在第一行給出一個正整數N
(≤ 10),隨後N
行,每行給出一個小於231的需要判斷的正整數。
輸出格式:
對每個需要判斷的正整數,如果它是素數,則在一行中輸出Yes
,否則輸出No
。
輸入樣例:
2
11
111
輸出樣例:
Yes
No
import java.util.Scanner;
import java.util.Vector;
public class Main {
static Scanner sc = new Scanner(System.in);
public static void main(String[]args)
{
int t = sc.nextInt();
for(int i = 1; i <= t ; i ++)
{
int x = sc.nextInt();
if(check(x))
System.out.println("Yes");
else
System.out.println("No");
}
}
private static boolean check(int x) {
if(x == 2)
return true;
if(x == 1)
return false;
for(int i = 2; i <= (int)Math.sqrt(x); i++)
{
if(x % i == 0)
return false;
}
return true;
}
}
L1-029 是不是太胖了 (5 分)
據說一個人的標準體重應該是其身高(單位:釐米)減去100、再乘以0.9所得到的公斤數。已知市斤是公斤的兩倍。現給定某人身高,請你計算其標準體重應該是多少?(順便也悄悄給自己算一下吧……)
輸入格式:
輸入第一行給出一個正整數H
(100 < H
≤ 300),爲某人身高。
輸出格式:
在一行中輸出對應的標準體重,單位爲市斤,保留小數點後1位。
輸入樣例:
169
輸出樣例:
124.2
import java.util.Scanner;
import java.util.Vector;
public class Main {
static Scanner sc = new Scanner(System.in);
public static void main(String[]args)
{
int t = sc.nextInt();
double gg = (t - 100) * 0.9 * 2;
System.out.printf("%.1f",gg);
}
}
L1-030 一幫一 (15 分)
“一幫一學習小組”是中小學中常見的學習組織方式,老師把學習成績靠前的學生跟學習成績靠後的學生排在一組。本題就請你編寫程序幫助老師自動完成這個分配工作,即在得到全班學生的排名後,在當前尚未分組的學生中,將名次最靠前的學生與名次最靠後的異性學生分爲一組。
輸入格式:
輸入第一行給出正偶數N
(≤50),即全班學生的人數。此後N
行,按照名次從高到低的順序給出每個學生的性別(0代表女生,1代表男生)和姓名(不超過8個英文字母的非空字符串),其間以1個空格分隔。這裏保證本班男女比例是1:1,並且沒有並列名次。
輸出格式:
每行輸出一組兩個學生的姓名,其間以1個空格分隔。名次高的學生在前,名次低的學生在後。小組的輸出順序按照前面學生的名次從高到低排列。
輸入樣例:
8
0 Amy
1 Tom
1 Bill
0 Cindy
0 Maya
1 John
1 Jack
0 Linda
輸出樣例:
Amy Jack
Tom Linda
Bill Maya
Cindy John
import java.util.Scanner;
import java.util.Vector;
public class Main {
static Scanner sc = new Scanner(System.in);
static class node{
int id;
String s;
int flag;
public node(int a, String st, int sex) {id = a;s = st;flag = sex;}
}
static node []s = new node[55];
public static void main(String[]args)
{
int t = sc.nextInt();
for(int i = 1; i <= t; i ++)
{
int x = sc.nextInt();
String gg = sc.next();
s[i] = new node(x,gg,0);
}
int cnt = 0;
for(int i = 1; i <= t && cnt < t / 2; i ++)
{
if(s[i].flag == 0)
{
System.out.print(s[i].s+ " ");
s[i].flag = 1;//被用了
for(int j = t; j >= 1; j --)
{
if(s[j].flag == 0)
{
if(s[j].id + s[i].id == 1)
{
s[j].flag = 1;
System.out.println(s[j].s);
break;
}
}
}
}
}
}
}
L1-031 到底是不是太胖了 (10 分)
據說一個人的標準體重應該是其身高(單位:釐米)減去100、再乘以0.9所得到的公斤數。真實體重與標準體重誤差在10%以內都是完美身材(即 | 真實體重 − 標準體重 | < 標準體重×10%)。已知市斤是公斤的兩倍。現給定一羣人的身高和實際體重,請你告訴他們是否太胖或太瘦了。
輸入格式:
輸入第一行給出一個正整數N
(≤ 20)。隨後N
行,每行給出兩個整數,分別是一個人的身高H
(120 < H
< 200;單位:釐米)和真實體重W
(50 < W
≤ 300;單位:市斤),其間以空格分隔。
輸出格式:
爲每個人輸出一行結論:如果是完美身材,輸出You are wan mei!
;如果太胖了,輸出You are tai pang le!
;否則輸出You are tai shou le!
。
輸入樣例:
3
169 136
150 81
178 155
輸出樣例:
You are wan mei!
You are tai shou le!
You are tai pang le!
import java.util.Scanner;
import java.util.Vector;
public class Main {
static Scanner sc = new Scanner(System.in);
public static void main(String[]args)
{
int t = sc.nextInt();
while(t -- > 0)
{
double a = sc.nextDouble();
double b = sc.nextDouble();
double right = (a - 100) * 0.9 * 2;
if(Math.abs(b - right) < right * 0.1)
System.out.println("You are wan mei!");
else if(b > right){
System.out.println("You are tai pang le!");
}
else{
System.out.println("You are tai shou le!");
}
}
}
}
L1-032 Left-pad (20 分)
根據新浪微博上的消息,有一位開發者不滿NPM(Node Package Manager)的做法,收回了自己的開源代碼,其中包括一個叫left-pad的模塊,就是這個模塊把javascript裏面的React/Babel幹癱瘓了。這是個什麼樣的模塊?就是在字符串前填充一些東西到一定的長度。例如用*
去填充字符串GPLT
,使之長度爲10,調用left-pad的結果就應該是******GPLT
。Node社區曾經對left-pad緊急發佈了一個替代,被嚴重吐槽。下面就請你來實現一下這個模塊。
輸入格式:
輸入在第一行給出一個正整數N
(≤104)和一個字符,分別是填充結果字符串的長度和用於填充的字符,中間以1個空格分開。第二行給出原始的非空字符串,以回車結束。
輸出格式:
在一行中輸出結果字符串。
輸入樣例1:
15 _
I love GPLT
輸出樣例1:
____I love GPLT
輸入樣例2:
4 *
this is a sample for cut
輸出樣例2:
cut
import java.util.Scanner;
import java.util.Vector;
public class Main {
static Scanner sc = new Scanner(System.in);
public static void main(String[]args)
{
int t = sc.nextInt();
String s = sc.nextLine();
s = s.substring(1);
String gg = sc.nextLine();
if(gg.length() > t)
System.out.println(gg.substring(gg.length() - t));
else if(gg.length() == t)
System.out.println(gg);
else
{
int k = t - gg.length();
for(int i = 1; i <= k; i ++)
System.out.print(s);
System.out.println(gg);
}
}
}