2016年10月15日 星期六

[ST22-易] Binary Conversion

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?a=975

import java.util.Scanner;
public class Main {
public static void main(String args[]) {
Scanner scn=new Scanner (System.in);
int n=Integer.parseInt(scn.nextLine());
while(n--!=0){
String s[]=scn.nextLine().split(" ");
System.out.println(Integer.parseInt(s[0],2)+Integer.parseInt(s[1],2));
}
}
/*題目:[ST22-易] Binary Conversion
作者:1010
時間:西元 2016 年 10 月 */
}
這題二進為字串轉時進位數字直接利用Integer.parseInt( " 二進為字串 " , 2 );

2016年10月13日 星期四

[C_AR190-易] 矩陣元素共同交集

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=29925

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int a[]=new int [4];
ArrayList<Integer>list=new ArrayList<Integer>();
for(int i=0;i<4;i++)
a[i]=scn.nextInt();
for(int i=0;i<4;i++){
int num=scn.nextInt();
for(int j=0;j<4;j++){
if(num==a[j])
list.add(num);
}
}
Collections.sort(list);
for(int i=0;i<list.size();i++){
if(i!=0)
System.out.print(" ");
System.out.print(list.get(i));
}
System.out.println();
}
/*題目:[[C_AR190-易] 矩陣元素共同交集
作者:1010
時間:西元 2016 年 10 月 */
}
這題就依序去尋找看看有沒交集的數,並丟入ArrayList中(因為最後要排序且不知容器有多大)
排序的語法是Collections.sort( )
注意在ITSA中ArrayList並不能簡略打法要遵循
ArrayList<Integer> list = new ArrayList<Integer> =>不能<>

2016年10月12日 星期三

[C_SO07-] 找出不合群的人

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=2277

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
String s[]=scn.nextLine().split(" ");
double arr[]=new double [s.length];
Boolean b=true;
for(int i=0;i<s.length;i++){
arr[i]=Double.parseDouble(s[i]);
}
Arrays.sort(arr);
int length=s.length-1;
double fs=arr[(int)(length*0.75)]-arr[(int)(length*0.25)],out_one=arr[(int)(length*0.25)]-1.5*fs,out_two=arr[(int)(length*0.75)]+1.5*fs;
for(int i=0;i<s.length;i++){
if(arr[i]>out_two||arr[i]<out_one){
b=false;
System.out.printf("The outlier is %.1f\n",arr[i]);
}
}//System.out.println(fs+" "+out_one+" "+out_two);
if(b)
System.out.println("There is no outlier.");
}
/*題目:[C_SO07-] 找出不合群的人
作者:1010
時間:西元 2016 年 10 月 */
}
這題就按照題意找出outlier,注意題目範例輸出第二個有誤事排序後去比較另外每個資料只空一行而已
Q1的位置= n × 0.25
Q2的位置= n × 0.5
Q3的位置= n × 0.75

Input:
7.0 4.0 5.0 4.0 100.0 2.0 8.0 9.0 -100.0
Output:
The outlier is -100.0

The outlier is 100.0

[C_AR80-中] 聖經密碼

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=7051

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
char arr[]="ddaazadassdchnrmloqzfhkujqwfvxhf,l.f124sae355d9fgd121cz6aefgb2366b6hhcqr75z8i9hb rstyuvwxsjlvakmn0op".toCharArray();
String s[]=scn.nextLine().split(",");
for(int i=0;i<s.length;i+=2){
int num=Integer.parseInt(s[i])*10+Integer.parseInt(s[i+1]);
System.out.print(arr[num]);
}
System.out.println();
}
/*題目:[C_AR80-中] 聖經密碼
作者:1010
時間:西元 2016 年 10 月 */
}
這題就把二維陣列轉乘一維方式讀取,作法是先把所有字串放入字元陣列中,再來用字串切割把讀進來的數字字串split(" , "),最後迴圈兩兩一組 i*10+(i+1) 就可以調出一開始創的陣列字元了
A[i,j] =  (i × n )j

