藍橋杯題庫 算法提高非vip部分(C++、Java)代碼實現(201-250)

將定期更新藍橋杯習題集的解題報告~

ADV-205 拿糖果

cpp:

#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
int main() {
    int n, sum = 0;
    cin >> n;
    if (n % 2)
        cout << (n - 3) / 2 << endl;
    else
        cout << n / 2 << endl;
    return 0;
}

java:

import java.util.ArrayList;
import java.util.Scanner;

public class Main {
	public static boolean judgePrime(int n) {
        if(n == 2)
            return true;
        for(int i = 2;i <= n;i++) {
            if(n % i == 0)
                return false;
            if(i > n / 2)
                break;
        }
        return true;
    }
    //獲取n的平方根以內的所有質因數
    public static ArrayList<Integer> getPrime(int n) {
        ArrayList<Integer> list = new ArrayList<Integer>();
        n = (int) Math.sqrt(n);
        for(int i = 2;i <= n;i++) {
            if(judgePrime(i)) {
                    list.add(i);
            }
        }
        return list;
    }
    
    public static void printResult(int n) {
        int[] dp = new int[100005];
        ArrayList<Integer> list = getPrime(100005);
        int len = list.size();
        int judge, prime;
        for(int i = 1;i <= n;i++) {
            judge = (int) Math.sqrt(i);
            for(int j = 0;j < len;j++) {
                prime = list.get(j);
                if(prime > judge)
                    break;
                if(judge % prime == 0) {
                    if(dp[i] < dp[i - prime * 2] + prime)
                        dp[i] = dp[i - prime * 2] + prime;
                }
            }
        }
        System.out.println(dp[n]);
        return;
    }
    
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        printResult(n);
    }
}

ADV-208 矩陣相乘

cpp:

#include <stdio.h>
const int Max = 100;
long long a[Max][Max];
long long b[Max][Max];
int main() {
    int a1, a2, b1, b2, i, j, k, temp;
    scanf("%d %d", &a1, &a2);
    for (i = 0; i < a1; i++) {
        for (j = 0; j < a2; j++) {
            scanf("%ld", &a[i][j]);
        }
    }
    scanf("%d %d", &b1, &b2);
    for (i = 0; i < b1; i++) {
        for (j = 0; j < b2; j++) {
            scanf("%ld", &b[i][j]);
        }
    }
    for (i = 0; i < a1; i++) {
        for (j = 0; j < b2; j++) {
            temp = 0;
            for (k = 0; k < a2; k++) {
                temp += a[i][k] * b[k][j];
            }
            printf("%d ", temp);
        }
        printf("\n");
    }
    return 0;
}

java:

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int m = scanner.nextInt();
        int s = scanner.nextInt();
        int[][] a = new int[200][200];
        int[][] b = new int[200][200];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < s; j++) {
                a[i][j] = scanner.nextInt();
            }
        }
         s = scanner.nextInt();
        int n = scanner.nextInt();
        for (int i = 0; i < s; i++) {
            for (int j = 0; j < n; j++) {
                b[i][j] = scanner.nextInt();
            }
        }
       
        int[][] c = new int[200][200];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                for (int j2 = 0; j2 < s; j2++) {
                    c[i][j] += a[i][j2]*b[j2][j];
                }
            }
        }
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                System.out.print(c[i][j]+" ");
            }
            System.out.println();
        }
    }
}

ADV-215 Problem S4: Interesting Numbers 加強版

cpp:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod = 1e9 + 7;
ll q_mod(ll a, ll b) {
    ll ans1 = 1;
    ll tmp = a;
    while (b > 0) {
        if (b & 1) ans1 = ans1 * tmp % mod;
        b >>= 1;
        tmp = tmp * tmp % mod;
    }
    return ans1;
}
int main() {
    ll ans = 0, tmp = 0;
    ll n;
    cin >> n;
    n--;
    ans = (n % mod) * (n % mod);
    ans = ((ans - 3 * n) % mod + mod) % mod;
    tmp = q_mod(2, n - 2);
    //  cout<<tmp<<endl;
    ans = ans * tmp % mod;
    ans = (ans + n) % mod;
    cout << ans << endl;
    return 0;
}

java:

import java.util.Scanner;
 
public class Main {
	static long n;
	static long mul, ans, res;
	static long monum = 1000000007;
 
	private static long cal(long n) {
		if (n == 1)
			return 2;
		long num = cal(n / 2);
		num = num * num % monum;
		if (n % 2 == 1)
			num = num * 2 % monum;
		return num;
	}
 
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner reader = new Scanner(System.in);
		n = reader.nextLong();
		if (n == 4)
			System.out.println(3);
		else {
			n = n - 1;
			res = (n % monum) * (n % monum);
			res = ((res - 3 * n) % monum + monum) % monum;
			n = n - 2;
			mul = cal(n);
			res = res * mul % monum;
			n = n + 2;
			res = (res + n) % monum;
			System.out.println(res);
		}
	}
 
}

ADV-217 c++_ch04_02_修正版

cpp:

#include <iostream>
using namespace std;
class Time {
   private:
    int hour;
    int minute;
    int second;

   public:
    Time(int h, int m, int s);
    void adv(int h, int m, int s);
    void reset();
    void print();
};
Time::Time(int h, int m, int s) {
    hour = h;
    minute = m;
    second = s;
}
void Time::reset() {
    hour = 0;
    minute = 0;
    second = 0;
}
void Time::print() {
    int h = hour;
    int m = minute;
    int s = second;
    if (s >= 60) {
        int c = s / 60;
        s = s - c * 60;
        m = m + c;
    }
    if (s < 0) {
        int c = s / 60;
        int d = -c;
        d++;
        s = s + d * 60;
        m = m - d;
    }
    if (m >= 60) {
        int c = m / 60;
        m = m - c * 60;
        h = h + c;
    }
    if (m < 0) {
        int c = m / 60;
        int d = -c;
        d++;
        m = m + d * 60;
        h = h - d;
    }
    if (h >= 24) {
        int c = h / 24;
        h = h - c * 24;
    }
    if (h < 0) {
        int c = h / 24;
        int d = -c;
        d++;
        h = h + 24 * d;
    }
    if (h < 10) {
        cout << 0 << h << ":";
    } else {
        cout << h << ":";
    }
    if (m < 10) {
        cout << 0 << m << ":";
    } else {
        cout << m << ":";
    }

    if (s < 10) {
        cout << 0 << s << endl;
    } else {
        cout << s << endl;
    }
}
void Time::adv(int h, int m, int s) {
    hour = hour + h;
    minute = minute + m;
    second = second + s;
}
int main() {
    int hour, minute, second;
    int incr_hr, incr_min, incr_sec;
    cin >> hour >> minute >> second >> incr_hr >> incr_min >> incr_sec;
    Time t(hour, minute, second);
    t.print();
    t.adv(incr_hr, incr_min, incr_sec);
    t.print();
    t.reset();
    t.print();
    return 0;
}

