安全数组
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
double sum = 0;
int cnt= in.nextInt();
if( cnt>0 )
{
int[] numbers = new int[cnt];
for( int i = 0; i < numbers.length; i++)
{
numbers[i] = in.nextInt();
sum += numbers[i];
}
double average = sum / cnt;
for(int i = 0; i < numbers.length; i ++)
{
if( numbers[i]>average )
{
System.out.println(numbers[i]);
}
}
System.out.println(sum / cnt);
}
}
}
投票统计
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int[] numbers = new int[10];
int x = in.nextInt();
while( x != -1)
{
if( x>=0 && x<=9)
{
numbers[x]++;
}
x = in.nextInt();
}
for( int i = 0; i<numbers.length; i++)
{
System.out.println(i + ":" + numbers[i]);
}
}
}
线形搜索
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int[] data = {3, 2, 5, 7, 4, 9, 11, 34, 12, 28};
int x = in.nextInt();
int loc = -1;
for( int i=0; i<data.length; i++)
{
if( x == data[i] )
{
loc = i;
break;
}
}
if( loc > -1)
{
System.out.println( x + "是第" + (loc+1) + "个");
}
else
{
System.out.println( "Not Found" );
}
}
}
for each循环 用于确定是否存在
package hello;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int[] data = {3, 2, 5, 7, 4, 9, 11, 34, 12, 28};
int x = in.nextInt();
int loc = -1;
boolean found = false;
for ( int k : data)
{
if( k == x)
{
found = true;
}
}
if (found)
{
System.out.println( x + "是存在的");
}
for( int i=0; i<data.length; i++)
{
if( x == data[i] )
{
loc = i;
break;
}
}
if( loc > -1)
{
System.out.println( x + "是第" + (loc+1) + "个");
}
else
{
System.out.println( "Not Found" );
}
}
}
Tic-Tac-Toe游戏(井字棋)
问题描述:
读入一个3x3的矩阵,矩阵中的数字为1表示该位置上有一个X,为0表示为O;
程序判断这个矩阵中是否有获胜的一方,输出表示获胜一方的字符X或O,或输出无人获胜 。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
final int SIZE = 3;
int[][] board = new int[SIZE][SIZE];
int flag = 0;// 1:X win; 0:on one win; -1:0 win
int numX1 = 0; int numX2 = 0;
int num01 = 0; int num02 = 0;
//读入矩阵
for ( int i=0; i<SIZE; i++ )
{
for( int j=0; j<SIZE; j++ )
{
board[i][j] = in.nextInt();
}
}
//检查行和列
for ( int i=0; i<SIZE && flag==0; i++ )
{
numX1 = numX2 = 0;
num01 = num02 = 0;
for(int j=0; j<SIZE; j++)
{
if ( board[i][j] == 1 ) {
numX1 ++;
} else {
num01 ++;
}
if ( board[j][i] == 1 ) {
numX2 ++;
} else {
num02 ++;
}
}
if ( numX1==SIZE || numX2==SIZE )
{
flag = 1;
}
else if ( num01==SIZE || num02==SIZE )
{
flag = -1;
}
}
//检查对角线
numX1 = numX2 = 0;
num01 = num02 = 0;
for ( int i=0; i<SIZE && flag == 0; i++ )
{
if( board[i][i] == 1 )
{
numX1 ++;
} else
{
num01 ++;
}
if( board[i][SIZE-i-1] == 1 )
{
numX2 ++;
}
else
{
num02 ++;
}
if ( numX1==SIZE || numX2==SIZE )
{
flag = 1;
}
else if ( num01==SIZE || num02==SIZE )
{
flag = -1;
}
}
//输出结果
if( flag == 0){
System.out.println("no one wins");
}
else if ( flag == 1)
{
System.out.println("x wins");
}
else
{
System.out.println("O wins");
}
}
}
1
多项式加法(5分)
题目内容:
一个多项式可以表达为x的各次幂与系数乘积的和,比如:
2x6+3x5+12x3+6x+20
现在,你的程序要读入两个多项式,然后输出这两个多项式的和,也就是把对应的幂上的系数相加然后输出。
程序要处理的幂最大为100。
输入格式:
总共要输入两个多项式,每个多项式的输入格式如下:
每行输入两个数字,第一个表示幂次,第二个表示该幂次的系数,所有的系数都是整数。第一行一定是最高幂,最后一行一定是0次幂。
注意第一行和最后一行之间不一定按照幂次降低顺序排列;如果某个幂次的系数为0,就不出现在输入数据中了;0次幂的系数为0时还是会出现在输入数据中。
输出格式:
从最高幂开始依次降到0幂,如:
2x6+3x5+12x3-6x+20
注意其中的x是小写字母x,而且所有的符号之间都没有空格,如果某个幂的系数为0则不需要有那项。
输入样例:
6 2
5 3
3 12
1 6
0 20
6 2
5 3
2 12
1 6
0 20
输出样例:
4x6+6x5+12x3+12x2+12x+40
时间限制:500ms内存限制:32000kb
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int count = 0;//统计输入得第几个多项式
int[] list = new int[101];//创建数组
//把输入的多项式相加
do {
int index = in.nextInt();//幂数
int value = in.nextInt();//系数
list[index] += value;
if (index == 0)
{
count++;
}
} while(count < 2);//第二个多项式输完跳出循环
boolean flag=true;//为下面循环做准备
for (int i = 100;i >= 0;i--)
{
if (list[i] != 0)//系数不为零进入循环
{
if (!flag && list[i] > 0)//第一次进入循环为true不带加号
System.out.print("+");
if (i == 0)//幂为零的情况
System.out.print(list[i]);
if(i > 1 && list[i] != 1)
System.out.print(list[i] + "x" + i);
if(i > 1 && list[i] == 1)//系数为1的情况
System.out.print("x" + i);
if(i == 1 && list[i] != 1)//幂数为1的情况下
System.out.print(list[i] + "x");
if(i == 1 && list[i] == 1)//幂数系数都为1的情况
System.out.print("x");
flag = false;//保证第一次循环后上面那个加号可以启用
}
}
if(flag) //如果没有输入输出0
System.out.print(0);
}
}