蓝桥杯—动态数组使用,删除数组零元素, 最小乘积(基本型) ,Torry的困惑(基本型)

来源:互联网 发布:长期招异地淘宝客服 编辑:程序博客网 时间:2024/05/22 16:48

算法训练 动态数组使用      
从键盘读入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);

}

}

1 0
原创粉丝点击