[C_AR83-中] 字元出現次數

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=7898

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
while(scn.hasNext()){
char s[]=scn.nextLine().toCharArray(),c=scn.nextLine().toCharArray()[0];
int arr[]=new int[200];
if(s[0]=='e'&&s[1]=='n'&&s[2]=='d')
break;
for(int i=0;i<s.length;i++){
arr[s[i]]++;
}
System.out.println(arr[c]);
}
}
/*題目:[C_AR83-中] 字元出現次數
作者:1010
時間:西元 2016 年 10 月 */
}
這題我是用快速寫法缺點是造成稀疏矩陣浪費記憶體,簡單來說就是利用ASCii來當每個字母索引值(大小寫分開)
題目給的測資有空白行別管他

[C_AR63-易] 矩陣相加

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=3535

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int a=scn.nextInt(),b=scn.nextInt(),arr[][]=new int [a][b];
for(int i=0;i<a;i++){
for(int j=0;j<b;j++){
arr[i][j]=scn.nextInt();
}
}
int c=scn.nextInt(),d=scn.nextInt();
for(int i=0;i<c;i++){
for(int j=0;j<d;j++){
int num=scn.nextInt();
if(j!=0)
System.out.print(" ");
System.out.print(num+arr[i][j]);
}
System.out.println();
}
}
/*題目:[C_AR63-易] 矩陣相加
作者:1010
時間:西元 2016 年 10 月 */
}
這題很簡單就只是單純的矩陣相加

2016年10月11日 星期二

Q12195 : Jingle Composing

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3347

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
while(scn.hasNext()){
String arr[]=scn.nextLine().split("/");
int count=0;
if(arr[0].equals("*"))
break;
for(int i=0;i<arr.length;i++){
double num=0.0;
char c[]=arr[i].toCharArray();
for(int j=0;j<c.length;j++){
if(c[j]=='W')
num+=1;
else if(c[j]=='H')
num+=0.5;
else if(c[j]=='Q')
num+=1/4.0;
else if(c[j]=='E')
num+=1/8.0;
else if(c[j]=='S')
num+=1/16.0;
else if(c[j]=='T')
num+=1/32.0;
else if(c[j]=='X')
num+=1/64.0;
}
if(num==1.0)
count++;
}
System.out.println(count);
}
}
/*題目:Q12195 : Jingle Composing
作者:1010
時間:西元 2016 年 10 月 */
}
這題就利用判斷式把每節內容相加最後看是不是1就好了

[C_ST62-易] 縮寫很方便

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?a=6419

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
while(scn.hasNext()){
String arr[]=scn.nextLine().split(" ");
for(int i=0;i<arr.length;i++){
char ary[]=arr[i].toCharArray();
if(ary[0]<97){
System.out.print(ary[0]);
}
}
System.out.println();
}
}
/*題目:[C_ST62-易] 縮寫很方便
作者:1010
時間:西元 2016 年 10 月 */
}
這題很簡單直接字串分割最後再依序判斷第一個字母是否為ASCii小於於97=>a,若是的話印出

