JAVA基础语法1

C语言实验——判断素数(循环结构)
Time Limit: 1000 ms Memory Limit: 65536 KiB
Submit Statistic
Problem Description

从键盘上输入任意一个正整数,然后判断该数是否为素数。
如果是素数则输出”This is a prime.”
否则输出“This is not a prime.”
Input

输入任意一个正整数n(1 <= n <= 1000000)。
Output

判断n是否为素数,并输出判断结果:
如果n是素数则输出”This is a prime.”
否则输出“This is not a prime.”
特别提醒:请注意对1的判定,1不是素数。
Sample Input

3
Sample Output

This is a prime.

import java.util.Scanner;
public class Main
{
    public static void main(String[] args) 
    {
         Scanner reader = new Scanner(System.in);
        int n=reader.nextInt();
        if(n==1)
        {
            System.out.println("This is not a prime.");
        }
        else if(n==2)
            System.out.println("This is a prime.");
        else {
            {
                for(int i=2;i<=n-1;i++)
                {
                    if(n%i==0)
                    {
                        System.out.println("This is not a prime.");
                        break;
                    }
                    if(i==n-1&&n%i!=0)
                    {
                        System.out.println("This is a prime.");
                    }
                }
            }
        }

    }
}

C语言实验——求阶乘(循环结构)
Time Limit: 3000 ms Memory Limit: 65536 KiB
Submit Statistic
Problem Description

从键盘输入任意一个大于等于0的整数n,然后计算n的阶乘,并把它输出。
提示: 0!是 1 。
Input

输入任意一个大于等于0的整数n。
Output

输出n!
Sample Input

3
Sample Output

6

import java.util.Scanner;
public class Main
{
    public static void main(String[] args) 
    {
         Scanner reader = new Scanner(System.in);
        int n=reader.nextInt();
        int m=1;
        for(int i=2;i<=n;i++)
        {
            m=m*i;
        }
        System.out.println(m);
    }
}

求绝对值最大值
Time Limit: 1000 ms Memory Limit: 65536 KiB
Submit Statistic
Problem Description

求n个整数中的绝对值最大的数。
Input

输入数据有2行,第一行为n,第二行是n个整数。
Output

输出n个整数中绝对值最大的数。
Sample Input

5
-1 2 3 4 -5
Sample Output

-5

import java.util.Scanner;
public class Main
{
    public static void main(String[] args) 
    {
         Scanner reader = new Scanner(System.in);
        int n=reader.nextInt();
        int max=0;
        int num=0;
        for(int i=1;i<=n;i++)
        {
            int x=reader.nextInt();
            if(x<=0&&-x>max)
            {
                max=-x;
                num=x;
            }
            else if(x>0&&x>max)
            {
                max=x;
                num=x;
            }
        }
        System.out.printf("%d\n",num);
    }
}

C语言实验——圆周率
Time Limit: 1000 ms Memory Limit: 65536 KiB
Submit Statistic
Problem Description

输入n值,并利用下列格里高里公式计算并输出圆周率:

Input

输入公式中的n值。
Output

输出圆周率,保留5位小数。
Sample Input

1
Sample Output

2.66667

import java.util.Scanner;
public class Main
{
    public static void main(String[] args) 
    {
         Scanner reader = new Scanner(System.in);
        double sum=0;
        int n=reader.nextInt();
        for(int i=1;i<=n;i++)
        {
            sum=sum+1/(double)(4*i-3)-1/(double)(4*i-1);
        }
        System.out.printf("%.5f\n",sum*4);
    }
}

简单计算
Time Limit: 1000 ms Memory Limit: 65536 KiB
Submit Statistic
Problem Description

接受从键盘输入的N个整数,输出其中的最大值、最小值和平均值(平均值为整除的商)。

Input

第一行一个正整数N(N<=100);
第二行有N个用空格隔开的整数Ti (1 <= i <= N, 0 <= Ti <= 10000000)
Output

三个有空格隔开的整数分别为最大值、最小值和平均值,其中平均值为整除的商。
Sample Input

5
1 2 3 5 4
Sample Output

5 1 3

import java.util.Scanner;
public class Main
{
    public static void main(String[] args) 
    {
         Scanner reader = new Scanner(System.in);
         int N=reader.nextInt();
         int max=0,sum=0,averge,min=10000000;
         for(int i=1;i<=N;i++)
         {
             int x=reader.nextInt();
             if(x>max)max=x;
             if(x<min)min=x;
             sum=sum+x;
         }
         averge=sum/N;
         System.out.printf("%d %d %d\n",max,min,averge);
    }
}

平方数
Time Limit: 1000 ms Memory Limit: 65536 KiB
Submit Statistic
Problem Description

飞飞特别喜欢平方数,可是他数学并不好,你能帮他计算n与m之间所有平方数之和吗?
提示:若一个整数的开方还是整数,它就是平方数。例如:4、9、16、25是平方数。
Input

第一行 T 代表数据的组数。
接下来有 T 行,每行两个整数n,m (0 <= n, m <= 100000000)
Output

输出一个整数,代表所求区间内平方数之和。
Sample Input

3
1 4
3 10
17 20
Sample Output

5
13
0

import java.util.Scanner;
public class Main
{
    public static void main(String[] args) 
    {
         Scanner reader = new Scanner(System.in);
        int N=reader.nextInt();
        for(int i=1;i<=N;i++)
        {
            int sum=0;
            int m=reader.nextInt();
            int n=reader.nextInt();
            int t;
            if(m<n)
            {
            t=m;m=n;n=t;    
            }
            for(int j=n;j<=m;j++)
            {
                for(int k=1;k<=j;k++)
                {
                    if(k*k==j)
                    {
                        sum=sum+j;
                    }
                }
            }
            System.out.println(sum);
        }
    }
}

C/C++练习7—求某个范围内的所有素数
Time Limit: 1000 ms Memory Limit: 65536 KiB
Submit Statistic
Problem Description

求小于n的所有素数,按照每行10个显示出来。
Input

输入整数n(n<10000)。
Output

每行10个依次输出n以内的所有素数。如果一行有10个素数,每个素数后面都有一个空格,包括每行最后一个素数。
Sample Input

100
Sample Output

2 3 5 7 11 13 17 19 23 29
31 37 41 43 47 53 59 61 67 71
73 79 83 89 97
Hint

import java.util.Scanner;
public class Main
{
    public static void main(String[] args) 
    {
         Scanner reader = new Scanner(System.in);
        int n=reader.nextInt();
        int a=0;
        for(int j=2;j<=n;j++)
        {
            int flag=0;
            for(int i=2;i<j;i++)
            {
                if(j%i==0)
                    {  
                       flag=1;
                       break;
                    }

            }
            if(flag==0)
            {
                System.out.printf("%d ",j);
                a++;
                if(a%10==0)
                {
                    System.out.println();   
                }
            }
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

奈何碎银没有几两

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值