洛谷入门3(1)

题目P5719

注意保留小数位数的方法

import java.util.*;
public class Main {
	public static void main(String[]args) {
		Scanner cin=new Scanner(System.in);
		int n=cin.nextInt();
		int k=cin.nextInt();
		int numa=0,numb=0,na=0,nb=0;
		for(int i=1;i<=n;i++) {
			if(i%k==0) {
				numa+=i;//相加求和
				na++;//数字个数增加
			}else {
				numb+=i;
				nb++;
			}
		}
		float da=(float)numa/na;
		float db=(float)numb/nb;
	    String sa=String.format("%.1f", da);//保留数字
		String sb=String.format("%.1f", db);
		System.out.print(sa+" "+sb);
	}
}

P5721

 

import java.util.Scanner;
public class Main {
	public static void main(String[]args) {
		Scanner cin=new Scanner(System.in);
		int n=cin.nextInt();//输入的数字L
		int sum=1;//质数之和,可以与n进行判断
		for(int a=n;a>=1;a--) {//将输入的数字进行赋值给a,一个控制列
			for(int b=1;b<=a;b++) {//一个控制行
				if(sum<=9) {//当a是一位数的时候,进行加0变为两位数
					System.out.print("0"+sum);
				}
				else {//本来就是两位数的时候,则直接相连接
					System.out.print(+sum);
				}
				sum++;
			}
			System.out.println();
		}
	}

P1009

 注意高精度计算的运用

方法一

import java.math.BigInteger;//高精度计算时所用
import java.util.Scanner;
public class Main {
	public static void main(String[]args) {
		Scanner cin=new Scanner(System.in);
		int n=cin.nextInt();//输入的正整数
		BigInteger total=new BigInteger("0");//定义高精度的数
		for(int i=1;i<=n;i++) {//i的递增
			total=total.add(f(i));//一个相加的函数
		}
		System.out.print(total);
	}
	private static BigInteger f(int a) {
		BigInteger sum=new BigInteger("1");
		for(int i=a;i>=1;i--) {
			sum=sum.multiply(BigInteger.valueOf(i));
		}
		return sum;
	}
}

方法二

import java.math.BigInteger;
import java.util.*;
public class Main {
	public static void main(String[]args) {
		Scanner cin=new Scanner(System.in);
		int n=cin.nextInt();
		BigInteger S=new BigInteger("0");//定义高精度的数
		BigInteger c=new BigInteger("1");
		//外循环将内循环算出的每一个数的阶乘相加到一起
		for(int i=1;i<=n;i++) {
			for(int j=1;j<=i;j++) {//内循环让数相乘 计算阶乘
				c=c.multiply(new BigInteger(j+""));
			}
			S=S.add(c);
		    c=new BigInteger("1");
		}
		System.out.print(S);
	}
}

P1980

 方法一

import java.util.Scanner;
public class Main {
  public static void main(String[] args) {
      Scanner sc = new Scanner(System.in);
      int n = sc.nextInt(), //输入的两个数字
      	  x = sc.nextInt(), 
      	ans = 0, m = 1;//定义的两个变量
      while(m <= n ){
    	  //对数字进行分解
          int a = n / (m * 10), b = n / m % 10, c = n % m;
          if(x != 0) {//x不等于0的时候
              if(b < x) ans += a * m;//
              if(b == x) ans += a * m + c + 1;
              if(b > x) ans += (a + 1) * m;
          } else {
              if(b != 0) ans += a * m;
              else ans += (a - 1) * m + c + 1;
          }
          m *= 10;
      }
      System.out.println(ans);
  }
}

方法二

import java.util.Scanner;
import java.util.*;
public class Main {
	public static void main(String[]args) {
		Scanner cin=new Scanner(System.in);
		int n=cin.nextInt();//输入的n整数
		int x=cin.nextInt();//出现次数的数字为要查找的数字
		int count=0;//出现的次数
		for(int i=1;i<=n;i++) {//i随着n的变化而变化,表示1~n之间的数字
			int t=i;//将i的值赋值给t
			while(t>0) {//当值>0,进行循环
//一个数中包含了好几个要查找的数字,就要不断的进行去掉百位十位个位去看要没有要查找的数字
				if(t%10==x) {
					count++;
				}
			//一直在用t判断if(t%10==x),用t=t/10去掉百位,十位..
				t=t/10;
			}
		}
		System.out.println(count);
	}
}

P1035

 方法一

import java.util.Scanner;
public class Main {
	public static void main(String[]args) {
		Scanner cin=new Scanner(System.in);
		int k=cin.nextInt();
		double n=1;
		double b=0;
		//开始b小于k的时候同时排除了下面n为0的情况
		while(!(b>k)) {
			b=b+1/n;//开始下面除n
			n++;//开始累增循环
		}
		System.out.println((int)(n-1));
	}
}

方法二

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        int k=cin.nextInt();
        double sum=0;
        int a=0;
        for(int i=1;;i++) {
        	sum+=(double)1/i;//累加求和
        	if(sum>k) {
        		a=i;
        		break;
        	}
        }
     	System.out.print(a);	
    }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值