Q12694: Meeting Room Arrangement

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4432

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scn =new Scanner(System.in);
int n=scn.nextInt();
while(n--!=0){
int arr[][]=new int [21][2],length=0;
while(true){
arr[length][0]=scn.nextInt();
arr[length][1]=scn.nextInt();
if(arr[length][0]+arr[length++][1]==0){
length--;
break;
}
}
for(int i=0;i<length-1;i++){
for(int j=i+1;j<length;j++){
if(arr[i][1]>arr[j][1]){
int temp=arr[i][1];
arr[i][1]=arr[j][1];
arr[j][1]=temp;
temp=arr[i][0];
arr[i][0]=arr[j][0];
arr[j][0]=temp;
}
else if(arr[j][1]==arr[i][1]){
if(arr[i][0]>arr[j][0]){
int temp=arr[i][1];
arr[i][1]=arr[j][1];
arr[j][1]=temp;
temp=arr[i][0];
arr[i][0]=arr[j][0];
arr[j][0]=temp;
}
}
}
}
int t=0,count=0;
for(int i=0;i<length;i++){
if(t<=arr[i][0]){
t=arr[i][1];
count++;
}
}
System.out.println(count);
}
}
/*題目:Q12694 Meeting Room Arrangement 二維陣列列方法
作者:1010
時間:西元 2016 年 10 月 */
}
import java.util.Scanner;
class Pair{
private int L;
private int R;
public Pair(int left,int right){
this.L=left;this.R=right;
}
public void change(int left,int right){
this.L=left;this.R=right;
}
public int getL(){
return L;
}
public int getR(){
return R;
}
}
public class Main {
public static void main(String[] args) {
Scanner scn =new Scanner(System.in);
int n=scn.nextInt();
while(n--!=0){
Pair arr[]=new Pair [21];
int length=0;
while(true){
arr[length] = new Pair(scn.nextInt(),scn.nextInt());
if(arr[length].getL()+arr[length++].getR()==0){
length--;
break;
}
}
for(int i=0;i<length-1;i++){
for(int j=i+1;j<length;j++){
if(arr[i].getR()>arr[j].getR()){
Pair temp=new Pair(arr[i].getL(),arr[i].getR());
arr[i]=new Pair(arr[j].getL(),arr[j].getR());
arr[j]=temp;
}
else if(arr[i].getR()==arr[j].getR()){
if(arr[i].getL()>arr[j].getL()){
Pair temp=new Pair(arr[i].getL(),arr[i].getR());
arr[i]=new Pair(arr[j].getL(),arr[j].getR());
arr[j]=temp;
}
}
}
}
int t=0,count=0;
for(int i=0;i<length;i++){
if(t<=arr[i].getL()){
t=arr[i].getR();
count++;
}
}
System.out.println(count);
}
}
/*題目:Q12694 Meeting Room Arrangement 結構化方法
作者:1010
時間:西元 2016 年 10 月 */
}
2016 10/05 CPE第五題 這題的方法是先以結束時間為基準做排序(氣泡排序),再來進入迴圈一個一個去比開始時間有沒有比t大,若有t改成結束時間count++
第一個打法式二維陣列,第二種是結構化打法

2016年10月10日 星期一

[C_AR191-易] 巴斯卡三角形

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?a=15389

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n=Integer.parseInt(scn.nextLine());
for(int i=0;i<n;i++){
for(int j=0;j<=i;j++){
int p=1;
for(int k=1;k<=j;k++)
p=p*(i-k+1)/k;
System.out.print(p);
}
System.out.println();
}
}
/*題目:[C_AR191-易] 巴斯卡三角形
作者:1010
時間:西元 2016 年 10 月 */
}
帕斯卡三角形就是算排列組合C幾取幾,利用公式算出每個位置數字