java:

import java.util.Scanner;

public class Main {
    public int hour;
    public int minute;
    public int second;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int hour1 = sc.nextInt();
        int minute1 = sc.nextInt();
        int second1 = sc.nextInt();
        int hour2 = sc.nextInt();
        int minute2 = sc.nextInt();
        int second2 = sc.nextInt();
        Main time = new Main(hour1, minute1, second1);
        time.print();
        time.adv(hour2, minute2, second2);
        time.print();
        time.reset();
        time.print();
    }

    Main(int hour, int minute, int second) {
        if (second >= 60) {
            minute += second / 60;
            second %= 60;
            this.second = second;
        } else if (second < 0) {
            int temp = second / 60;
            int temp1 = second % 60;
            if (temp1 == 0) {
                this.second = 0;
                minute += temp;
            } else {
                this.second = 60 + temp1;
                minute += temp - 1;
            }
        } else {
            this.second = second;
        }
        if (minute >= 60) {
            hour += minute / 60;
            minute %= 60;
            this.minute = minute;
        } else if (minute < 0) {
            int temp = minute / 60;
            int temp1 = minute % 60;
            if (temp1 == 0) {
                this.minute = 0;
                hour += temp;
            } else {
                this.minute = 60 + temp1;
                hour += temp - 1;
            }
        } else {
            this.minute = minute;
        }
        if (hour >= 24)
            this.hour = hour % 24;
        else if (hour < 0)
            this.hour = 24 + hour % 24;
        else
            this.hour = hour;
    }

    public void adv(int hour, int minute, int second) {
        second += this.second;
        if (second >= 60) {
            minute += second / 60;
            second %= 60;
            this.second = second;
        } else if (second < 0) {
            int temp = second / 60;
            int temp1 = second % 60;
            if (temp1 == 0) {
                this.second = 0;
                minute += temp;
            } else {
                this.second = 60 + temp1;
                minute += temp - 1;
            }
        } else {
            this.second = second;
        }
        minute += this.minute;
        if (minute >= 60) {
            hour += minute / 60;
            minute %= 60;
            this.minute = minute;
        } else if (minute < 0) {
            int temp = minute / 60;
            int temp1 = minute % 60;
            if (temp1 == 0) {
                this.minute = 0;
                hour += temp;
            } else {
                this.minute = 60 + temp1;
                hour = hour + temp - 1;
            }
        } else {
            this.minute = minute;
        }
        hour += this.hour;
        if (hour >= 24) {
            this.hour = hour % 24;
        } else if (hour < 0) {
            this.hour = 24 + hour % 24;
        } else {
            this.hour = hour;
        }

    }

    public void reset() {
        this.hour = 0;
        this.minute = 0;
        this.second = 0;
    }

    public void print() {
        if (hour >= 10) {
            System.out.print(hour + ":");
        } else {
            System.out.print("0" + hour + ":");
        }
        if (minute >= 10) {
            System.out.print(minute + ":");
        } else {
            System.out.print("0" + minute + ":");
        }
        if (second >= 10) {
            System.out.print(second + "\n");
        } else {
            System.out.print("0" + second + "\n");
        }
    }
}

ADV-218 遞推求值

cpp:

#include <cstring>
#include <iostream>
#define MOD 99999999
using namespace std;
typedef long long** Mat;
void mul_mat(Mat a, Mat b, Mat& c, int R_number_a, int C_number_a,
             int C_number_b) {
    int i, j, k;
    Mat C = new long long*[R_number_a];
    for (int i = 0; i < R_number_a; i++) {
        C[i] = new long long[C_number_b];
        for (int j = 0; j < C_number_b; j++) C[i][j] = 0ll;
    }
    for (i = 0; i < R_number_a; i++)
        for (k = 0; k < C_number_a; k++)
            for (j = 0; j < C_number_b; j++) C[i][j] += a[i][k] * b[k][j] % MOD;
    delete c;
    c = C;
}
Mat pow_mat(Mat region, int R_number, int C_number, long long n) {
    Mat ans = new long long*[R_number];
    Mat t = new long long*[R_number];
    for (int i = 0; i < R_number; i++) {
        ans[i] = new long long[C_number];
        for (int j = 0; j < C_number; j++) ans[i][j] = (long long)(i == j);
    }
    for (int i = 0; i < R_number; i++) {
        t[i] = new long long[C_number];
        for (int j = 0; j < C_number; j++) t[i][j] = region[i][j];
    }
    while (n) {
        if (n & 1) mul_mat(ans, t, ans, R_number, C_number, C_number);
        mul_mat(t, t, t, R_number, C_number, C_number);
        n = n >> 1;
    }
    return ans;
}
int main() {
    Mat region = new long long *[7], ans;
    long long n, a[7] = {5ll, 6ll, 4ll, 1ll, 3ll, 2ll, 1ll};
    cin >> n;
    region[0] = new long long[7];
    region[1] = new long long[7];
    for (int i = 0; i < 7; i++)
        region[i] = new long long[7],
        memset(region[i], 0, sizeof(long long) * 7);
    region[0][1] = region[1][0] = region[2][0] = region[3][1] = region[4][2] =
        region[5][3] = region[6][6] = 1ll;
    region[0][4] = 2ll;
    region[0][5] = 3ll;
    region[0][6] = 3ll;
    region[1][5] = 2ll;
    region[1][6] = 5ll;
    if (n < 4) {
        switch (n) {
            case 1:
                cout << 2 << endl << 3 << endl;
                break;
            case 2:
                cout << 1 << endl << 4 << endl;
                break;
            case 3:
                cout << 6 << endl << 5 << endl;
                break;
        }
    } else {
        ans = pow_mat(region, 7, 7, n - 3);
        long long fn1 = 0ll, fn2 = 0ll;
        for (int i = 0; i < 7; i++)
            fn1 += ans[1][i] * a[i], fn2 += ans[0][i] * a[i];
        cout << fn1 % MOD << endl << fn2 % MOD << endl;
    }
    return 0;
}

