2017年9月20日 星期三

2017年9月19日 星期二

JAVA 無限輸入的問題,題目沒有規定要輸入幾次

JAVA 無限輸入的問題,題目沒有規定要輸入幾次
___________________________________

遇到無限輸入的時候,輸入的數字時
Scanner s=new Scanner(System.in);
使用while(sc.hasNext()){}

如果輸入的是無限字串
Scanner s=new Scanner(System.in);
使用while(sc.hasNextLine()){}

2016年12月21日 星期三

UVA JAVA上傳前必須知道細節

以後要考CPE

所以寫來爽爽

可是英文太菜 需要看中文翻譯

如果要上傳到UVA要把檔名改成class Main

有傳上來的都是AC


2016/10/04 CPE 12694: Meeting Room Arrangement

題目英文:https://uva.onlinejudge.org/external/126/p12694.pdf
題目中文:http://luckycat.kshs.kh.edu.tw/homework/q12694.htm

import java.util.Scanner;

public class Q12694 {

public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
while(n-->0){
int a=-1,b=-1,i,j,num=0;
int[] s=new int[20];
int[] f=new int[20];
while(a!=0 || b!=0){
a=sc.nextInt();
b=sc.nextInt();
if(a!=0 || b!=0){
s[num]=a;
f[num]=b;
num++;
}
}
for(i=0;i<num;i++){
for(j=0;j<num;j++){
if(s[i]<s[j]){
swap(s,i,j);
swap(f,i,j);
}else if(s[i]==s[j]){
if(f[i]<f[j])
swap(f,i,j);
}
}
}

int max=-1,time=0;
for(i=0;i<num-1;i++){
int number=0;
time=f[i];
number++;
for(j=i+1;j<num;j++){
if(s[j]>=time){
time=f[j];
number++;
}
}
if(number>max){
max=number;
}
}
System.out.println(max);

}
}

static void swap(int[] arr,int i,int j){
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}

}

2016年12月20日 星期二

2016/10/04 CPE 11192: Group Reverse

Group reversing a string means reversing a string by groups. For example consider a string: “TOBENUMBERONEWEMEETAGAINANDAGAINUNDERBLUEICPCSKY” This string has length 48. We have divided into 8 groups of equal length and so the length of each group is 6. Now we can reverse each of these eight groups to get a new string: “UNEBOTNOREBMEEMEWENIAGATAGADNAEDNUNIIEULBRYKSCPC” Given the string and number of groups in it, your program will have to group reverse it. Input The input file contains at most 101 lines of inputs. Each line contains at integer G (G < 10) which denotes the number of groups followed by a string whose length is a multiple of G. The length of the string is not greater than 100. The string contains only alpha numerals. Input is terminated by a line containing a single zero. Output For each line of input produce one line of output which contains the group reversed string. Sample Input 3 ABCEHSHSH 5 FA0ETASINAHGRI0NATWON0QA0NARI0 0 Sample Output CBASHEHSH ATE0AFGHANISTAN0IRAQ0NOW0IRAN0

import java.util.Scanner;
public class Q11192 {

public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int G,i;
String str;
while((G=sc.nextInt())!=0){
str=sc.next();
StringBuilder sb=new StringBuilder("");
int temp=str.length()/G;                                //細節
for(i=0;i<str.length();i+=str.length()/G,temp+=str.length()/G){
sb.append(reverse(str,i,temp));
}
         System.out.println(sb);
}
}

static StringBuilder reverse(String str,int start,int end){
int i;
StringBuilder sb=new StringBuilder("");
for(i=end-1;i>=start;i--)
sb.append(str.charAt(i));
return sb;
}

}

英文:https://uva.onlinejudge.org/external/111/p11192.pdf
中文:http://luckycat.kshs.kh.edu.tw/homework/q11192.htm