Q:10415 - Eb Alto Saxophone Player

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1356

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n = Integer.parseInt(scn.nextLine());
String arr[] = { "0111001111", "0111001110", "0111001100", "0111001000", "0111000000", "0110000000",
"0100000000", "0010000000", "1111001110", "1111001100", "1111001000", "1111000000", "1110000000",
"1100000000" };
int hash[] = new int[200];
hash['c'] = 0;
hash['d'] = 1;
hash['e'] = 2;
hash['f'] = 3;
hash['g'] = 4;
hash['a'] = 5;
hash['b'] = 6;
hash['C'] = 7;
hash['D'] = 8;
hash['E'] = 9;
hash['F'] = 10;
hash['G'] = 11;
hash['A'] = 12;
hash['B'] = 13;
while (n-- != 0) {
int ans[] = new int[10], state[] = new int[10];
char c[] = scn.nextLine().toCharArray();
for (int i = 0; i < c.length; i++) {
char ary[] = arr[hash[c[i]]].toCharArray();
for (int j = 0; j < 10; j++) {
if (ary[j] == '1' && state[j] == 0) {
ans[j]++;
state[j] = 1;
}
if (ary[j] == '0')
state[j] = 0;
}
}
for (int i = 0; i < 10; i++) {
if (i != 0)
System.out.print(" ");
System.out.print(ans[i]);
}
System.out.println();
}
}
/*題目:Q:10415 - Eb Alto Saxophone Player
作者:1010
時間:西元 2016 年 10 月 */
}
這題是2016 10/06 CPE題目 我的解法是先把14個按鍵方法先存入陣列中(arr)再建立一個整數陣列把按鈕轉換成配對arr的索引值 例如hash['c']=0(c紐就配對arr[0])
最後就是一個字一個字讀取並且依序檢查是否有按過 案過之後state[]=1反之
if (ary[j] == '1' && state[j] == 0) {
ans[j]++;
state[j] = 1;
}
if (ary[j] == '0')

state[j] = 0;

2016年10月9日 星期日

Q:11192 - Group Reverse

https://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=2133

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
while(scn.hasNext()){
int n=scn.nextInt();
if(n==0)break;
char arr[]=scn.nextLine().trim().toCharArray();
for(int i=arr.length/n-1;i<arr.length;i+=arr.length/n){
for(int j=0;j<arr.length/n;j++){
System.out.print(arr[i-j]);
}
}System.out.println();
}
}
/*題目:Q:11192 - Group Reverse
作者:1010
時間:西元 2016 年 10 月 */
}
這題是2016 10/04 CPE第一題,第一個數字是要把字串切割成幾等分,內迴圈從後面讀取印出來

[C_CH13-中] 坑洞路面

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=2078

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n=scn.nextInt(),arr[]=new int [n],arr_large[]=new int [n],ans=0,max=0;
for(int i=0;i<n;i++){
arr[i]=scn.nextInt();
if(arr[i]>max)
max=arr[i];
arr_large[i]=max;
}
max=0;
for(int i=n-1;i>=0;i--){
if(arr[i]>max)
max=arr[i];
int num=Math.min(max,arr_large[i])-arr[i];
if(num<0)
num=0;
ans+=num;
}
System.out.println(ans);
}
/*題目:[C_CH13-中] 坑洞路面
作者:1010
時間:西元 2016 年 10 月 */
}
這題先依序找出最大值放入陣列arr_large,另外再從尾找出最大值 和arr_large比看誰最小減去目前的坑洞高
若結果小於0就不做相加到ans反之

Q:11344 - The Huge One

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2319

import java.math.BigInteger;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n=Integer.parseInt(scn.nextLine());
while(n--!=0){
BigInteger num=new BigInteger(scn.nextLine());
boolean b=true;
String s[]=scn.nextLine().split(" ");
for(int i=1;i<s.length;i++){
if(num.equals(new BigInteger("0")))
break;
if(!(num.mod(new BigInteger(s[i])).equals(new BigInteger("0")))){
b=false;
break;
}
}
if(b)
System.out.println(num+" - "+"Wonderful.");
else
System.out.println(num+" - "+"Simple.");
}
}
/*題目:Q:11344 - The Huge One
作者:1010
時間:西元 2016 年 10 月 */
}
這題是2016 10/04 CPE第四題 利用大數去判斷是有整除就好了
另外除數是0時直接跳出迴圈Wonder
ful  num.equals(new BigInteger("0"))  這裡也可以用compareTo去比對
還有每組資料第二行的第一個數字是接下來有多少個數字並不是被除數

2016年10月8日 星期六

