ACM帳號終於辦好了
JAVA上傳簡直被各種80
前面一定要是public class Main才能過
public class UVA就不過了
改成 class UVA 倒是可以過
還有那超麻煩的輸出排版
上傳後各種超時
想說是不是我寫得太爛
我隨便去找一題別人寫的
還不是一樣超時
林阿罵哩
用JAVA來寫是不是太自虐@@
改一天再來把寫過的修一修
2016/5/14要去成大的桂冠杯玩一下闖關組
所以最近才跑來練練ACM 哀英文太差了
JAVA UVA練習
2016年5月13日 星期五
UVA 389: Basically Speaking
Q389: Basically Speaking
有一家製造計算機的公司請你幫忙設計新型的計算機。這台計算機必須可以做不同數字系統的轉換(例如:將一個2進位的數字轉換成10進位的數字)。這台計算機還必須有下列的特性:- 有 7-digit 的顯示
- 除了有 0 ~ 9 的按鈕之外,還有 A ~ F 的按鈕
- 支援 2 進位到 16 進位
Input
輸入含有多組測試資料。每組測試資料一列,含有 3 個數字。第一個數字為你所要轉換的數字。第二個數字為要轉換的數字為多少進位。第三個數字為要將這個數字轉成多少進位的數。在這3個數前後可能有一或多個空白字元存在。
以Sample Input 第一組測試資料為例:將 1111000 從 2 進位 轉換成 10 進位。
Output
對每組測試資料輸出一列 ,輸出轉換後出現在計算機顯示器上的數字。這些數字長度為 7,靠右對齊。如果這個數字太大無法以計算機顯示器顯示,則輸出 "ERROR"。輸出格式請參考 Sample Output。Sample Input | Sample Output |
1111000 2 10 1111000 2 16 2102101 3 10 2102101 3 15 12312 4 2 1A 15 2 1234567 10 16 ABCD 16 15 0 10 2 000234 10 10 F00000 16 10 | 120 78 1765 7CA ERROR 11001 12D687 D071 0 234 ERROR |
import java.util.Scanner;
public class UVA_389 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
while(true){
String sb=sc.nextLine(),s_n=sc.nextLine(),s_m=sc.nextLine();
String s="",a="",b=""; //沒是幾進位的數字也要在那邊輸入空白 有夠不爽的
for(int i=0;i<sb.length();i++)
if(sb.charAt(i)!=' ')
s+=String.valueOf(sb.charAt(i));
for(int i=0;i<s_n.length();i++)
if(s_n.charAt(i)!=' ')
a+=String.valueOf(s_n.charAt(i));
for(int i=0;i<s_m.length();i++)
if(s_m.charAt(i)!=' ')
b+=String.valueOf(s_m.charAt(i));
int n=Integer.parseInt(a),m=Integer.parseInt(b);
int ans=Integer.parseInt(s,n);
char[] bef=new char[8];
int c=0;
while(ans>0 && c<7){
bef[c++]=change(ans%m);
ans/=m;
}
if(c>=7){
System.out.println(" ERROR");
}else{
for(int i=7-c;i>1;i--)
System.out.print(" ");
for(int i=c;i>=0;i--)
System.out.print(bef[i]);
System.out.println();
}
}
}
private static char change(int a){
switch(a){
case 0:
return '0';
case 1:
return '1';
case 2:
return '2';
case 3:
return '3';
case 4:
return '4';
case 5:
return '5';
case 6:
return '6';
case 7:
return '7';
case 8:
return '8';
case 9:
return '9';
case 10:
return 'A';
case 11:
return 'B';
case 12:
return 'C';
case 13:
return 'D';
case 14:
return 'E';
case 15:
return 'F';
}
return 0;
}
}
2016年5月12日 星期四
UVA 386: Perfect Cubes
Q386: Perfect Cubes
我們可以找到所謂的"完美立方"(Perfect Cube)方程式:a3 = b3 + c3 + d3例如:123 = 63 + 83 + 103
這個問題是要請你寫一個程式找到 a<= 200,所有這樣的集合{a,b,c,d}來滿足以上的方程式(a,b,c,d均大於1)
Output
輸出應該像是以下所示:每一個Perfect Cube一列,並且不同的列之間,a以非遞減的順序出現。而在同同一列中b,c,d也是以非遞減的順序出現。若存在a可以由好幾組b,c,d的組合產生,越小的b應該要越先出現(可以參考輸出的第三、四列)。
Cube = 6, Triple = (3,4,5) Cube = 12, Triple = (6,8,10) Cube = 18, Triple = (2,12,16) Cube = 18, Triple = (9,12,15) Cube = 19, Triple = (3,10,18) Cube = 20, Triple = (7,14,17) Cube = 24, Triple = (12,16,20) ...... ...... ......
http://luckycat.kshs.kh.edu.tw/homework/q386.htm
public class UVA_386 {public static void main(String[] args) {
for(int a=6;a<=200;a++){
for(int b=2;b<a;b++){
for(int c=b;c<a;c++){
for(int d=c;d<a;d++){
if(Math.pow(a,3)==Math.pow(b,3)+Math.pow(c,3)+Math.pow(d,3)){
System.out.printf("Cube = %d, Triple = (%d,%d,%d)\n",a,b,c,d);
}
}
}
}
}
}
}
UVA 374: Big Mod
Q374: Big Mod
計算 R = BP mod M對相當大的B、P、M請寫一個有效率的演算法來。
Input
每筆測試資料有3行,各有1個整數分別代表B、P、M。
其中 0 <= B <= 2147483647 0 <= P <= 2147483674 1 <= M <= 46340
其中 0 <= B <= 2147483647 0 <= P <= 2147483674 1 <= M <= 46340
Output
輸出計算的結果,每筆測試資料一行。
Sample input
3 18132 17 17 1765 3 2374859 3029382 36123
Sample Output
13 2 13195
http://luckycat.kshs.kh.edu.tw/homework/q374.htm
import java.util.Scanner;
public class UVA_374 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
while(true){
System.out.println();
int B=sc.nextInt(),P=sc.nextInt(),M=sc.nextInt();
long R=1;
for(int i=1;i<=P;i++){
R*=B;
R%=M;
}
System.out.println(R);
}
}
}
UVA 382: Perfection
Q382: Perfection
一個整數b如果可以被另一個整數a整除(在這裡a>b),我們稱b是a的一個因數。
Perfect Number是一個正整數並且等於其所有因數的和。例如:6和28都是perfect number。因為6=1+2+3,28=1+2+4+7+14。如果一個正整數不是perfect,那他就是deficient或者是abundant,根據其所有因數的和是小於或大於這個數本身。因此,9是deficient因為1+3<9。而12是abundant因為1+2+3+4+6>12。
請寫一個程式求出某一個數是perfect, deficient 或者abundant。
Perfect Number是一個正整數並且等於其所有因數的和。例如:6和28都是perfect number。因為6=1+2+3,28=1+2+4+7+14。如果一個正整數不是perfect,那他就是deficient或者是abundant,根據其所有因數的和是小於或大於這個數本身。因此,9是deficient因為1+3<9。而12是abundant因為1+2+3+4+6>12。
請寫一個程式求出某一個數是perfect, deficient 或者abundant。
Input
有一連串(不會超過100個)的正整數n(1 <= n < 60000),n=0代表輸入結束。
Output
請參考 Sample Output。
數字部分佔5個字元長度,靠右對齊。與後方的敘述間空2個空白格。
數字部分佔5個字元長度,靠右對齊。與後方的敘述間空2個空白格。
Sample Input
15 28 6 56 60000 22 496 0
Sample Output
PERFECTION OUTPUT 15 DEFICIENT 28 PERFECT 6 PERFECT 56 ABUNDANT 60000 ABUNDANT 22 DEFICIENT 496 PERFECT END OF OUTPUT
import java.util.Scanner;
public class UVA_382 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n;
System.out.println("PERFECTION OUTPUT");
while((n=sc.nextInt())!=0){
long total=0;
for(int i=1;i<n;i++){
if(n%i==0)
total+=i;
}
if(total==n)
System.out.printf("%5d PERFECT\n",n);
else if(total>n)
System.out.printf("%5d ABUNDANT\n",n);
else
System.out.printf("%5d DEFICIENT\n",n);
}
if(n==0)
System.out.println("END OF OUTPUT");
}
}
UVA 357: Let Me Count The Ways
Q357: Let Me Count The Ways
經過在百貨公司的一場血拼之後,小梅發現她身上的零錢共有17分(cent,美金貨幣單位,其他貨幣及面值請參考下方紅字部分),分別是1個dime,1個nickel,以及2個penny。隔天,小梅去便利商店買完東西後發現她身上的零錢恰好又是17分,這次是2個nickel及7個penny。小梅就在想,有幾種硬幣的組合可能湊成17分呢?經過仔細算算之後,發現共有6種。
你的問題就是:給一個金額,請你回答共有多少種硬幣組合的方式。
p.s 美國的零錢共有以下幾種硬幣以及其面值:
- penny, 1 cent
- nickel, 5 cents
- dime, 10 cents
- quarter, 25 cents
- half-dollar, 50 cents
Input
每組測試資料1列,有1個整數n(0 <= n <=30000),代表零錢的總金額。
Output
對每組測試資料請輸出共有多少種硬幣組合方式。輸出格式為以下2種其中之一。
- There are m ways to produce n cents change.
- There is only 1 way to produce n cents change.
Sample input
17
11
4
Sample Output
There are 6 ways to produce 17 cents change.
There are 4 ways to produce 11 cents change.
There is only 1 way to produce 4 cents change.
http://luckycat.kshs.kh.edu.tw/homework/q357.htm
import java.util.Scanner;
public class UVA_357 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int[] coin={1,5,10,25,50};
long[] m=new long[30001];
for(int i=0;i<30001;i++) //DP演算法
m[i]=0;
m[0]=1;
for(int i=0;i<5;i++){
for(int j=coin[i];j<=30000;j++){
m[j]+=m[j-coin[i]];
}
}
while(true){
int n=sc.nextInt();
/*for(int i=0;i<=n/50;i++){
for(int j=0;j<=(n-i*50)/25;j++){
for(int z=0;z<=(n-i*50-j*25)/10;z++){
for(int x=0;x<=(n-i*50-j*25-z*10)/5;x++){
m+=(n-i*50-j*25-z*10-x*5)>=0?1:0;
}
}
}
}*/
if(m[n]==1){
System.out.printf("There is only 1 way to produce %d cents change.\n",n);
}else{
System.out.println("There are "+m[n]+" ways to produce "+n+" cents change.");
}
}
}
}
UVA 356:Square Pegs And Round Holes
Q356:Square Pegs And Round Holes
在一個邊長為 2n 的正方形棋盤中央畫一個直徑為 2n-1 的圓,以下的圖為n=3
Input
輸入包含好幾組測試資料,每組一列,含有一個正整數 n(n<=150)
Output
對每一組測試資料,輸出2列,第一列為部分被包含在圓中的格子數。第二列為完全被包含在圓中的格子數。
請注意:各測試資料之間要空一列。請參考Sample output
Sample Iutput
3
4
4
Sample Output
In the case n = 3, 20 cells contain segments of the circle.
There are 12 cells completely contained in the circle.
In the case n = 4, 28 cells contain segments of the circle.
There are 24 cells completely contained in the circle.
There are 12 cells completely contained in the circle.
In the case n = 4, 28 cells contain segments of the circle.
There are 24 cells completely contained in the circle.
import java.util.Scanner;
public class UVA_356 {
static int n;
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
while((n=sc.nextInt())<=150){
System.out.println();
int cover=0;
int cross=0;
for(int i=0;i<=n*2;i++){
for(int j=0;j<=n*2;j++){ //上下左右四個點 (0,0)(0,1)(1,0)(0.0)
if(dist(i,j)<=n-0.5 && dist(i,j+1)<=n-0.5 && dist(i+1,j)<=n-0.5
&& dist(i+1,j+1)<=n-0.5)
cover++;
else if(dist(i,j)<=n-0.5 || dist(i,j+1)<=n-0.5 || dist(i+1,j)<=n-0.5
|| dist(i+1,j+1)<=n-0.5)
cross++;
}
}
System.out.printf("In the case n = %d, %d cells contain segments of the circle.\n",n,cross);
System.out.printf("There are %d cells completely contained in the circle.\n",cover);
}
}
private static double dist(int i,int j){
return Math.sqrt((n-i)*(n-i)+(n-j)*(n-j));
}
}
訂閱:
文章 (Atom)