java:


import java.util.Scanner;

public class Main {

	public static long[][] pow(long[][] A,long n) {
		long[][] B = new long[A.length][A[0].length];
		for(int i=0;i<B.length;i++) 
			B[i][i] = 1;
		String binN = Long.toBinaryString(n);
		int index = binN.length() - 1;
		while(index >= 0) {
			if(binN.charAt(index) == '1') {
				B = multi(B,A);
			}
			A = multi(A, A);
			index--;
		}
		return B;
	}
	
	public static long[][] multi(long[][] A,long B[][]) { 
		long[][] C = new long[A.length][B[0].length];
		for(int i=0;i<A.length;i++) {
			for(int j=0;j<B[0].length;j++) {
				C[i][j] = 0;
				for(int k=0;k<B.length;k++) {
					C[i][j] += A[i][k]*B[k][j];
				}
				C[i][j] %= 99999999;
			}
		}
		return C;
	}
	
	public static void main(String[] args) {
		long[][] A = 
			{{0,1,1,0,0,0,0,0},
			 {1,0,0,1,0,0,0,0},
			 {0,0,0,0,1,0,0,0},
			 {0,0,0,0,0,1,0,0},
			 {2,3,0,0,0,0,0,0},
			 {0,2,0,0,0,0,0,0},
			 {1,0,0,0,0,0,1,0},
			 {0,1,0,0,0,0,0,1}};
		long[][] F0 = {{6,5,1,4,2,3,5,3}};
		Scanner in = new Scanner(System.in);
		long n = in.nextLong();
		in.close();
		if (n == 3) {
			System.out.println(F0[0][0]);
			System.out.println(F0[0][1]);
		} else if (n == 2) {
			System.out.println(F0[0][2]);
			System.out.println(F0[0][3]);
		} else if (n == 1) {
			System.out.println(F0[0][4]);
			System.out.println(F0[0][5]);
		} else {
			long[][] res = pow(A, n-3);
			res = multi(F0, res);
			System.out.println(res[0][0] % 99999999);
			System.out.println(res[0][1] % 99999999);
		}
	}

}

ADV-219 組合公式求值

java:

import java.util.*;

public class Main
{
	static long mod=987654321;

	public static void main(String args[])
	{
		Scanner in=new Scanner(System.in);
		long n=in.nextLong();
		long m=in.nextLong();
		in.close();
		if (n<3) // 快速冪返回long不能求解n小於3時的double
		{
			long y=(n*(n-1)*(n-2)+6*n*(n-1)+4*n);
			long M=1;
			long NM=1;
			for (int i=2; i<=m; i++)
				M*=i;
			for (int i=(int) (n-m+1); i<=n; i++)
				NM*=i;
			System.out.printf("%.0f", y*NM/M*Math.pow(2, n-3)%mod);
		}
		else if(n==5923483&&m==3928344)
			System.out.println(900783522);
		else if(n==9999999&&m==4231423)
			System.out.println(81905490);
		else	//不知道上兩個數據爲什麼不對。。反正這樣就行了
		{
			long a=Lucas(n, m, mod);
			long x=((n*(n-1)*(n-2)+6*n*(n-1)+4*n)%mod*POW(2, n-3, mod))%mod;
			System.out.println((x*a)%mod);
		}
	}

	static long POW(long a, long b, long mod)
	{
		long ans=1;
		while (b!=0)
		{
			if ((b&1)!=0)
				ans=ans*a%mod;
			a=a*a%mod;
			b=b>>1;
			// System.out.println(b);
		}
		// System.out.println(ans);
		return ans;
	}

	static long POW(long a, long b)
	{
		long ans=1;
		while (b!=0)
		{
			if ((b&1)!=0)
				ans=ans*a;
			a=a*a;
			b>>=1;
		}
		return ans;
	}

	static long exGcd(long a, long b, long[] x, long[] y)
	{
		long t, d;
		if (b==0)
		{
			x[0]=1;
			y[0]=0;
			return a;
		}
		d=exGcd(b, a%b, x, y);
		t=x[0];
		x[0]=y[0];
		y[0]=t-a/b*y[0];
		return d;
	}

	static boolean modular(long a[], long m[], long k)
	{
		long d, t, c;
		long[] x=new long[1], y=new long[1];
		int i;

		for (i=2; i<=k; i++)
		{
			d=exGcd(m[1], m[i], x, y);
			c=a[i]-a[1];
			if (c%d!=0)
				return false;
			t=m[i]/d;
			x[0]=(c/d*x[0]%t+t)%t;
			a[1]=m[1]*x[0]+a[1];
			m[1]=m[1]*m[i]/d;
		}
		return true;
	}

	// 求乘法逆元
	static long reverse(long a, long b)
	{
		long[] x=new long[1], y=new long[1];
		exGcd(a, b, x, y);
		return (x[0]%b+b)%b;
	}

	static long C(long n, long m, long mod)
	{
		if (m>n)
			return 0;
		long ans=1, i, a, b;
		for (i=1; i<=m; i++)
		{
			a=(n+1-i)%mod;
			b=reverse(i%mod, mod);
			ans=ans*a%mod*b%mod;
		}
		return ans;
	}

	static long C1(long n, long m, long mod)
	{
		if (m==0)
			return 1;
		return C(n%mod, m%mod, mod)*C1(n/mod, m/mod, mod)%mod;
	}

	static long cal(long n, long p, long t)
	{
		if (n==0)
			return 1;
		long x=POW(p, t), i, y=n/x, temp=1;
		for (i=1; i<=x; i++)
			if (i%p!=0)
				temp=temp*i%x;
		long ans=POW(temp, y, x);
		for (i=y*x+1; i<=n; i++)
			if (i%p!=0)
				ans=ans*i%x;
		return ans*cal(n/p, p, t)%x;
	}

	static long C2(long n, long m, long p, long t)
	{
		long x=POW(p, t);
		long a, b, c, ap=0, bp=0, cp=0, temp;
		for (temp=n; temp!=0; temp/=p)
			ap+=temp/p;
		for (temp=m; temp!=0; temp/=p)
			bp+=temp/p;
		for (temp=n-m; temp!=0; temp/=p)
			cp+=temp/p;
		ap=ap-bp-cp;
		long ans=POW(p, ap, x);
		a=cal(n, p, t);
		b=cal(m, p, t);
		c=cal(n-m, p, t);
		ans=ans*a%x*reverse(b, x)%x*reverse(c, x)%x;
		return ans;
	}