[C_SO15-易] 逆序與奇/偶排列

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=8204

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int arr[]= new int [4],count=0;
for(int i=0;i<4;i++)
arr[i]=scn.nextInt();
Arrays.sort(arr);
for(int i=0;i<4;i++){
for(int j=i+1;j<4;j++){
if(arr[i]<arr[j])
count++;
}
}
if(count%2==0&&count!=0)
System.out.println("0");
else
System.out.println("1");
}
/*題目:[C_SO15-易] 逆序與奇/偶排列
作者:1010
時間:西元 2016 年 10 月 */
}
這題必須先排序後再做比對逆序數量

[C_AR143-易] 轉換英文字母到數字

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=24061

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
char arr[]=scn.next().toCharArray();
for(int i=0;i<arr.length;i++){
System.out.printf("(%d)",arr[i]-'a');
}
System.out.println();
}
/*題目:[C_AR143-易] 轉換英文字母到數字
作者:1010
時間:西元 2016 年 10 月 */
}
這題非常簡單把字串拆開成字元陣列再把每個字元-'a'就是答案要的數字了

[C_AR142-易] 陣列存多少?

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=24058

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int arr[]=new int [100];
for(int i=1;i<=100;i++)
arr[i-1]=i*i;
int m=scn.nextInt(),n=scn.nextInt(),tot=0;
for(int i=0;i<m;i++){
if(arr[i]%n==0)
tot+=arr[i];
}
System.out.println(tot);
}
/*題目:[C_AR142-易] 陣列存多少?
作者:1010
時間:西元 2016 年 10 月 */
}
為了避免超時利用動態規劃先把1~100次方建立起來之後再去判斷相加

ITSA 第46次月賽 Problem 4 貧富不均

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=29454

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n=scn.nextInt();
while(n--!=0){
int k=scn.nextInt(),tot=0,arr[]=new int [k],p=0;
for(int i=0;i<k;i++)
arr[i]=scn.nextInt();
Arrays.sort(arr);
tot=arr[k-1]*40/100;
for(int i=k-2,j=1;i>=0;i--,j++){
if(arr[i]!=arr[i+1])
p=(int)(Math.ceil((double)j/k*100.0));
if (p < 11)
tot += arr[i] * 40 / 100;
else if (p < 31)
tot += arr[i] * 30 / 100;
else if (p < 61)
tot += arr[i] * 20 / 100;
else if (p < 81)
tot += arr[i] * 10 / 100;
}
System.out.println(tot);
}
}
/*題目:ITSA 第46次月賽 Problem 4 貧富不均
作者:1010
時間:西元 2016 年 10 月 */
}
這題用由大至小排序後做比較好做,無條件進位要用ceil方法不過要強制轉型成(int)內部數字要浮點數

ITSA 第48次月賽 [Problem 4] 字串轉換成浮點數

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=31398

import java.math.BigDecimal;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n=Integer.parseInt(scn.nextLine());
while(n--!=0){
String s[]=scn.nextLine().split(" "),str="";
for(int i=0;i<s.length;i++)
str+=s[i];
BigDecimal num=new BigDecimal(str);
System.out.println(num.toPlainString());
}
}
/*題目:ITSA 第48次月賽 [Problem 4] 字串轉換成浮點數
作者:1010
時間:西元 2016 年 10 月 */
}
這題可能有空白所以要先把所有空白去掉,最後放入BigDecimal再toPlainString把科學記號的完整數字印出來

2016年10月7日 星期五

ITSA 第49次月賽 Problem2. 時間表示轉換

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?a=17017

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n=scn.nextInt();
while(n--!=0){
int num=scn.nextInt(),a=0,b=0,c=0;
System.out.print(num+"=");
a=num/86400;
num%=86400;
b=num/3600;
num%=3600;
c=num/60;
num%=60;
System.out.printf("%02d,%02d,%02d,%02d\n",a,b,c,num);
}
}
/*題目:ITSA 第49次月賽 Problem2. 時間表示轉換
作者:1010
時間:西元 2016 年 10 月 */
}
這題很簡單一天有86400秒一小時3600秒一分鐘60秒下去除
最後印出%02d 就能顯示數位數字了

