藍橋杯—動態數組使用,刪除數組零元素, 最小乘積(基本型) ,Torry的困惑(基本型)

算法訓練 動態數組使用      
從鍵盤讀入n個整數,使用動態數組存儲所讀入的整數,並計算它們的和與平均值分別輸出。要求儘可能使用函數實現程序代碼。平均值爲小數的只保留其整數部分。
樣例輸入:
5
3 4 0 0 2
樣例輸出:
9 1
樣例輸入:
7
3 2 7 5 2 9 1
樣例輸出:
29 4
 

import java.util.*;
public class Main
{
public static void main(String[] args)
{
ArrayList<Integer> a=new ArrayList<Integer>();
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
for(int i=0;i<n;i++)
{
int c=sc.nextInt();
a.add(c);
}
int result1=Sum(n,a);
int result2=result1/n;
System.out.print(result1+" "+result2);
}
public static int Sum(int n,ArrayList<Integer> a)
{ int sum=0;
for(int i=0;i<n;i++)
{
sum=sum+a.get(i);
}
return sum;
}
}


算法訓練 刪除數組零元素      
從鍵盤讀入n個整數放入數組中,編寫函數CompactIntegers,刪除數組中所有值爲0的元素,其後元素向數組首端移動。注意,CompactIntegers函數需要接受數組及其元素個數作爲參數,函數返回值應爲刪除操作執行後數組的新元素個數。輸出刪除後數組中元素的個數並依次輸出數組元素。
樣例輸入: (輸入格式說明:5爲輸入數據的個數,3 4 0 0 2 是以空格隔開的5個整數)
5
3 4 0 0 2
樣例輸出:(輸出格式說明:3爲非零數據的個數,3 4 2 是以空格隔開的3個非零整數)
3
3 4 2
樣例輸入:
7
0 0 7 0 0 9 0
樣例輸出:
2
7 9
樣例輸入:
3
0 0 0
樣例輸出:
0

 import java.util.Scanner;
public class Main
{
public static int CompactIntegers ( int[] a, int n )
{
for ( int i = 0; i < n; i++ )
{
if (a[i] == 0)
{
int[] tmp = new int[a.length - 1];
System.arraycopy (a, 0, tmp, 0, i);
System.arraycopy (a, i + 1, tmp, i, tmp.length - i);
a = tmp;
return CompactIntegers (a, a.length);
}
}

System.out.println (n);
for ( int i = 0; i < n; i++ )
{
System.out.print (a[i] + " ");
}
return n;
}

public static void main ( String[] args )
{

Scanner sc = new Scanner (System.in);
int n = sc.nextInt ();
int a[] = new int[n];
for ( int i = 0; i < n; i++ )
{
a[i] = sc.nextInt ();
}
sc.close ();
CompactIntegers (a, n);
}
}


算法訓練 最小乘積(基本型)  
問題描述
  給兩組數,各n個。
  請調整每組數的排列順序,使得兩組數據相同下標元素對應相乘,然後相加的和最小。要求程序輸出這個最小值。
  例如兩組數分別爲:1 3  -5和-2 4 1

  那麼對應乘積取和的最小值應爲:
  (-5) * 4 + 3 * (-2) + 1 * 1 = -25
輸入格式
  第一個行一個數T表示數據組數。後面每組數據,先讀入一個n,接下來兩行每行n個數,每個數的絕對值小於等於1000。
  n<=8,T<=1000
輸出格式
  一個數表示答案。
樣例輸入
2
3
1 3 -5
-2 4 1
5
1 2 3 4 5
1 0 1 0 1

樣例輸出
-25
6

import java.util.Arrays;
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
for(int i=0;i<t;i++)
{
int n=sc.nextInt();
int a[]=new int[n];
int b[]=new int[n];
for(int j=0;j<n;j++)
{
a[j]=sc.nextInt();
}

for(int j=0;j<n;j++)
{
b[j]=sc.nextInt();
}
Arrays.sort(a);
Arrays.sort(b);
int sum=0;
for(int k=0;k<n;k++)
{
sum=sum+a[k]*b[n-k-1];
}
System.out.println(sum);
}

}
}



算法訓練 Torry的困惑(基本型)      
問題描述
  Torry從小喜愛數學。一天,老師告訴他,像2、3、5、7……這樣的數叫做質數。Torry突然想到一個問題,前10、100、1000、10000……個質數的乘積是多少呢?他把這個問題告訴老師。老師愣住了,一時回答不出來。於是Torry求助於會編程的你,請你算出前n個質數的乘積。不過,考慮到你才接觸編程不久,Torry只要你算出這個數模上50000的值。
輸入格式
  僅包含一個正整數n,其中n<=100000。
輸出格式
  輸出一行,即前n個質數的乘積模50000的值。
樣例輸入
1

樣例輸出
2

import java.util.Scanner;
public class Main{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int a[]=new int[100000];
int count=1;
int sum=1;


for(int i=2;count<=n;i++)
{ int j;
for(j=2;j<i;j++)
if(i%j==0) break;
if(i==j)
{
a[count]=i;
sum*=a[count];
count++;
}

}

int r=sum%50000;
System.out.print(r);

}

}


 

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