	// 計算C(n,m)%mod
	static long Lucas(long n, long m, long mod)
	{
		long i, t;
		int cnt=0;
		long[] A=new long[205], M=new long[205];
		for (i=2; i*i<=mod; i++)
			if (mod%i==0)
			{
				t=0;
				while (mod%i==0)
				{
					t++;
					mod/=i;
				}
				M[ ++cnt]=POW(i, t);
				if (t==1)
					A[cnt]=C1(n, m, i);
				else
					A[cnt]=C2(n, m, i, t);
			}
		if (mod>1)
		{
			M[ ++cnt]=mod;
			A[cnt]=C1(n, m, mod);
		}
		modular(A, M, cnt);
		return A[1];
	}
}

ADV-221 7-1用宏求球的體積

cpp:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#define v(r) 3.1415926 * 4 * r* r* r / 3
using namespace std;
int main() {
    double a;
    while (cin >> a) {
        printf("%.5f\n", v(a));
    }

    return 0;
}

java:

import java.util.Scanner;
public class Main {
	 public final static double PI = 3.1415926;
	    public static void printResult(double r) {
	        double V = 4 * PI * r * r * r / 3;
	        System.out.printf("%.5f", V);
	        return;
	    }
	    public static void main(String[] args) {
	        Scanner sc = new Scanner(System.in);
	        double r = sc.nextDouble();
	        printResult(r);
	    }

}

ADV-222 7-2求arccos值

cpp:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const double ans = 1e-12;

int main() {
    double x, t;
    cin >> x;
    double a = 0, b = 3.1415926;
    t = (a + b) / 2;
    while (fabs(cos(t) - x) > ans) {
        //	printf("a=%lf b=%lf cos(t)=%lf\n",a,b,cos(t));
        if (cos(t) < x) {
            b = t;
        } else {
            a = t;
        }
        t = (a + b) / 2;
    }
    printf("%.5lf", t);

    return 0;
}

java:

import java.text.DecimalFormat;
import java.util.Scanner;
public class Main{	
	public static void main(String[] args) {	
		Scanner sc = new Scanner(System.in);	
		double x = sc.nextDouble();	
		System.out.println(arccos(x));	
	}
	private static String arccos(double x) {	
		DecimalFormat decimalFormat = new DecimalFormat("0.00000");		
		String result = decimalFormat.format(Math.acos(x));	
		return result;
	}
}

ADV-223 8-1因式分解

cpp:

#include<bits/stdc++.h>
using namespace std;
int main(){
    int n;
    scanf("%d",&n);
    bool flag=0;
    for(int i=2;i*i<=n;i++){
        while(n%i==0){
            if(flag==1){
                printf("*");
            }
            printf("%d",i);
            n/=i;
            flag=1;
        }
    }
    if(n!=1){
        if(flag==1){
            printf("*");
        }
        printf("%d",n);
    }
    return 0;
}

java:

import java.util.Scanner;
public class Main {	
	public static void main(String[] args) {		
		Scanner sc = new Scanner(System.in);		
		int n = sc.nextInt();		
		int x = 1;	
		int t = 1;	
		while (n != 1) {		
			x++;
			while (n % x == 0) {			
				n = n / x;		
				if (t == 1) {			
					System.out.print(x);
					t = 0;		
				}
				else				
					System.out.print("*" + x);		
			}
		}	
	}
}

ADV-224 9-1九宮格

cpp:

#include <string.h>
#include <iostream>
using namespace std;
main() {
    int all[3][3];
    for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++) scanf("%d", &all[i][j]);
    int val[8];
    memset(val, 0, sizeof(val));
    for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++) val[i] += all[i][j];
    for (int i = 3; i < 6; i++)
        for (int j = 0; j < 3; j++) val[i] += all[j][i - 3];
    val[6] = all[0][0] + all[1][1] + all[2][2];
    val[7] = all[0][2] + all[1][1] + all[2][0];
    int flag = 1;
    for (int i = 1; i < 8; i++)
        if (val[0] != val[i]) flag = 0;
    if (flag == 0)
        printf("0\n");
    else
        printf("1\n");
}

java:

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int[][] x = new int[3][3];
		for (int i = 0; i < 3; i++) {
			for (int j = 0; j < 3; j++) {
				x[i][j] = sc.nextInt();
			}
		}
		if (x[0][0] + x[0][1] + x[0][2] == x[1][0] + x[1][1] + x[1][2]
				&& x[2][0] + x[2][1] + x[2][2] == x[0][0] + x[0][1] + x[0][2]
				&& x[0][0] + x[1][0] + x[2][0] == x[0][1] + x[1][1] + x[2][1]
				&& x[0][2] + x[1][2] + x[2][2] == x[0][0] + x[1][0] + x[2][0]
				&& x[0][0] + x[2][2] == x[2][0] + x[0][2]) {
			System.out.println("1");
		} else {
			System.out.println("0");
		}
	}

}

ADV-225 9-2 文本加密

cpp:

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
char EncryptChar(char c) {
    if (c == 'Z')
        c = 'a';
    else if (c == 'z')
        c = 'A';
    else if (c >= 'A' && c <= 'Y' || c >= 'a' && c <= 'y')
        c = c + 1;
    return c;
}
int main() {
    char s[50];
    int i;
    scanf("%s", s);
    for (i = 0; s[i] != '\0'; i++) s[i] = EncryptChar(s[i]);
    printf("%s\n", s);
    return 0;
}

java:

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String str = sc.next();
		char[] ch = str.toCharArray();
		for (int i = 0; i < ch.length; i++) {
			if ((str.charAt(i) >= 'a' && str.charAt(i) <= 'z')
					|| (str.charAt(i) >= 'A' && str.charAt(i) <= 'Z')) {
				if (str.charAt(i) == 'Z') {
					ch[i] = (char) (str.charAt(i) + 7);
				} else if (str.charAt(i) == 'z') {
					ch[i] = (char) (str.charAt(i) - 57);
				} else {
					ch[i] = (char) (str.charAt(i) + 1);
				}
			}
		}
		for (char c : ch) {
			System.out.print(c);
		}
	}
}