ITSA 第49次月賽 Problem4. 跳舞的小人

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=32660

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n=Integer.parseInt(scn.nextLine());
while(n--!=0){
String s[]=scn.nextLine().toUpperCase().split(" ");
StringBuilder str=new StringBuilder("");
int num=0;
for(int i=s.length-1;i>=0;i--){
char c=s[i].toCharArray()[0];
if((c-'0')<=9)
num=Integer.parseInt(s[i]);
else{
char tot=(char)(c+num);
if(tot>'Z')
tot=(char)(tot%'Z'+'A'-1);
str.append(tot);
}
}
System.out.println(str.reverse());
}
}
/*題目:ITSA49次月賽 Problem4. 跳舞的小人
作者:1010
時間:西元 2016 年 10 月 */
}
這題我的作法是從尾巴讀取是否有遇到數字若有把數字存起來反之加上位移數(ps.數字也可能2位數)
這題還要考慮字母溢位 所以必須
if(tot > 'Z'){
     tot=tot % 'Z' + 'A' -1 ;
}
因為是從最後開始讀取所以先append進字串,最後再reverse出來

範例測資
input
1
A B C 13 D E 2 X Y Z 20
output

NOPFGRST

[C_MM148-易] 三位數字摸彩

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=7022

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
String s[]=scn.next().split(",");
int arr[]=new int [s.length];
for(int i=0;i<arr.length;i++)
arr[i]=Integer.parseInt(s[i]);
Arrays.sort(arr);
for(int i=arr.length-1;i>=arr.length-4;i--){
for(int j=i-1;j>=1;j--){
for(int k=j-1;k>=0;k--){
System.out.printf("%d%d%d\n",arr[i],arr[j],arr[k]);
}
}
}
}
/*題目:[C_MM148-易] 三位數字摸彩
作者:1010
時間:西元 2016 年 10 月 */
}
這題先讀入字串再利用split做字串切割數字,最後再放入int型態的陣列做sort排序
最後利用迴圈印出後3個最大數(3個迴圈)

[C_MM106-易] 求質數問題

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?a=1292

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n=scn.nextInt(),j;
for(int i=n;i>=2;i--){
for(j=2;j<i;j++){
if(i%j==0)
break;
}
if(i==j){
System.out.println(i);
break;
}
}
}
/*題目:C_MM106-易 求質數問題
作者:1010
時間:西元 2016 年 10 月 */
}
檢查質數最簡單的方法就是從2~n-1一個一個去判斷是否整除,題目是求範圍內最大的質數那我們就依序遞減判斷

2016年10月6日 星期四

Q11689: Soda Surpler

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2736

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n=scn.nextInt();
while(n--!=0){
int e=scn.nextInt(),f=scn.nextInt(),c=scn.nextInt(),num=e+f,tot=0;
while(true){
if(num<c)
break;
tot+=num/c;
num=num/c+num%c;
}
System.out.println(tot);
}
}
/*題目:Q11689: Soda Surpler
作者:1010
時間:西元 2016 年 10 月 */
}
這題就是簡單的利用/和%來計算換取的飲料,當不能再換時num<c就跳出迴圈

2016年10月5日 星期三

Q10302: Summation of Polynomials

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1243

import java.math.BigInteger;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
BigInteger arr[]=new BigInteger[50000];
for(int i=1;i<=50000;i++){
BigInteger x=new BigInteger(Integer.toString(i));
arr[i-1]=x.pow(3);
}
while(scn.hasNext()){
int n=scn.nextInt();
BigInteger tot=new BigInteger("0");
for(int i=0;i<n;i++)
tot=tot.add(arr[i]);
System.out.println(tot);
}
}
/*題目:Q10302: Summation of Polynomials
作者:1010
時間:西元 2016 年 10 月 */
}
這題其實用long就可以了這題有兩種解法
第一種是動態規劃先把5000內所有立方算出後來再相加
第二種解法是利用公式解 (x*x*(x+1)*(x+1))/4 

