- 软件的开发分为6个主要的步骤:制定计划、需求分析、软件设计、程序编写、程序测试、运行和维护。前面的总结中已经说了制定计划、需求分析、软件设计这四个步骤,现在我们来说说程序编写。
- 程序编写是为了把用户的要求变为计算机能够"接受"的形式。具体地说,就是为每个模块编写程序,经过软件设计阶段首先得到系统的模块结构,设计阶段结束后得到了系统的模块说明书,这时就可根据模块说明书使用某种程序设计语言编写程序。
- 软件编码需要我们持之以恒的练习,“登高必自卑,行远必自迩”,“滴水穿石,非一日之功”,“天赋决定上限,努力决定下限”,学习编程需要做好艰苦学习的心理准备。编程是一个长期、艰苦的过程,有乐趣,更有挫折!
1、设计素数判定程序。进一步判定100——1000之间的所有素数,并验证哥德巴赫猜想
分析:采用面向对象方法进行设计
设计:一个素数类来进行素数的筛选,Gede类来进行判断,主函数中调用两类来验证哥德巴赫猜想。
public class Sushu {
public void sc(){
int count=0,i,j;
for(i=100;i<=1000;i++){
boolean t=true;
for(j=2;j<i;j++){
if(i%j==0){
t=false;
break;
}
}
if(t==true){
count++;
System.out.print(i+" ");
if(count%10==0)
System.out.println();
t=true;
}
}
}
}
package edau;
import edau.Sushu;
public class Gede {
private int m;
public Gede(int m) {
this.m = m;
}
public Gede(){}
// 判断素数
public boolean sushu(int m){
int i;
for(i=2;i<=(int)(Math.sqrt((double)m));i++)
if(m%i==0)
return false;
return true;
}
//歌德巴赫猜想
public void gede(){
int i;
for (i=2;i<=m-i;i++){
if(sushu(i)&&sushu(m-i))
System.out.println(m+"="+i+"+"+(m-i));
}
}
}
package edau;
import java.util.Scanner;
public class main {
public static void main(String[] args) {
Scanner cin=new Scanner(System.in);
int m;
System.out.println("输入歌德猜想的范围");
m=cin.nextInt();
Gede g=new Gede(m);
g.gede();
Sushu s=new Sushu();
System.out.println("100~1000之间所有的素数为:");
s.sc();
System.out.println();
}
}
2、设计计算s=1+2+3+…+n和值的程序。要求,至少给出5种计算方法。
分析:采用面向对象设计
设计:设计一个shu类,生成从1到n个数,在主类中进行计算。
public class shu {
public shu(){}
public int he1(int n){
int sum=0;
for(int i=1;i<=n;i++)
sum=sum+i;
return sum;
}
package edau;
import java.util.Scanner;
public class N_main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("请输入n的值");
int n=sc.nextInt();
N n1=new N();
System.out.print("方法一的值为:");
System.out.println(n1.he1(n));
System.out.print("方法二的值为:");
System.out.println(n1.he2(n));
System.out.print("方法三的值为:");
System.out.println(n1.he3(n));
System.out.print("方法四的值为:");
System.out.println(n1.he4(n));
System.out.print("方法五的值为:");
System.out.println(n1.he5(n));
sc.close();
}
}
3、设计具有n个实数的数组A,所有元素的和值。要求,采用递归程序。
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int [] arr = new int [n];
for(int i = 0;i < arr.length;i++) {
arr[i] = sc.nextInt();
}
System.out.print(add_Arr(arr,arr.length-1));
}
public static int add_Arr(int [] arr,int n) {
if(n == 0) {
return arr[0];
}else {
return arr[n] + add_Arr(arr,n-1);
}
}
}
4、设计具有n个实数的数组A,求其中最大值的程序。要求,采用递归程序。
import java.util.Scanner;
public class Main{
public static void main(String []args){
Scanner sc = new Scanner(System.in);
int n;
int []a;
a = new int[100];
n = sc.nextInt();
for(int i=0;i<n;i++){
a[i] = sc.nextInt();
}
System.out.println(max(a,n));
}
static int max(int []arr,int n){//方法重载
return max(arr,0,n);
}
static int max(int []arr,int from,int n){//方法重载
if(from==n-1){
return arr[from];//终止语句
}
else{
return Math.max(arr[from],max(arr,from+1,n));//递归关键句
}
}
}
5、计算两个日期之间相差的天数
import java.util.Calendar;
public class DateTest {
public static void main(String[] args) {
Calendar c1=Calendar.getInstance();
c1.set(2020,12,4);
Calendar c2=Calendar.getInstance();
c2.set(2020,12,5);
long t1=c1.getTimeInMillis();
long t2=c2.getTimeInMillis();
long days=(t2-t1)/(24*60*60*1000);
System.out.println("两日期相差的天数为:"+days);
}
}
1、使用for循环、while循环和递归写出3个函数来计算给定数列的总和:
- 计算s=1+2+3+….+100
public class t1 {
private static int[] arr_Ints = { 1,2,3,4,5,6,7,8,9,10 };
public static void main(String[] args) {
System.out.println("The Count is " + getNumByFor() + " .");
System.out.println("The Count is " + getNumByWhile() + " .");
System.out.println("The Count is " + getNumByEcursion(0) + " .");
}
/**
* for 循环
*/
private static int getNumByFor() {
int count = 0;
for (int i = 0; i < arr_Ints.length; i++) {
count += arr_Ints[i];
}
return count;
}
/**
* while 循环
*/
private static int getNumByWhile() {
int count = 0;
int i = 0;
while (i < arr_Ints.length) {
count += arr_Ints[i];
i++;
}
return count;
}
/**
* 递归
*/
private static int getNumByEcursion(int i) {
if (arr_Ints.length == 0)
return 0;
else if (i < arr_Ints.length - 1)
return arr_Ints[i] + getNumByEcursion(i + 1);
else
return arr_Ints[i];
}
}
2、编写一个交错合并列表元素的函数。
- 例如:给定的两个列表为[a,B,C]和[1,2,3],
- 函数返回[a,1,B,2,C,3]
public class t2 {
private static String[] arr1 = { "a", "B", "c", "D", "e" };
private static String[] arr2 = { "1", "2", "3" };
public static void main(String[] args) {
String[] arr = getNum(arr1, arr2);
for (int i = 0; i < arr.length; i++)
System.out.println("The Num is " + arr[i] + " .");
}
private static String[] getNum(String[] arr12, String[] arr22) {
String[] arr = new String[arr1.length + arr2.length];
int i, j;
for (i = 0, j = 0; i < arr1.length; i++) {
j = 2 * i;
if (j > 2 * arr2.length)
j = arr2.length + i;
arr[j] = arr1[i];
}
for (i = 0, j = 0; i < arr2.length; i++) {
j = 2 * i + 1;
if (j > 2 * arr1.length)
j = arr1.length + i;
arr[j] = arr2[i];
}
return arr;
}
}
- 3、编写一个计算前100位斐波那契数的函数。
- 根据定义,斐波那契序列的前两位数字是0和1,随后的每个数字是前两个数字的和。
- 例如,前10位斐波那契数为:0,1,1,2,3,5,8,13,21,34。
public class t3 {
public static void main(String[] args) {
try {
System.out.println("The Nums is " + getCount(100) + " .");
} catch (Exception e) {
}
}
// 获取值
private static int getNum(int num) {
int count = 0;
if (num <= 1)
count = 0;
else if (num == 2)
count = 1;
else
count = getNum(num - 1) + getNum(num - 2);
return count;
}
// 获取和
private static String getCount(int num) {
String strNums = "";
for (int i = 0; i <= num; i++) {
strNums += getNum(i) + ",";
}
strNums = strNums.substring(0, strNums.length()-1);
return strNums;
}
}
- 4、编写一个能将给定非负整数列表中的数字排列成最大数字的函数。
- 例如,给定[50,2,1,9],最大数字为95021
public class t4 {
private static Integer[] VALUES = { 50, 2, 100, 99, 5, 7, 51,50 ,11};
public static void main(String[] args) {
Arrays.sort(VALUES, new Comparator<Object>() {
@Override
public int compare(Object lhs, Object rhs) {
String v1 = lhs.toString();
String v2 = rhs.toString();
return (v1 + v2).compareTo(v2 + v1) * -1;
}
});
String result = "";
for (Integer integer : VALUES) {
result += integer.toString();
}
System.out.println(result);
}
}
- 5、编写一个在1,2,…,9(顺序不能变)数字之间插入+或-或什么都不插入,
- 使得计算结果总是100的程序,并输出所有的可能性。
- 例如:1+2+34–5+67–8+9=100。
import java.util.ArrayList;
public class t5 {
private static int TARGET_SUM = 100;
private static int[] VALUES = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
private static ArrayList add(int digit, String sign, ArrayList branches) {
for (int i = 0; i < branches.size(); i++) {
branches.set(i, digit + sign + branches.get(i));
}
return branches;
}
private static ArrayList f(int sum, int number, int index) {
int digit = Math.abs(number % 10);
if (index >= VALUES.length) {
if (sum == number) {
ArrayList result = new ArrayList();
result.add(Integer.toString(digit));
return result;
} else {
return new ArrayList();
}
}
ArrayList branch1 = f(sum - number, VALUES[index], index + 1);
ArrayList branch2 = f(sum - number, -VALUES[index], index + 1);
int concatenatedNumber = number >= 0 ? 10 * number + VALUES[index] : 10
* number - VALUES[index];
ArrayList branch3 = f(sum, concatenatedNumber, index + 1);
ArrayList results = new ArrayList();
results.addAll(add(digit, "+", branch1));
results.addAll(add(digit, "-", branch2));
results.addAll(add(digit, "", branch3));
return results;
}
public static void main(String[] args) {
ArrayList list = f(TARGET_SUM, VALUES[0], 1);
for(int i=0;i<list.size();i++)
{
System.out.println(list.get(i));
}
}
}
6、设计一个识别出一篇英语文章所有单词的程序,并将这些单词按字母顺序输出
四级英语考试中,阅读理解短文中生词率统计。
要求:根据4级单词大纲,对于一篇阅读理解中生词的多少,决定了文章的难易程度,一般生词率为5%.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
public class english {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("d:\\b.txt"));
StringBuffer sb = new StringBuffer();
String text =null;
while ((text=br.readLine())!= null){
sb.append(text);// 将读取出的字符追加到stringbuffer中
}
br.close(); // 关闭读入流
String str = sb.toString().toLowerCase(); // 将stringBuffer转为字符并转换为小写
String[] words = str.split("[^(a-zA-Z)]+"); // 非单词的字符来分割,得到所有单词
Map<String ,Integer> map = new HashMap<String, Integer>() ;
for(String word :words){
if(map.get(word)==null) // 若不存在说明是第一次,则加入到map,出现次数为1
map.put(word,1);
else
map.put(word,map.get(word)+1); // 若存在,次数累加1
}
// 排序
List<Map.Entry<String ,Integer>> list = new ArrayList<Map.Entry<String,Integer>>(map.entrySet());
Comparator<Map.Entry<String,Integer>> comparator = new Comparator<Map.Entry<String, Integer>>() {
public int compare(Map.Entry<String, Integer> left, Map.Entry<String, Integer> right) {
return (left.getValue().compareTo(right.getValue()));
}
};
// 集合默认升序升序
Collections.sort(list,comparator);
for(int i=0;i<list.size();i++)// 由高到低输出
System.out.println(list.get(list.size()-i-1).getKey() +":"+list.get(list.size()-i-1).getValue());
}
}
1、设计有关的日期函数,并运行测试。
(1)闰年判定:编写一个程序,判定并报告某年是否是闰年。闰年是指年数符合下列两者之一的:
能被4整除,但不能被100整除
能被4整除,又能被400整除
(2)任意输入一个日期,计算该日期是该年的第几天?
(3)任意输入一个日期,计算该日期的下一天的日期?
计算两个日期之间相差的天数
package sdau;
public class riqi {
public static String rn (int y){
String z="";
if((y%4==0&&y%100!=0)||(y%4==0&&y%400==0))
z="是闰年";
else
z="不是闰年";
return z;
}
public static String count(int y,int m,int d){
String z=" ";
int count = 0;
int days = 0;
if (y > 0 && m> 0 && m < 13 && d > 0 && d < 32) {
for (int i = 1; i < m; i++) {
switch (i) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days = 31;
break;
case 4:
case 6:
case 9:
case 11:
days = 30;
break;
case 2: {
if (rn(y)=="是闰年") {
days = 29;
} else {
days = 28;
}
break;
}
}
count = count + days;
}
count = count + d;
z="今天是" + y + "年的第" + count + "天";
} else
z="数据输入错误!";
return z;
}
public static String next(int y,int m,int d){
String z=" ";
if (y > 0 && m> 0 && m < 13 && d > 0 && d < 32) {
if(m==1||m==3||m==5||m==7||m==8||m==10||m==12)
if(d<31) d=d+1;
else {m=m+1;d=1;}
if(m==4||m==6||m==9||m==11)
{
if(d<30) d=d+1;
else {m=m+1;d=1;}
}
if(m==2){
if(d<28) d=d+1;
else if(rn(y)=="是闰年")
d=d+1;
m=m+1;d=1;
}
z="下一天的日期为"+y+"年"+m+"月"+d+"日";
}
else
z="数据输入错误!";
return z;
}
public static void main(String[] args) {
String rn=rn(2001);
System.out.println(rn);
String count=count (2000,10,23);
System.out.println(count);
String next=next (2000,1,31);
System.out.println(next);
}
}
2、关于素数计算问题
(1)判定一个整数x是否为素数?
(2)输入1000以内的所有素数。
(3)验证哥德巴赫猜想。
package sdau;
public class sushu {
public static String isPrime(int n){
String z=" ";
int m = 0;
for (int i = 2; i < n ; i++) {
if(n % i==0)
m++;
}
if (m == 0)
z="是素数";
else
z="不是素数";
return z;
}
public static void out(int n){
for(int i=0;i<=n;i++){
if(isPrime(i)=="是素数")
System.out.println(i);
}
}
public static void yz(int n){
if(n<6||n%2==1){
return;
}
for(int i=2;i<=n-1;i++){
if(isPrime(i)=="是素数" && isPrime(n-i)=="是素数"){
System.out.println(n+"="+i+"+"+(n-i));
}
}
}
public static void main(String[] args) {
String s=isPrime(2);
System.out.println(s);
out(1000);
yz(1000);
}
}
3、输入一组数据(10个数据),找出最大值(并给出最大值所在位置),并输出。
package sdau;
public class shuzu {
public static void max(int a[]){
int max=a[0],p=0;
for(int i=1;i<a.length;i++) {
if(max<a[i]) {
max=a[i];
p=i;
}
}
System.out.println();
System.out.println("最大值为:"+max);
System.out.println("最大值在数组中的位置为:a["+p+"]"+",数组中第"+(p+1)+"个元素");
}
public static void mami(int a[]){
int max = a[0];int m=0;
int min = a[0];int n=0;
for (int i = 0; i < a.length; i++) {
if (a[i] > max) {
max = a[i];
m=i;
}
if (a[i] < min) {
min = a[i];
n=i;
}
}
System.out.println("数组的最大值是:" + max);
System.out.println("最大值在数组中的位置为:a["+m+"]"+",数组中第"+(m+1)+"个元素");
System.out.println("数组的最小值是:" + min);
System.out.println("最小值在数组中的位置为:a["+n+"]"+",数组中第"+(n+1)+"个元素");
}
public static void main(String[] args) {
int a[]={22,12,33,4,15};
max(a);
mami(a);
}
}
4、基于菜单方式设计两个负数的运算(加减乘除)
package sdau;
import java.io.IOException;
import java.util.Scanner;
public class fushu {
public void complexOperation(char operation,double a,double b,double c,double d){
if(operation == '+'){
double temp1 = a + c;
double temp2 = b + d;
System.out.printf("%.2f",temp1);
System.out.print("+");
System.out.printf("%.2f",temp2);
System.out.print("i");
}
if(operation == '-'){
double temp1 = a - c;
double temp2 = b - d;
System.out.printf("%.2f",temp1);
System.out.print("+");
System.out.printf("%.2f",temp2);
System.out.print("i");
}
if(operation == '*'){
double temp1 = a*c - b*d;
double temp2 = a*d + b*c;
System.out.printf("%.2f",temp1);
System.out.print("+");
System.out.printf("%.2f",temp2);
System.out.print("i");
}
if(operation == '/'){
double temp1 = (a*c + b*d)/(c*c + d*d);
double temp2 = (b*c - a*d)/(c*c + d*d);
System.out.printf("%.2f",temp1);
System.out.print("+");
System.out.printf("%.2f",temp2);
System.out.print("i");
}
}
public static void main(String[] args){
fushu test = new fushu();
Scanner in = new Scanner(System.in);
System.out.println("----请选择运算符----");
System.out.println("----+、-、*、/----");
System.out.println("----请输入复数----");
char operation = 0;
try {
operation = (char)System.in.read();
} catch (IOException e) {
e.printStackTrace();
}
double[] temp = new double[4];
for(int i = 0;i < 4;i++){
temp[i] = in.nextDouble();
}
test.complexOperation(operation, temp[0], temp[1], temp[2], temp[3]);
}
}
5、体操运动员参加自由体操决赛,有10个评委为他们打分。选手的成绩计算方法是:10个评委分数中,去掉一个最高分和一个最低分,余下分数的平均值为选手的最后得分。假设有20名选手。
要求,计算出每个选手的成绩,并给出名次,输出格式如下:名次姓名评委成绩1绩(10个成绩依次列出) 最终得分
package sdau;
import java.util.Scanner;
public class BiSai {
public static String mingci(String b[],int a[][] ){
String s="";int []sum=new int[a.length];
double []avg=new double[a.length];
int []r=new int[a.length ];
for(int i=0;i<a.length;i++){
int min=100,max=0;
for(int j=0;j<10;j++){
sum[i]+=a[i][j];
if(a[i][j]>max) max=a[i][j];
if(a[i][j]<min) min=a[i][j];
}
avg[i]=(sum[i]-min-max)*1.0/8;
}
for(int i=0;i<a.length;i++){
r[i]=1;
for(int j=0;j<a.length;j++){
if(avg[i]<avg[j])
r[i]++;
}
}for(int i=0;i<a.length;i++){
s=s+"名次:"+r[i]+"姓名:"+b[i];
for(int j=0;j<10;j++){
s=s+a[i][j]+" ";
}
s=s+avg[i]+'\n';
}
return s;
}
public static void main(String[] args) {
int n=0;
System.out.println("请输入运动员个数:");
Scanner cin=new Scanner(System.in);
n=cin.nextInt();
int [][]a=new int[n][10];
String []b=new String[n];
for(int i=0;i<n;i++){
System.out.println("请输入运动员姓名:");
b[i]=cin.next();
System.out.println("请输入 10个评委的成绩:");
for(int j=0;j<10;j++){
a[i][j]=cin.nextInt();
}
}
System.out.println("最终名次为:"+mingci(b,a));
}
}
6、统计n个实数中正数的平均值和负数的平均值
#include "stdio.h"
void main()
{
int c[10],a[10],b[10],i,j=0,k=0,sum=0;
printf("向数组a中输入10个实型数:");
for(i=0;i<10;i++)
scanf("%d",&a[i]);
for(i=0;i<10;i++)
{
if(a[i]>=0) c[j++]=i;
else b[k++]=i;
}
for(i=0;i<j;i++)
{
sum+=a[c[i]];
}
printf("正数的平均值:%d\n",sum/j);
sum=0;
for(i=0;i<k;i++)
{
sum+=a[b[i]];
}
printf("负数的平均值:%d\n",sum/k);
}