L1-017到底有多二 (15 分)
一個整數“犯二的程度”定義爲該數字中包含2的個數與其位數的比值。如果這個數是負數,則程度增加0.5倍;如果還是個偶數,則再增加1倍。例如數字-13142223336
是個11位數,其中有3個2,並且是負數,也是偶數,則它的犯二程度計算爲:3/11×1.5×2×100%,約爲81.82%。本題就請你計算一個給定整數到底有多二。
輸入格式:
輸入第一行給出一個不超過50位的整數N
。
輸出格式:
在一行中輸出N
犯二的程度,保留小數點後兩位。
輸入樣例:
-13142223336
輸出樣例:
81.82%
注意下輸出%是兩個%%
代碼
import java.util.Scanner;
public class Main {
static Scanner sc = new Scanner(System.in);
public static void main(String[]args)
{
String s = sc.next();
double sum = 1;
if(s.charAt(0) == '-')
{
sum = sum * 1.5;
s = s.substring(1);
}
int len = s.length();
if((s.charAt(len - 1) - '0') % 2 == 0)
sum = sum * 2.0;
double cnt = 0;
for(int i = 0 ; i < len ; i ++)
{
if(s.charAt(i) == '2')
cnt ++;
}
System.out.printf("%.2f%%",cnt / len * sum * 100 );
}
}
L1-018 大笨鐘 (10 分)
微博上有個自稱“大笨鐘V”的傢伙,每天敲鐘催促碼農們愛惜身體早點睡覺。不過由於笨鍾自己作息也不是很規律,所以敲鐘並不定時。一般敲鐘的點數是根據敲鐘時間而定的,如果正好在某個整點敲,那麼“當”數就等於那個整點數;如果過了整點,就敲下一個整點數。另外,雖然一天有24小時,鍾卻是隻在後半天敲1~12下。例如在23:00敲鐘,就是“噹噹噹噹噹噹噹噹噹噹噹”,而到了23:01就會是“噹噹噹噹噹噹噹噹噹噹噹噹”。在午夜00:00到中午12:00期間(端點時間包括在內),笨鍾是不敲的。
下面就請你寫個程序,根據當前時間替大笨鐘敲鍾。
輸入格式:
輸入第一行按照hh:mm
的格式給出當前時間。其中hh
是小時,在00到23之間;mm
是分鐘,在00到59之間。
輸出格式:
根據當前時間替大笨鐘敲鍾,即在一行中輸出相應數量個Dang
。如果不是敲鐘期,則輸出:
Only hh:mm. Too early to Dang.
其中hh:mm
是輸入的時間。
輸入樣例1:
19:05
輸出樣例1:
DangDangDangDangDangDangDangDang
輸入樣例2:
07:05
輸出樣例2:
Only 07:05. Too early to Dang.
import java.util.Scanner;
public class Main {
static Scanner sc = new Scanner(System.in);
public static void main(String[]args)
{
String s = sc.next();
int ss1 = Integer.parseInt(s.substring(0, 2));
int ss2 = Integer.parseInt(s.substring(3));
if(ss1 < 12 || ss1 == 12 && ss2 == 0)
System.out.println("Only " + s +". Too early to Dang.");
else
{
int cnt = ss1 - 12 + (ss2 > 0 ? 1: 0);
for(int i = 1; i <= cnt ; i ++)
System.out.print("Dang");
System.out.println();
}
}
}
L1-019 誰先倒 (15 分)
划拳是古老中國酒文化的一個有趣的組成部分。酒桌上兩人划拳的方法爲:每人口中喊出一個數字,同時用手比劃出一個數字。如果誰比劃出的數字正好等於兩人喊出的數字之和,誰就輸了,輸家罰一杯酒。兩人同贏或兩人同輸則繼續下一輪,直到唯一的贏家出現。
下面給出甲、乙兩人的酒量(最多能喝多少杯不倒)和划拳記錄,請你判斷兩個人誰先倒。
輸入格式:
輸入第一行先後給出甲、乙兩人的酒量(不超過100的非負整數),以空格分隔。下一行給出一個正整數N
(≤100),隨後N
行,每行給出一輪划拳的記錄,格式爲:
甲喊 甲劃 乙喊 乙劃
其中喊
是喊出的數字,劃
是劃出的數字,均爲不超過100的正整數(兩隻手一起劃)。
輸出格式:
在第一行中輸出先倒下的那個人:A
代表甲,B
代表乙。第二行中輸出沒倒的那個人喝了多少杯。題目保證有一個人倒下。注意程序處理到有人倒下就終止,後面的數據不必處理。
輸入樣例:
1 1
6
8 10 9 12
5 10 5 10
3 8 5 12
12 18 1 13
4 16 12 15
15 1 1 16
輸出樣例:
A
1
import java.util.Scanner;
public class Main {
static Scanner sc = new Scanner(System.in);
public static void main(String[]args)
{
int anow = sc.nextInt();
int bnow = sc.nextInt();
int a = anow, b = bnow ;
int n = sc.nextInt();
for(int i = 1; i <= n; i ++)
{
int a1 = sc.nextInt();
int a2 = sc.nextInt();
int b1 = sc.nextInt();
int b2 = sc.nextInt();
if(a2 == a1 + b1)
a --;
if(b2 == a1 + b1)
b--;
if(a2 == a1 + b1 && b2 == a1 + b1)
{
a ++;
b ++;
}
if(a < 0)
{
System.out.println("A");
System.out.println(bnow - b);
System.exit(0);
}else if(b < 0)
{
System.out.println("B");
System.out.println(anow - a);
System.exit(0);
}
}
}
}
L1-020 帥到沒朋友 (20 分)
當芸芸衆生忙着在朋友圈中發照片的時候,總有一些人因爲太帥而沒有朋友。本題就要求你找出那些帥到沒有朋友的人。
輸入格式:
輸入第一行給出一個正整數N
(≤100),是已知朋友圈的個數;隨後N
行,每行首先給出一個正整數K
(≤1000),爲朋友圈中的人數,然後列出一個朋友圈內的所有人——爲方便起見,每人對應一個ID號,爲5位數字(從00000到99999),ID間以空格分隔;之後給出一個正整數M
(≤10000),爲待查詢的人數;隨後一行中列出M
個待查詢的ID,以空格分隔。
注意:沒有朋友的人可以是根本沒安裝“朋友圈”,也可以是隻有自己一個人在朋友圈的人。雖然有個別自戀狂會自己把自己反覆加進朋友圈,但題目保證所有K
超過1的朋友圈裏都至少有2個不同的人。
輸出格式:
按輸入的順序輸出那些帥到沒朋友的人。ID間用1個空格分隔,行的首尾不得有多餘空格。如果沒有人太帥,則輸出No one is handsome
。
注意:同一個人可以被查詢多次,但只輸出一次。
輸入樣例1:
3
3 11111 22222 55555
2 33333 44444
4 55555 66666 99999 77777
8
55555 44444 10000 88888 22222 11111 23333 88888
輸出樣例1:
10000 88888 23333
輸入樣例2:
3
3 11111 22222 55555
2 33333 44444
4 55555 66666 99999 77777
4
55555 44444 22222 11111
輸出樣例2:
No one is handsome
//運行超時
import java.util.HashSet;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;
import java.util.Vector;
public class Main {
static Scanner sc = new Scanner(System.in);
static int pre[] = new int[100050];
static int cnt[] = new int[100050];
static int flag[] = new int[100050];
public static void main(String[]args)
{
for(int i = 0; i <= 99999; i ++)
pre[i] = i;
Vector<Integer>vec = new Vector<Integer>();
Set<Integer>st = new HashSet<Integer>();
int n = sc.nextInt();
while(n -- > 0)
{
int num = sc.nextInt();
int kk = sc.nextInt();
st.add(kk);
for(int i = 2; i <= num; i ++)
{
int gg = sc.nextInt();
merge(kk,gg);
st.add(gg);
}
}
n = sc.nextInt();
for(int i = 0 ; i < n ; i ++)
{
int x = sc.nextInt();
if(vec.contains(x) == false)
vec.add(x);
}
Iterator<Integer> it = st.iterator();
while(it.hasNext())
cnt[find(it.next())] ++;
Vector<Integer>out = new Vector<Integer>();
for(int i = 0 ; i < vec.size(); i ++)
{
if(cnt[find(vec.get(i))] <= 1 )
out.add(vec.get(i));
}
if(out.size() == 0)
System.out.println("No one is handsome");
else
{
int size = out.size();
for(int i = 0 ; i < size; i ++)
{
if(i != size - 1)
System.out.printf("%05d ",out.get(i));
else
System.out.printf("%05d\n",out.get(i));
}
}
}
private static void merge(int x, int y) {
int fx = find(x);
int fy = find(y);
if(fx != fy)
pre[fx] = fy;
return;
}
private static int find(int x) {
if(pre[x] == x)
return x;
else
return pre[x] = find(pre[x]);
}
}
#include<bits/stdc++.h>
using namespace std;
int book[100005];
int gg[100005];
int main()
{
int n,k,x;
scanf("%d",&n);
while(n--)
{
int k;
scanf("%d",&k);
for(int i=1;i<=k;i++){
scanf("%d",&x);
if(k>=2)book[x]=1;
}
}
scanf("%d",&k);
int flag=0;
for(int i=1;i<=k;i++){
scanf("%d",&x);
if(book[x]==0)
{
if(!flag&&gg[x]==0)
{
printf("%05d",x);
flag=1;
gg[x]++;
}else if(flag==1&&gg[x]==0)
{
printf(" %05d",x);
gg[x]++;
}
}
}
if(!flag)printf("No one is handsome\n");
return 0;
}
L1-021 重要的話說三遍 (5 分)
這道超級簡單的題目沒有任何輸入。
你只需要把這句很重要的話 —— “I'm gonna WIN!”——連續輸出三遍就可以了。
注意每遍佔一行,除了每行的回車不能有任何多餘字符。
輸入樣例:
無
輸出樣例:
I'm gonna WIN!
I'm gonna WIN!
I'm gonna WIN!
package vj;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;
import java.util.Vector;
public class Main {
static Scanner sc = new Scanner(System.in);
public static void main(String[]args)
{
System.out.println("I'm gonna WIN!\nI'm gonna WIN!\nI'm gonna WIN!");
}
}
L1-022 奇偶分家 (10 分)
給定N
個正整數,請統計奇數和偶數各有多少個?
輸入格式:
輸入第一行給出一個正整N
(≤1000);第2行給出N
個非負整數,以空格分隔。
輸出格式:
在一行中先後輸出奇數的個數、偶數的個數。中間以1個空格分隔。
輸入樣例:
9
88 74 101 26 15 0 34 22 77
輸出樣例:
3 6
import java.util.HashSet;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;
import java.util.Vector;
public class Main {
static Scanner sc = new Scanner(System.in);
public static void main(String[]args)
{
int n = sc.nextInt();
int odd = 0 , even = 0;
for(int i = 1; i <= n ; i ++)
{
int x = sc.nextInt();
if(x % 2 == 0)
even ++;
else
odd ++;
}
System.out.println(odd +" " + even);
}
}
L1-023 輸出GPLT (20 分)
給定一個長度不超過10000的、僅由英文字母構成的字符串。請將字符重新調整順序,按GPLTGPLT....
這樣的順序輸出,並忽略其它字符。當然,四種字符(不區分大小寫)的個數不一定是一樣多的,若某種字符已經輸出完,則餘下的字符仍按GPLT
的順序打印,直到所有字符都被輸出。
輸入格式:
輸入在一行中給出一個長度不超過10000的、僅由英文字母構成的非空字符串。
輸出格式:
在一行中按題目要求輸出排序後的字符串。題目保證輸出非空。
輸入樣例:
pcTclnGloRgLrtLhgljkLhGFauPewSKgt
輸出樣例:
GPLTGPLTGLTGLGLL
//老師掛的題裏面是正確的 天梯裏面運行超時?? 你逗我?
import java.util.HashSet;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;
import java.util.Vector;
public class Main {
static Scanner sc = new Scanner(System.in);
public static void main(String[]args)
{
String s = sc.next();
s = s.toUpperCase();
int []a = new int[10];
for(int i = 0 ; i < s.length(); i ++)
{
if(s.charAt(i) == 'G') a[1]++;
else if(s.charAt(i) == 'P') a[2]++;
else if(s.charAt(i) == 'L') a[3]++;
else if(s.charAt(i) == 'T') a[4]++;
}
char str[] = {'o','G','P','L','T'};
int total = a[1] + a[2] + a[3] + a[4];
while(total != 0)
{
for(int i = 1; i <= 4; i ++)
{
if(a[i] != 0)
{
System.out.print(str[i]);
total --;
a[i]--;
}
}
}
System.out.println();
}
}
L1-024 後天 (5 分)
如果今天是星期三,後天就是星期五;如果今天是星期六,後天就是星期一。我們用數字1到7對應星期一到星期日。給定某一天,請你輸出那天的“後天”是星期幾。
輸入格式:
輸入第一行給出一個正整數D
(1 ≤ D
≤ 7),代表星期裏的某一天。
輸出格式:
在一行中輸出D
天的後天是星期幾。
輸入樣例:
3
輸出樣例:
5
import java.util.Scanner;
public class Main {
static Scanner sc = new Scanner(System.in);
public static void main(String[]args)
{
int n = sc.nextInt();
System.out.println((n + 2) % 7 == 0 ? 7 : (n + 2) % 7);
}
}