Q10365: Blocks

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1306

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n=scn.nextInt();
while(n--!=0){
int num=scn.nextInt(),min=Integer.MAX_VALUE;
for(int i=1;i<=num;i++){
if(num%i==0){
for(int j=i;j<=num/i;j++){
if(num%(i*j)==0){
int k=num/i/j;
if(k<j)
break;
else{
int tot=(i*j+j*k+k*i)*2;
if(tot<min)
min=tot;
}
}
}
}
}
System.out.println(min);
}
}
/*題目:Q10365: Blocks
作者:1010
時間:西元 2016 年 10 月 */
}
這題就是把體積拆成長*寬*高(i*j*k)
體積計算面積的公式(i*j+j*k+k*i)*2

2016年10月4日 星期二

ITSA 第49次月賽 Problem5.Finding a Maximum Profit Interval for a Long Term Investment

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=32661

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scn=new Scanner(System.in);
int n=scn.nextInt();
while(n--!=0){
int j=scn.nextInt(),tot=0,max=0;
for(int i=0;i<j;i++){
int num=scn.nextInt();
if(tot<0)
tot=num;
else
tot+=num;
if(tot>max)
max=tot;
}
System.out.println(max);
}
}
/*
題目:Problem5. Finding a Maximum Profit Interval for a Long Term Investment
作者:1010
時間:西元 2016 年 10 月 */
}
這題就是貪婪法則取最大值每次相加做比對,當tot遇到負數時直接tot=num重新開始連加

2016年10月3日 星期一

Q490: Rotating Sentences

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=431

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scn=new Scanner (System.in);
char arr[][]=new char[100][100];
int count=0,max=0;
while(scn.hasNext()){
String str=scn.nextLine();
arr[count++]=str.toCharArray();
if(str.length()>max)
max=str.length();
}
for(int i=0;i<max;i++){
for(int j=count-1;j>=0;j--){
if(arr[j].length<=i)
System.out.print(" ");
else
System.out.print(arr[j][i]);
}
System.out.println();
}
}
/*題目:Q490: Rotating Sentences
作者:1010
時間:西元 2016 年 10 月 */
}

這題純粹是字串翻轉不過需要一些小技巧才會AC,簡單來說就是把每個字串讀入二維陣列,每次存入之前檢查字數是否為該行最大值,最後翻轉印出來,這裡要確認當該列數小於最大值時要印出空白不然會拋出例外

2016年10月1日 星期六

Q855: Lunch in Grid City

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=796

import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scn=new Scanner (System.in);
int n=scn.nextInt();
while(n--!=0){
int s=scn.nextInt(),a=scn.nextInt(),f=scn.nextInt();
int x[]=new int[f],y[]=new int [f];
for(int i=0;i<f;i++){
x[i]=scn.nextInt();
y[i]=scn.nextInt();
}
Arrays.sort(x);
Arrays.sort(y);
System.out.printf("(Street: %d, Avenue: %d)\n",x[(f-1)/2], y[(f-1)/2]);
}
}
/*
題目:Q11875 - Brick Game
作者:1010
時間:西元 2016 年 10 月 */
}
這題就取兩個排序後的中位數就好了

[C_MM24-易] 計算薪水

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=6922

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scn=new Scanner (System.in);
while(scn.hasNext()){
int time=scn.nextInt(),salary=scn.nextInt();
double tot=0;
if(time<=60){
tot=time*salary;
}
else if(time>60&&time<=120){
tot=60*salary+time*(time-60)*1.33;
}
else
tot=salary*60+salary*60*1.33+(time-120)*salary*1.66;
System.out.printf("%.1f\n",tot);
}
}
/*
題目:Q11494: Queen
作者:1010
時間:西元 2016 年 10 月 */
}
這題跟計算停車費類似