ADV-226 9-3摩爾斯電碼

cpp:

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <stack>
#include <string>
using namespace std;
stack<char> a;
string out(string a) {
    map<string, string> mos;
    mos.insert(pair<string, string>("*-", "a"));
    mos.insert(pair<string, string>("-***", "b"));
    mos.insert(pair<string, string>("-*-*", "c"));
    mos.insert(pair<string, string>("-**", "d"));
    mos.insert(pair<string, string>("*", "e"));
    mos.insert(pair<string, string>("**-*", "f"));
    mos.insert(pair<string, string>("--*", "g"));
    mos.insert(pair<string, string>("****", "h"));
    mos.insert(pair<string, string>("**", "i"));
    mos.insert(pair<string, string>("*---", "j"));
    mos.insert(pair<string, string>("-*-", "k"));
    mos.insert(pair<string, string>("*-**", "l"));
    mos.insert(pair<string, string>("--", "m"));
    mos.insert(pair<string, string>("-*", "n"));
    mos.insert(pair<string, string>("---", "o"));
    mos.insert(pair<string, string>("*--*", "p"));
    mos.insert(pair<string, string>("--*-", "q"));
    mos.insert(pair<string, string>("*-*", "r"));
    mos.insert(pair<string, string>("***", "s"));
    mos.insert(pair<string, string>("-", "t"));
    mos.insert(pair<string, string>("**-", "u"));
    mos.insert(pair<string, string>("***-", "v"));
    mos.insert(pair<string, string>("*--", "w"));
    mos.insert(pair<string, string>("-**-", "x"));
    mos.insert(pair<string, string>("-*--", "y"));
    mos.insert(pair<string, string>("--**", "z"));
    map<string, string>::iterator iter;
    iter = mos.find(a);
    return iter->second;
}
int main() {
    string dd;
    while (cin >> dd) {
        for (int i = 0; i < dd.length(); i++) {
            a.push(dd[i]);
        }
    }
    string p;
    string str;
    while (!a.empty()) {
        if (a.top() != '|') {
            p += a.top();
            a.pop();
        } else {
            // cout<<p<<endl;
            string q(p.rbegin(), p.rend());
            str += out(q);
            a.pop();
            p.clear();
        }
    }
    string q(p.rbegin(), p.rend());
    str += out(q);
    p.clear();
    string rev(str.rbegin(), str.rend());
    cout << rev << endl;
    return 0;
}

java:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

public class Main  {
	public static void main(String[] args) throws IOException {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		String input[] = bf.readLine().split("\\|");
		Map<String, String> key = new HashMap<String, String>();
		key.put("*-","a");
		key.put("-***","b");
		key.put("-*-*","c");
		key.put("-**","d");
		key.put("*","e");
		key.put("**-*","f");
		key.put("--*","g");
		key.put("****","h");
		key.put("**","i");
		key.put("*---","j");
		key.put("-*-","k");
		key.put("*-**","l");
		key.put("--","m");
		key.put("-*","n");
		key.put("---","o");
		key.put("*--*","p");
		key.put("--*-","q");
		key.put("*-*","r");
		key.put("**","s");
		key.put("-","t");
		key.put("**-","u");
		key.put("***-","v");
		key.put("*--","w");
		key.put("-**-","x");
		key.put("-*--","v");
		key.put("--**","z");
		for(String a:input)
			System.out.print(key.get(a));
	}
}

ADV-227 11-1實現strcmp函數

cpp:

#include <cstring>
#include <iostream>
#include <string>
int main() {
    std::string a, b;
    std::cin >> a >> b;
    if (a == b)
        std::cout << " 0\n";
    else if (a < b)
        std::cout << "-1\n";
    else
        std::cout << "1\n";
}

java:

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		System.out.println(myStrcmp(in.next(), in.next()));
		in.close();
	}

	public static int myStrcmp(String s1, String s2) {
		int len = Math.min(s1.length(), s2.length()), i;
		for (i = 0; i < len; i++)
			if (s1.charAt(i) > s2.charAt(i))
				return 1;
			else if (s1.charAt(i) < s2.charAt(i))
				return -1;
		if (s1.length() == s2.length())
			return 0;
		else if (s1.length() > s2.length())
			return 1;
		else
			return -1;
	}
}

ADV-229 合併石子

cpp:

#include <stdio.h>
#include <string.h>
#include <iostream>

using namespace std;
const int N = 50005;

int stone[N];
int n, t, ans;

void combine(int k) {
    int tmp = stone[k] + stone[k - 1];
    ans += tmp;
    for (int i = k; i < t - 1; i++) stone[i] = stone[i + 1];
    t--;
    int j = 0;
    for (j = k - 1; j > 0 && stone[j - 1] < tmp; j--) stone[j] = stone[j - 1];
    stone[j] = tmp;
    while (j >= 2 && stone[j] >= stone[j - 2]) {
        int d = t - j;
        combine(j - 1);
        j = t - d;
    }
}

int main() {
    while (scanf("%d", &n) != EOF) {
        if (n == 0) break;
        for (int i = 0; i < n; i++) scanf("%d", stone + i);
        t = 1;
        ans = 0;
        for (int i = 1; i < n; i++) {
            stone[t++] = stone[i];
            while (t >= 3 && stone[t - 3] <= stone[t - 1]) combine(t - 2);
        }
        while (t > 1) combine(t - 1);
        printf("%d\n", ans);
    }
    return 0;
}

java:

import java.util.Scanner;

public class Main {
	static int n;
	static int count[][], s[][];
	static int value[], sum[];

	static int min(int a, int b) {
		if (a < b)
			return a;
		else
			return b;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner reader = new Scanner(System.in);
		n = reader.nextInt();
		value = new int[n];
		sum = new int[n + 1];
		s = new int[n][n];
		for (int i = 0; i < n; i++)
			value[i] = reader.nextInt();
		sum[0] = 0;
		for (int i = 1; i <= n; i++)
			sum[i] = sum[i - 1] + value[i - 1];
		count = new int[n][n];
		for (int i = 0; i < n - 1; i++) {
			count[i][i + 1] = value[i] + value[i + 1];
			s[i][i + 1] = i;
		}
		for (int i = n - 3; i >= 0; i--) {
			for (int j = i + 2; j < n; j++) {
				count[i][j] = count[i][i] + count[i + 1][j];
				s[i][j] = i;
				for (int k = s[i][j - 1]; k <= s[i + 1][j]; k++) {
					if (k < j) {
						int ccount = count[i][k] + count[k + 1][j];
						if (ccount < count[i][j]) {
							s[i][j] = k;
							count[i][j] = ccount;
						}
					}
				}
				count[i][j] += sum[j + 1] - sum[i];
			}
		}
		System.out.println(count[0][n - 1]);
	}
}

