2017年3月28日 星期二

Q496 : Simply Subsets

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

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
while (scn.hasNext()) {
String str1[] = scn.nextLine().split(" "), str2[] = scn.nextLine().split(" ");
int arr1[] = new int[str1.length], arr2[] = new int[str2.length];
for (int i = 0; i < arr1.length; i++)
arr1[i] = Integer.parseInt(str1[i]);
for (int i = 0; i < arr2.length; i++)
arr2[i] = Integer.parseInt(str2[i]);
Arrays.sort(arr1);
Arrays.sort(arr2);
if (arr1.length == arr2.length) { // 先判斷兩串列依樣長時
int i = 0;
int count = 0;
for (i = 0; i < arr1.length; i++) {
for (int j = 0; j < arr2.length; j++) {
if (arr1[i] == arr2[j]) {
count++;
}
}
}
if (count == arr1.length)
System.out.println("A equals B");
else if (count == 0)
System.out.println("A and B are disjoint");
else
System.out.println("I'm confused!");
} else if (arr1.length > arr2.length) {// 當arr1(A)長度比arr2(B)長時
int count = 0;
for (int i = 0; i < arr1.length; i++) {
for (int j = 0; j < arr2.length; j++) {
if (arr1[i] == arr2[j]) {
count++;
break;
}
}
}
if (count == arr2.length)
System.out.println("B is a proper subset of A");
else if (count > 0)
System.out.println("I'm confused!");
else
System.out.println("A and B are disjoint");
} else if (arr1.length < arr2.length) {// 當arr1(A)長度比arr2(B)短時
int count = 0;
for (int i = 0; i < arr2.length; i++) {
for (int j = 0; j < arr1.length; j++) {
if (arr2[i] == arr1[j]) {
count++;
break;
}
}
}
if (count == arr1.length)
System.out.println("A is a proper subset of B");
else if (count > 0)
System.out.println("I'm confused!");
else
System.out.println("A and B are disjoint");
}
}
}
/*
題目:Q496 : Simply Subsets
作者:1010
時間:西元 2017 年 3 月 28 日*/
}

這題只是數值比對有四種情況
1.A equals B (兩串列相等)
2.A is a proper subset of B(A是B的子集)
3.B is a proper subset of A(B是A的子集)
4.A and B are disjoint(兩串列完全不同,沒交集)
5.I'm confused!(有相等的數字但不構成子集,如{1,2} {2,3} 或是 {1,2,3} {3,4} )

這題建議不要使用uDebug的測資因為內有例外測資,簡單來說就是沒有重複數字的集合
The judge's input most likely does not contain any negative integers and sets like so
3 3 3 2
3 3 3 3 3 3 2
However, both cases are handled correctly by the code for this problem on uDebug.

Q482: Permutation Arrays

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

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) {
scn.nextLine();
String arr[] = scn.nextLine().split(" "), arr2[] = scn.nextLine().split(" "),
ans[] = new String[arr.length];
for (int i = 0; i < arr.length; i++) {
ans[Integer.parseInt(arr[i]) - 1] = arr2[i];
}
for (int i = 0; i < ans.length; i++) {
System.out.println(ans[i]);
}
if (n != 0)
System.out.println();
}
}
/*題目:Q482: Permutation Arrays
作者:1010
時間:西元 2017 年 3 月 28日*/
}

Permutation顧名思義就是要交換排列,第一行n是接下來有幾筆測資,每筆測資第一行有一行空白要讀,輸出最後一個數字記得多加一個換行,但最後依筆測資不換行(這題光排版就被擺了一道,不過題目有說明)
每筆次資中有兩行,其中第一行以例子來說3 1 2是代表第二行測資每個數字的擺放位置,最簡單的實作方法就是再新增一個陣列依序擺放這些交換的數字囉,記得索引值index是從0開始




