小鑫数数儿
Time Limit: 1000 ms Memory Limit: 65536 KiB
Submit Statistic
Problem Description
某天小鑫忽然得到了许多的数字,他很好学,老师给他布置了一个任务,求出这些数字中,小于他们平均数、等于他们平均数、大于他们平均数的数字的数量是多少。(对于出现的平均数,保证都是整数,不会出现浮点数)
Input
多组输入。
对于每次的输入,第一行一个整数N(1 <= N <= 10),代表数字的个数。
接下来的一行,输入N个整数M(0 <= M <= 100)。
Output
输出包含三个数,第一个跟第二个数后面是空格,最后一个数后面是换行。
第一个数是这些数字中小于他们平均数的数字的个数,第二个数为等于他们平均数的数字的个数,第三个数为大于他们平均数的数字的个数。
Sample Input
3
1 2 3
5
2 4 4 5 5
Sample Output
1 1 1
1 2 2
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner reader=new Scanner(System.in);
while(reader.hasNext())
{
int N=reader.nextInt();
int A[]=new int [101];
for(int i=1;i<=N;i++)
{
A[i]=reader.nextInt();
}
int sum=0;
for(int i=1;i<=N;i++)
{
sum=sum+A[i];
}
int AVE=sum/N;
int N1=0,N2=0,N3=0;
for(int i=1;i<=N;i++)
{
if(A[i]>AVE)N1++;
else if(A[i]==AVE)N2++;
else N3++;
}
System.out.println(N3+" "+N2+" "+N1);
}
reader.close();
}
}
编码
Time Limit: 1000 ms Memory Limit: 65536 KiB
Submit Statistic
Problem Description
给你一个由大写字母组成的组成的字符串,你可以用如下规则对其进行编码:
1、 包含K个相同字母的连续字符串可以用KX表示,其中X是相同的字母。
2、 如果K为1,不输出K
Input
输入有多组,直到文件结束。每组一个字符串,长度为10000以内
Output
输出编码后的字符串。
Sample Input
ABC
ABBCCC
Sample Output
ABC
A2B3C
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner reader=new Scanner(System.in);
while(reader.hasNext())
{
String S=reader.next();
int count=0;
char A[]=new char [100000];
for(int i=0;i<S.length();i++)
{
A[i]=S.charAt(i);
}
for(int i=0;i<S.length();i++)
{
if(A[i+1]==A[i])
{
count++;
}
else if(A[i+1]!=A[i])
{
if(count==0)
{
System.out.print(A[i]);
}
else
{
System.out.print(count+1);
System.out.print(A[i]);
count=0;
}
}
}
System.out.println();
}
reader.close();
}
}
C语言实验——余弦
Time Limit: 1000 ms Memory Limit: 65536 KiB
Submit Statistic
Problem Description
输入n的值,计算cos(x)。
Input
输入数据有多行,每行两个数,包括x和n。第一数据为x,第二个数据为n。
Output
输出cos(x)的值,保留4位小数。
Sample Input
0.0 100
1.5 50
Sample Output
1.0000
0.0707
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner reader=new Scanner(System.in);
while(reader.hasNext())
{
double x=reader.nextDouble();
int n=reader.nextInt();
double sum=1;
double A[]=new double [10000];
for(int i=1;i<=n;i++)
{
double sum1=1,sum2=1;
for(int j=1;j<=2*i;j++)
{
sum1=sum1*x;
}
for(int j=1;j<=2*i;j++)
{
sum2=sum2*j;
}
A[i]=sum1/sum2;
}
for(int i=1;i<=n;i++)
{
if(i%2==0)
{
sum=sum+A[i];
}
else
{
sum=sum-A[i];
}
}
System.out.printf("%.4f\n",sum);
}
reader.close();
}
}
进制转换
Time Limit: 1000 ms Memory Limit: 65536 KiB
Submit Statistic
Problem Description
输入一个十进制数N,将它转换成R进制数输出。
Input
输入数据包含多个测试实例,每个测试实例包含两个整数N(32位整数)和R(2<=R<=16, R != 10)。
Output
为每个测试实例输出转换后的数,每个输出占一行。如果R大于10,则对应的数字规则参考16进制(比如,10用A表示,等等)。
Sample Input
7 2
23 12
-4 3
Sample Output
111
1B
-11
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner reader=new Scanner(System.in);
while(reader.hasNext())
{
int m=reader.nextInt();
int n=reader.nextInt();
System.out.println(Integer.toString(m,n).toUpperCase());
}
reader.close();
}
}
简单枚举类型——植物与颜色
Time Limit: 1000 ms Memory Limit: 65536 KiB
Submit Statistic
Problem Description
请定义具有red, orange, yellow, green, blue, violet六种颜色的枚举类型color,根据输入的颜色名称,输出以下六种植物花朵的颜色:
Rose(red), Poppies(orange), Sunflower(yellow), Grass(green), Bluebells(blue), Violets(violet)。如果输入的颜色名称不在枚举类型color中,例如输入purple,请输出I don’t know about the color purple.
Input
输入数据有多行,每行有一个字符串代表颜色名称,颜色名称最多30个字符,直到文件结束为止。
Output
输出对应颜色的植物名称,例如:Bluebells are blue. 如果输入的颜色名称不在枚举类型color中,例如purple, 请输出I don’t know about the color purple.
Sample Input
blue
yellow
purple
Sample Output
Bluebells are blue.
Sunflower are yellow.
I don’t know about the color purple.
Hint
请用枚举类型实现。
import java.util.Scanner;
enum Color
{
red("Rose", "red"), orange("Poppies", "orange"), yellow("Sunflower", "yellow"), green("Grass", "green"), blue("Bluebells", "blue"), violet("Violets", "violet");
String name;
String color;
Color(String name, String color)
{
this.name = name;
this.color = color;
}
public String toString()
{
return name + " are " + color + ".";
}
}
public class Main
{
public static void main(String[] args)
{
Scanner reader = new Scanner(System.in);
while(reader.hasNext())
{
String incolor = reader.nextLine();
try
{
Color color = Color.valueOf(incolor);
System.out.println(color);
}
catch(Exception e)
{
System.out.println("I don't know about the color "+incolor+".");
}
}
reader.close();
}
}
汉诺塔
Time Limit: 1000 ms Memory Limit: 65536 KiB
Submit Statistic
Problem Description
汉诺塔(又称河内塔)问题是印度的一个古老的传说。
开天辟地的神勃拉玛在一个庙里留下了三根金刚石的棒A、B和C,A上面套着n个圆的金片,最大的一个在底下,其余一个比一个小,依次叠上去,庙里的众僧不倦地把它们一个个地从A棒搬到C棒上,规定可利用中间的一根B棒作为帮助,但每次只能搬一个,而且大的不能放在小的上面。
僧侣们搬得汗流满面,可惜当n很大时这辈子恐怕就很搬完了。
聪明的你还有计算机帮你完成,你能写一个程序帮助僧侣们完成这辈子的夙愿吗?
Input
输入金片的个数n。这里的n<=10。
Output
输出搬动金片的全过程。格式见样例。
Sample Input
2
Sample Output
Move disk 1 from A to B
Move disk 2 from A to C
Move disk 1 from B to C
import java.util.Scanner;
public class Main
{
public void mov(int n,char A,char B,char C)
{
if(n==1)
{
System.out.printf("Move disk %d from %c to %c\n",n,A,C);
}
else
{
mov(n-1, A, C, B);
System.out.printf("Move disk %d from %c to %c\n",n,A,C);
mov(n-1, B, A, C);
}
}
public static void main(String[] args)
{
Scanner reader = new Scanner(System.in);
int n=reader.nextInt();
Main mov=new Main();
mov.mov(n,'A','B','C');
reader.close();
}
}
计算组合数
Time Limit: 1000 ms Memory Limit: 32768 KiB
Submit Statistic
Problem Description
计算组合数。C(n,m),表示从n个数中选择m个的组合数。
计算公式如下:
若:m=0,C(n,m)=1
否则, 若 n=1,C(n,m)=1
否则,若m=n,C(n,m)=1
否则 C(n,m) = C(n-1,m-1) + C(n-1,m).
Input
第一行是正整数N,表示有N组要求的组合数。接下来N行,每行两个整数n,m (0 <= m <= n <= 20)。
Output
输出N行。每行输出一个整数表示C(n,m)。
Sample Input
3
2 1
3 2
4 0
Sample Output
2
3
1
import java.util.Scanner;
public class Main {
public int c(int n,int m)
{
if(m==0) return 1;
else
{
if(n==1) return 1;
else
{
if(m==n) return 1;
else return c(n-1,m-1)+c(n-1,m);
}
}
}
public static void main(String[] args) {
Scanner reader=new Scanner(System.in);
int t=reader.nextInt();
for(int i=0;i<t;i++)
{
int n=reader.nextInt();
int m=reader.nextInt();
Main c=new Main();
int a=c.c(n,m);
System.out.println(a);
}
reader.close();
}
}
C/C++程序训练6—歌德巴赫猜想的证明
Time Limit: 1000 ms Memory Limit: 65536 KiB
Submit Statistic
Problem Description
验证“每个不小于6的偶数都是两个素数之和”,输入一个不小于6的偶数n,找出两个素数,使它们的和为n。
Input
输入一个不小于6的偶数n。
Output
找出两个素数,使它们的和为n。只需要输出其中第一个素数最小的一组数据即可。
Sample Input
80
Sample Output
80=7+73
import java.util.Scanner;
public class Main
{
public int su(int n)
{
int i,flag=1;
for(i=2;i<n;i++)
{
if(n%i==0)
{
flag=0;
break;
}
}
if(n==1)
{
flag=0;
}
if(flag==1)
return 1;
else
return 0;
}
public static void main(String[] args)
{
Scanner reader = new Scanner(System.in);
int n,i,j=0;
n=reader.nextInt();
Main su=new Main();
for(i=2;i<n;i++)
{
if(su.su(i)==1&&su.su(n-i)==1)
{
j=i;
break;
}
}
System.out.printf("%d=%d+%d\n",n,j,n-j);
reader.close();
}
}
分段函数
Time Limit: 1000 ms Memory Limit: 65536 KiB
Submit Statistic
Problem Description
函数是一种特殊的映射,即数集到数集的映射。对于给定的每个自变量都能给出一个确定的值,这是一件多么牛的事情呀。其实不是函数牛,而是因为它具有这种性质我们的数学家才这么定义了它。函数有很多类型,虽然本质都是映射,但为了方便研究和应用,数学家们做了很多分类。比如线性函数,非线性函数,随机函数,还有一些具有特殊性质的函数等等。
今天我们要关注的是分段函数,所谓分段就是对于整个定义域来说,函数的值域是不连续的。很明显的一个就是绝对值函数,类似于y=|x|,(x,y属于R)。不连续是按照自变量的连续变化函数值的变化不连续而已,但函数仍然不离不弃的给了每个自变量一个值。
总之,函数就是按照规则对自变量进行操作得到相应的值。而程序里的函数就更牛了,它可以对我们的输入(自变量)进行各种我们想做的操作,最后得到输出(值),很好玩吧。
今天,就希望你能用程序里的函数实现数学里的分段函数,加油哦。
这个分段函数长得是这个样子的:
F(x) = log2(x) 0<x<10
= |x|+sin(x) x<0
=0 x=0
=x^2 x>=10
Input
输入包含多组。
第一行给出数据的组数T。
接下来T行每行一个实数X。
Output
输出T行,每行一个函数值,四舍五入保留到小数点后两位。
希望你能根据函数的表达式,对于给定的每个自变量不离不弃的计算出它的值。
Sample Input
4
0
10
5
-1
Sample Output
0.00
100.00
2.32
0.16
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner reader = new Scanner(System.in);
while(reader.hasNext())
{
int T,i;
double x,y;
T=reader.nextInt();
for(i=0;i<T;i++)
{
x=reader.nextDouble();
if(x>0&&x<10)
{
y=Math.log(x)/Math.log((double)2);
}
else if(x<0)
{
y=-1*x+Math.sin(x);
}
else if(x==0)
{
y=0;
}
else
{
y=x*x;
}
System.out.printf("%.2f\n",y);
}
}
reader.close();
}
}
字符的变化
Time Limit: 1000 ms Memory Limit: 65536 KiB
Submit Statistic
Problem Description
一个长度为n(1<=n<=1000)的字符串(只包含小写字母),然后对这个字符串进行操作,一次操作为:a->b,b->c,c->d…,z->a; 输出经过m次操作之后的字符串。(例如 abcde 经过一次操作之后变为 bcdef)。
Input
单组输入。测试用例的第一行为字符串s,第二行为一个整数m(0<=m<=1000)。
Output
输出经过m次操作之后的字符串。
Sample Input
xyzcd
2
Sample Output
zabef
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner reader = new Scanner(System.in);
String S=reader.next();
int n=reader.nextInt();
int A[]=new int [1001];
for(int i=0;i<S.length();i++)
{
A[i]=S.charAt(i);
}
for(int j=1;j<=n;j++)
{
for(int i=0;i<S.length();i++)
{
if(A[i]=='z')
{
A[i]='a';
}
else
{
A[i]=A[i]+1;
}
}
}
for(int i=0;i<S.length();i++)
{
System.out.print((char)A[i]);
}
reader.close();
}
}