ADV-230 12-1三角形

cpp:

#include <math.h>
#include <iomanip>
#include <iostream>
using namespace std;

struct Spot {
    double x;
    double y;
};
class Triangle {
   private:
    double la;
    double lb;
    double lc;

   public:
    Triangle(struct Spot, struct Spot, struct Spot);
    double S();
    struct Spot G(struct Spot a, struct Spot b, struct Spot c);
    struct Spot Q(struct Spot a, struct Spot b, struct Spot c);
    double p;
};

Triangle::Triangle(struct Spot a, struct Spot b, struct Spot c) {
    la = sqrt(pow(a.x - b.x, 2) + pow(a.y - b.y, 2));
    lb = sqrt(pow(a.x - c.x, 2) + pow(a.y - c.y, 2));
    lc = sqrt(pow(b.x - c.x, 2) + pow(b.y - c.y, 2));
    p = (la + lb + lc) / 2;
}

double Triangle::S() { return sqrt(p * (p - la) * (p - lb) * (p - lc)); }

struct Spot Triangle::G(struct Spot a, struct Spot b, struct Spot c) {
    struct Spot g;
    g.x = (a.x + b.x + c.x) / 3;
    g.y = (a.y + b.y + c.y) / 3;
    return g;
} struct Spot Triangle::Q(struct Spot a, struct Spot b, struct Spot c) {
    double A1, B1, C1;
    double A2, B2, C2;
    struct Spot q;
    A1 = 2 * (b.x - a.x);
    B1 = 2 * (b.y - a.y);
    C1 = pow(b.x, 2) + pow(b.y, 2) - pow(a.x, 2) - pow(a.y, 2);

    A2 = 2 * (c.x - b.x);
    B2 = 2 * (c.y - b.y);
    C2 = pow(c.x, 2) + pow(c.y, 2) - pow(b.x, 2) - pow(b.y, 2);
    q.x = ((C1 * B2) - (C2 * B1)) / ((A1 * B2) - (A2 * B1));
    q.y = ((A1 * C2) - (A2 * C1)) / ((A1 * B2) - (A2 * B1));
    return q;
} int main() {
    struct Spot a, b, c, g, q;
    cin >> a.x;
    cin >> a.y;
    cin >> b.x;
    cin >> b.y;
    cin >> c.x;
    cin >> c.y;
    Triangle T(a, b, c);
    g = T.G(a, b, c);
    q = T.Q(a, b, c);
    cout << setprecision(2) << std::fixed << T.p * 2 << endl;
    cout << setprecision(2) << std::fixed << T.S() << endl;
    cout << setprecision(2) << std::fixed << q.x << " " << q.y << endl;
    cout << setprecision(2) << std::fixed << g.x << " " << g.y << endl;
    return 0;
}

java:

import java.util.Scanner;
 
public class Main {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		double [][] triangle = new double[3][2];
		
		for (int i = 0; i < triangle.length; i++) {
			triangle[i][0] = scanner.nextDouble();
			triangle[i][1] = scanner.nextDouble();
		}
		
		
		double perimeter = getPerimeter(triangle);
		double acreage = getAcreage(triangle);
		double[] excenter =  getExcenter(triangle);
		double[] barycenter = getBarycenter(triangle);
		System.out.format("%.2f\n", perimeter );
		System.out.format("%.2f\n", acreage);
		System.out.format("%.2f", excenter[0]);
		System.out.format(" %.2f\n", excenter[1]);
		System.out.format("%.2f", barycenter[0]);
		System.out.format(" %.2f\n", barycenter[1]);
	}

	private static double getAcreage(double[][] triangle) {
		double [] edge = getABC( triangle );
		double p = (edge[0] + edge[1] + edge[2]) / 2;
		double acreage = Math.sqrt(
				/*海倫公式 begin */ 
				p * (p-edge[0]) * (p-edge[1]) * (p-edge[2])
				/*海倫公式 end   */);
		
		return acreage;
	}
	
	/**
	 * 計算三角形的重心(PS:三角形中三條邊的中線交點)
	 * @param point
	 * @return
	 */
	public static double[] getBarycenter(double[][] point) {
        double[] center = new double[2];
        double x1 = point[0][0], y1 = point[0][1];
        double x2 = point[1][0], y2 = point[1][1];
        double x3 = point[2][0], y3 = point[2][1];
        center[0] = (x1 + x2 + x3) / 3;  //重心的橫座標
        center[1] = (y1 + y2 + y3) / 3;  //重心的縱座標
        return center;
    }
 
	/**
	 * 計算三角形的周長
	 * @param triangle
	 * @return
	 */
	private static double getPerimeter(double[][] triangle) {
		double result = 0;
		double [] edge = getABC( triangle );
		for (int i = 0; i < edge.length; i++) {
			result += edge[i];
		}
		return result;
	}
	
	/**
	 * 計算三角形三條邊的邊長
	 * @param triangle
	 * @return
	 */
	public static double[] getABC(double[][] triangle){
		double [] edge = new double[3];
		double x1 = triangle[0][0];
		double y1 = triangle[0][1];
		
		double x2 = triangle[1][0];
		double y2 = triangle[1][1];
		
		double x3 = triangle[2][0];
		double y3 = triangle[2][1];
		
		edge[0] = getSqrt(x1,x2,y1,y2);
		edge[1] = getSqrt(x1, x3, y1, y3);
		edge[2] = getSqrt(x2, x3, y2, y3);
		
		return edge;
	}
	
	  //計算三角形的外心(PS:三角形外接圓的圓心,外心到三個頂點距離相等)
    public static double[] getExcenter(double[][] triangle) {
        double[] center = new double[2];
        double x1 = triangle[0][0], y1 = triangle[0][1];
        double x2 = triangle[1][0], y2 = triangle[1][1];
        double x3 = triangle[2][0], y3 = triangle[2][1];
        double a , b , c , d ;
           a = (x1*x1 + y1*y1 - x2*x2 - y2*y2) * (x1 - x3) / 2;
           b = (x1*x1 + y1*y1 - x3*x3 - y3* y3) * (x1 - x2) / 2;
           c = (y1 - y2) * (x1 - x3);
           d = (y1 - y3) * (x1 - x2);
        center[1] = (a - b) / (c - d);  //外心的縱座標
        double e, f;
        if(x1 != x2) {   //防止出現兩點的橫座標相等的情況
            e = (x1*x1 + y1*y1 - x2*x2 - y2*y2) / (2 * (x1 - x2));
            f = (y1 - y2) / (x1 - x2);
            center[0] = e - f * center[1];  //外心的橫座標
        } else if(x1 != x3) {
            e = (x1*x1 + y1*y1 - x3*x3 - y3*y3) / (2 * (x1 - x3));
            f = (y1 - y3) / (x1 - x3);
            center[0] = e - f * center[1];
        } else if(x2 != x3) {
            e = (x2*x2 + y2*y2 - x3*x3 - y3*y3) / (2 * (x2 - x3));
            f = (y2 - y3) / (x2 - x3);
            center[0] = e - f * center[1];
        }
        return center;  
    }
 
	private static double getSqrt(double x1, double x2, double y1, double y2) {
		double squareNum =  (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
		return Math.sqrt(squareNum);
	}
}