Q993: Product of digits

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

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
int n;
while(cin >> n)
{
int b=9;
string ans="";
if(n==0)
{
cout << 0 << endl;
continue;
}
else if(n==1)
{
cout << 1 << endl;
continue;
}
while(b>1 && n>1)
{
if(n%b==0)
{
ans+=(b+'0');
n/=b;
}
else
{
b--;
}
}
vector<char> v;
for(int i=0; i<(int)ans.length(); i++)
{
v.push_back(ans[i]);
}
sort(v.begin(), v.end());
ans="";
for(int i=0; i<(int)v.size(); i++)
{
ans+=v[i];
}
if(ans=="" || n>1)
{
cout << -1 << endl;
}
else
{
cout << ans << endl;
}
}
return 0;
}
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(),i=9,tot=1,temp=num;
StringBuilder str=new StringBuilder("");
if(num==1){
System.out.println("1");
continue;
}else if(num==0){
System.out.println("0");
continue;
}
while(i!=1){
if(num%i==0){
str.append(i);
tot*=i;
num/=i;
}else{
i--;
}
}
if(temp==tot)
System.out.println(str.reverse());
else
System.out.println("-1");
}
}
/*題目:Q993: Product of digits
作者:1010
時間:西元 2017 年 3 月 28日*/
}

這題其實不難,我當初想太複雜還用質數求公因數,但其實不是這樣的,題目只是要每個位數相乘等於你輸入的數目
舉例:90=>2*5*9 所以輸出259就好啦~很簡單吧
至於我是怎麼實作的,首先建立一個StringBuilder他類似於String型態但她可以呼叫reverse()字串翻轉很方便,那我們就從9開始除囉直到不等於1,若能整除依序append()到字串變數中,最後輸出reverse()就是最小數啦,那還要檢查最後如果每個位數相乘不等於輸入的數就輸出-1囉!
還有還有0跟1要個別判斷哦

2017年3月27日 星期一

Q11349: Symmetric Matrix

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

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int t=Integer.parseInt(scn.nextLine()),count=1;;
while(t--!=0){
scn.next();scn.next();
int n=scn.nextInt();
String arr[]=new String [n*n];
Boolean b=true;
for(int i=0;i<n*n;i++)
arr[i]=scn.next();
for(int i=0,j=arr.length-1;i<n*n;i++,j--){
if(!arr[i].equals(arr[j])||arr[i].contains("-")){
b=false;
break;
}
}
if(b)
System.out.printf("Test #%d: Symmetric.\n",count++);
else
System.out.printf("Test #%d: Non-symmetric.\n",count++);
}
}
/*題目:Q11349:- Symmetric Matrix
作者:1010
時間:西元 2017 年 3 月 */
}

這題主要是要搞清楚對稱矩陣(Symmetric matrix)的特性以下是名詞解釋

若矩陣A與其對換矩陣AT相等,則A稱為對稱矩陣,對稱矩陣恆為一方陣。   對換矩陣AT是依序交換A中行與列後所得矩陣,亦即:(AT)ij=Aji(對所有i與j而言)

寫了這麼多看不懂的話聽我解釋~簡單來說低一個數字跟最後個數字要一樣第二個數字要跟倒數第二個數字一樣
我們再把二維陣列概念想成簡單的一維去時做就行了
記註記住對稱矩陣內不能有負數


Q1644: Prime Gap

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

/* package whatever; // don't place package name! */
import java.util.*;
class Main
{
public static void main (String[] args)
{
ArrayList<Integer> primeTable = new ArrayList<Integer>();
primeTable.add(2);
for(int i = 3;i<1000000 ; i += 2)
{
boolean check = true;
for(int j = 3; j * j <= i; j += 2)
{
if(i % j == 0)
{
check=false;
break;
}
}
if(check)
primeTable.add(i);
}
for(int i = 0; i < primeTable.size(); ++i)
System.out.println(primeTable.get(i));
}
/*題目:PrimeTable質數建表
作者:1010
時間:西元 2017 年 3 月 */
}
view raw PrimeTable.java hosted with ❤ by GitHub
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
ArrayList<Integer> primeTable = new ArrayList<Integer>();
primeTable.add(2);
for(int i = 3;i<=1299709 ; i += 2)
{
boolean check = true;
for(int j = 3; j * j <= i; j += 2)
{
if(i % j == 0)
{
check=false;
break;
}
}
if(check)
primeTable.add(i);
}
while(true){
int num=scn.nextInt(),i;
Boolean b=true;
if(num==0)
break;
for(i=0;i<primeTable.size();i++){
if(primeTable.get(i)==num){
b=false;
break;
}
else if(primeTable.get(i)>num){
break;
}
}
if(b){
int count=0;
for(int j=primeTable.get(i-1);j<primeTable.get(i);j++)
count++;
System.out.println(count);
}else{
System.out.println("0");
}
}
}
/*題目:Q1644: Prime Gap
作者:1010
時間:西元 2017 年 3 月 */
}
這題是2016/12/10CPE第四題,並不難主要是要先建立質數表才不會LTE
至於怎麼快速建表呢?簡單來說質數一定是奇數,不多說下面有質數建表範例