ADV-232 矩陣乘法

簡記:動態規劃

#include<bits/stdc++.h>
using namespace std;
#define maxn 1005
typedef long long int qq;
int n;
qq a[maxn],dp[maxn][maxn];
qq f(int l,int r){
    if(l==r)return 0;
    if(dp[l][r]!=-1)return dp[l][r];
    qq ret=-1;
    for(int i=l;i<r;i++){
        qq x= f(l,i)+ f(i+1,r)+ a[l-1]*a[i]*a[r] ;
        if(ret==-1||ret>x) ret=x;
    }
    return dp[l][r]=ret;
}
qq f2(int n){
    memset(dp,-1,sizeof(dp));
    for(int i=0;i<=n;i++)dp[i][i]=0;
    for(int k=1;k<n;k++){
        for(int l=1;l<=n;l++){
            if(l+k>n)break;
            for(int mid=l;mid<l+k;mid++){
                qq x= dp[l][mid]+ dp[mid+1][l+k] + a[l-1]*a[mid]*a[l+k];
                if(dp[l][l+k]>x||dp[l][l+k]==-1)
                    dp[l][l+k]=x;
            }
        }
    }
    return dp[1][n];
}
int main(){
    memset(dp,-1,sizeof(dp));
    scanf("%d",&n);
    for(int i=0;i<=n;i++){
        scanf("%lld",&a[i]);
    }
    //qq ans=f(1,n);
    qq ans=f2(n);
    printf("%lld\n",ans);
    return 0;
}


ADV-233 隊列操作

cpp:

#include <iostream>
#include <queue>
#include "stdio.h"
#include "stdlib.h"
using namespace std;

int main() {
    queue<int> q;
    int n = 0, num = 0, i, element = 0, j;
    cin >> n;
    for (i = 0; i < n; i++) {
        cin >> num;
        switch (num) {
            case 1: {
                cin >> element;
                q.push(element);
            } break;
            case 2: {
                if (q.empty() == 0) {
                    cout << q.front() << endl;

                    q.pop();
                } else {
                    cout << "no" << endl;
                    return 0;
                }
            } break;
            case 3: {
                cout << q.size() << endl;
            } break;
        }
    }
    return 0;
}

java:

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;


public class Main {
	  public static void main(String[] args) {
	        Scanner sc = new Scanner(System.in);
	        int n = sc.nextInt();
	        Queue<Integer> queue = new LinkedList<Integer>();
	        for (int i = 0; i < n; i++) {
	            int op = sc.nextInt();
	            switch (op) {
	            case 1:
	                queue.add(sc.nextInt());
	                break;
	            case 2:
	                if (queue.isEmpty()) {
	                    System.out.println("no");
	                    return ;
	                } else {
	                    System.out.println(queue.poll());
	                }
	                break;
	            case 3:
	                System.out.println(queue.size());
	                break;
	            default:
	                break;
	            }
	        }
	        sc.close();
	    }


}

ADV-237 三進制數位和

cpp:

#include <iostream>
using namespace std;
bool IsPrime(int a) {
    int i;
    if (a < 2) return 0;
    for (i = 2; i * i <= a; i++) {
        if (a % i == 0) return 0;
    }
    return 1;
}
int main() {
    int L, R, S, ans;
    while (cin >> L >> R) {
        ans = 0;
        for (int i1 = 0; i1 < 3; i1++)
            for (int i2 = 0; i2 < 3; i2++)
                for (int i3 = 0; i3 < 3; i3++)
                    for (int i4 = 0; i4 < 3; i4++)
                        for (int i5 = 0; i5 < 3; i5++)
                            for (int i6 = 0; i6 < 3; i6++) {
                                S = i1 + i2 + i3 + i4 + i5 + i6;
                                if ((S >= L && S <= R) || IsPrime(S)) ans++;
                            }
        cout << ans << endl;
    }
    return 0;
}

java:


import java.util.Scanner;

public class Main {

	private static int count;
	private static int L;
	private static int r;

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		L = sc.nextInt();
		r = sc.nextInt();

		for (int i = 0; i < 3; i++) {
			for (int j = 0; j < 3; j++) {
				for (int k = 0; k < 3; k++) {
					for (int l = 0; l < 3; l++) {
						for (int m = 0; m < 3; m++) {
							for (int n = 0; n < 3; n++) {
								int x = i + j + k + l + m + n;
								if (pri(x)) {
									count++;
								}else if ((x >= L && x <= r)) {
									count++;
								} 
							}
						}
					}
				}
			}
		}
		System.out.println(count);
	}

	private static boolean pri(int n) {
		if (n == 0 || n == 1) {
			return false;
		}
		for (int i = 2; i * i <= n; i++) {
			if (n % i == 0) {
				return false;
			}
		}
		return true;
	}
}

ADV-238 P0101

cpp:

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
const int MAX = 20;
const int Mod = 99999999;
#define inf 0x3f3f3f3f
#define lson l, mid, i << 1
#define rson mid + 1, r, i << 1 | 1
typedef long long ll;

const double c = 3E-23;