這題主要是找尋該數在質數的Gap當中有幾個非質數數目,若本身為質數就輸出零

[C_AR118-易] 硬幣組合

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

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int arr1[]=new int[10],arr2[]=new int [10];
Set<Integer> set = new TreeSet<Integer>();
for(int i=0;i<10;i++)
arr1[i]=scn.nextInt();
for(int i=0;i<10;i++)
arr2[i]=scn.nextInt();
for(int i=0;i<10;i++){
for(int j=0;j<10;j++){
set.add(arr1[i]+arr2[j]);
}
}
ArrayList<Integer>list=new ArrayList<Integer>(set);
for(int i=0;i<list.size();i++){
if((i)%10==0&&i!=0)
System.out.println();
if((i)%10!=0)
System.out.print(" ");
System.out.print(list.get(i));
}
System.out.println();
}
/*題目:[C_AR118-易] 硬幣組合方法一
作者:1010
時間:西元 2017 年 3 月 */
}
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int c, count = 0, num1[] = new int[10], num2[] = new int[10], ans[] = new int[100];
for (int i = 0; i < 10; i++)
num1[i] = scn.nextInt();
for (int i = 0; i < 10; i++)
num2[i] = scn.nextInt();
Arrays.sort(num1);
Arrays.sort(num2);
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
ans[i * 10 + j] = num1[i] + num2[j];
}
}
Arrays.sort(ans);
boolean b = false;
for (int i = 0; i < 100; i++) {
if (count == 10 && b) {
System.out.println();
count = 0;
}
for (c = i + 1; c < 100; c++)
if (ans[i] == ans[c])
break;
if (c == 100) {
if (count != 0)
System.out.print(" ");
System.out.print(ans[i]);
count++;
b = true;
} else
b = false;
}
System.out.println();
}
/*題目:[C_AR118-易] 硬幣組合方法二
作者:1010
時間:西元 2017 年 3 月 */
}
這題的組合簡單來說就是兩個串列中各挑一個數字相加,那他的時間複雜度就是O(10*10)
用雙迴圈就能算出所有組合了,但這邊要記住相加結果重複就不用再印出來了
Java有個很好用的容器較TreeSet,他不但可以做排序而且若遇到相同的數值會直接合併(捨去),簡單來說TreeSet是個集合它裡面幫你做好排序而且所有值不會重複具有唯一性
至於要怎麼把集合Set的值呼叫出來,這時就需要一個容器ArrayList的方式儲存集合裡的值才能去取得集合裡的資料
最後注意輸出是每十個為一行,每行最後一個不空白,最後結束還要換行!
ps.至於第二種寫法是大二時寫的單純只用到陣列輸出時再逐一判斷是否有重複(非常浪費時間,屬於笨方法)


2017年3月15日 星期三

Q11461:Square Numbers

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

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
ArrayList<Integer> list=new ArrayList<Integer>();
int i=1,num=1;
while(num<=100000){
list.add(num);
i++;
num=i*i;
}
while(true){
int a=scn.nextInt(),b=scn.nextInt(),count=0;
if(a==0&&b==0)
break;
if(a==b)
b++;
for(int k=0;k<list.size();k++){
if(list.get(k)>=a&&list.get(k)<=b)
count++;
}
System.out.println(count);
}
}
/*題目:Q11461:Square Numbers
作者:1010
時間:西元 2017 年 3 月 */
}

這題用ArrayList先建表,計算輸入a、b序列中有幾個是符合平方的數