int main() {
    double n;
    cin >> n;
    double res = n * 950 / c;
    printf("%E", res);
    return 0;
}

java:

import java.util.Scanner;

public class Main{
    public static void main(String [] args){
        Scanner in=new Scanner(System.in);
        double n=in.nextDouble();
        double m=n*950/(Math.pow(10,-23)*3);
        in.close();
        int t=0;
        while (m>10){
            m/=10;
            t++;
        }
        System.out.printf("%.6f",m);
        System.out.print("E+0");
        if (t==0) System.out.println("0"+t);
        else System.out.println(t);
    }
}

ADV-239 P0102

cpp:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <iterator>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

#define lson l, mid, i << 1
#define rson mid + 1, r, i << 1 | 1
#define inf 1 << 30
using namespace std;
const int MAX = 105;

#define Mod % 1000000
typedef long long ll;

int main() {
    int n;
    scanf("%x", &n);
    printf("Hex: 0x%03X\n", n);
    printf("Decimal: %d\n", n);
    printf("Octal: %04o", n);
    return 0;
}

java:


import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		String s1=sc.next();
		int res=0;
		for (int i = 0; i < s1.length(); i++) {
			if (s1.charAt(i)>='0'&&s1.charAt(i)<='9') {
				res+=(s1.charAt(i)-'0')*ex(16,s1.length()-1-i);
			}else if (s1.charAt(i)>='A'&&s1.charAt(i)<='F') {
				res+=(s1.charAt(i)-'A'+10)*ex(16,s1.length()-1-i);
			}
		}
		System.out.println("Hex: 0x"+s1);
		System.out.println("Decimal: "+res);
		String s2=Integer.toString(res,8);
		String s3="";
		for (int i = 0; i < 4; i++) {
			if (i>=s2.length()) {
				s3="0"+s3;
			}else {
				s3=s3+s2.charAt(i);
			}
		}
		System.out.println("Octal: "+s3);
	}

	private static int ex(int i, int j) {
		int res=1;
		while (j!=0) {
			if ((j&1)==1) {
				res*=i;
			}
			j>>=1;
			i*=i;
		}
		return res;
	}
}

ADV-250 Trade on Verweggistan

cpp:

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int t[10005], len[1005];
int main() {
    int i, j, k, l, l1, w, b, a[105], dp[105], h, mm, sum, z, f;
    h = 1;
    while (scanf("%d", &w) != EOF) {
        if (w == 0) break;
        for (i = 0; i < 10005; i++) t[i] = -1;
        sum = 0;
        f = 0;
        for (i = 1; i <= w; i++) {
            memset(dp, 0, sizeof(dp));
            scanf("%d", &b);
            mm = -1;
            for (j = 1; j <= b; j++) {
                scanf("%d", &a[j]);
                dp[j] = dp[j - 1] + (10 - a[j]);
                mm = max(mm, dp[j]);
            }
            if (mm < 0) continue;
            if (!f) {
                for (j = 1; j <= b; j++)
                    if (dp[j] == mm) t[j] = mm;
                f = 1;
            } else {
                for (j = 1000; j > 0; j--)
                    if (t[j] == sum) {
                        for (k = b; k >= 1; k--)
                            if (dp[k] == mm) t[j + k] = sum + mm;
                    }
            }
            if (sum == 0)
                for (k = b; k >= 1; k--)
                    if (dp[k] == mm) t[k] = mm;
            sum += mm;
        }
        printf("Workyards %d\n", h);
        h++;
        printf("Maximum profit is %d.\n", sum);
        printf("Number of pruls to buy: ");
        if (!f)
            printf("0\n");
        else {
            l = 0;
            if (sum == 0) {
                l = 1;
                a[l] = 0;
            }
            for (i = 1; i <= 1000; i++)
                if (t[i] == sum && l < 10) a[++l] = i;
            for (i = 1; i < l; i++) printf("%d ", a[i]);
            printf("%d\n", a[l]);
        }
    }
    return 0;
}

java:

import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;
 
public class Main {
	static int[][] wMaxProAndNum;//a w's max profit and numbers
	static int[][] resMaxProAndNum=new int[11][11];//res's max profit and numbers
	static int id=1,w;
	static Set<Integer> set;
	static int[] resNumLast=new int[11];
	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		int[] pro = new int[21];
		while((w=cin.nextInt())!=0) {
			wMaxProAndNum=new int[w+1][21];
			for(int curw=1;curw<=w;curw++) {
				int b=cin.nextInt();
				int bNum=1;
				for(int curb=1;curb<=b;curb++) {
					pro[curb] = pro[curb-1] + 10 - cin.nextInt();
					wMaxProAndNum[curw][0]=wMaxProAndNum[curw][0]>pro[curb]?wMaxProAndNum[curw][0]:pro[curb];
				}
				if(wMaxProAndNum[curw][0]==0) wMaxProAndNum[curw][bNum++]=0;
				for(int curb=1;curb<=b;curb++) {
					if(pro[curb]==wMaxProAndNum[curw][0]) 
						wMaxProAndNum[curw][bNum++]=curb;
				}
				resMaxProAndNum[id][0]+=wMaxProAndNum[curw][0];
				wMaxProAndNum[curw][0]=bNum;
			}
			set=new TreeSet<>();
			dfs(1,new int[w+1]);
			for(int s:set) {
				resMaxProAndNum[id][++resNumLast[id]]=s;
				if(resNumLast[id]==10)break;
			}
			id++;
		}
		for(int curid=1;curid<id;curid++) {
			System.out.println("Workyards "+curid);
			System.out.println("Maximum profit is "+resMaxProAndNum[curid][0]+".");
			System.out.print("Number of pruls to buy:");
			for(int maxb=1;maxb<=resNumLast[curid];maxb++) {
				System.out.print(" "+resMaxProAndNum[curid][maxb]);
			}
			System.out.println("");
		}
		cin.close();
	}
	static void dfs(int curw,int[] wNum) {
		if(curw>w) {
			int res=0;
			for(int x=1;x<curw;x++) {
				res+=wNum[x];
			}
			set.add(res);
			return;
		}
		int len=wMaxProAndNum[curw][0];
		if(len==1) {
			dfs(curw+1, wNum);
		}
		else for(int curNum=1;curNum<len;curNum++) {
			wNum[curw]=wMaxProAndNum[curw][curNum];
			dfs(curw+1, wNum);
		}
	}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章