做题——打卡001

题目1:特别数的和

 方法一:

package 打卡;

import java.util.Scanner;

public class 特别数之和 {
	
	
	public static void main(String[] args) {
		
	 //思路:从1 遍历到n  然后取出每位数字进行比较,符合条件了则统计(只统计一次)
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int sum = 0;
		//遍历
		for (int i = 1; i <= n; i++) {
			//备份i,以免被改变
			int c = i;
			//取数字
			while (c != 0) {
				int m = c % 10;
				if (m == 1 || m == 0 || m == 2 || m == 9) {
					sum += i;
					break;
				}
				c /= 10;
			}
		}
		System.out.println(sum);
	}
}

方法二:

package 打卡;

import java.util.Scanner;

public class 特别数字之和 {
	//思路:用以用字符做法,将数字变成字符,看是否包含 2019 每个字符
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n =sc.nextInt();
		int sum =0 ; 
		for(int i =1 ; i<=n ;i++) {
			String ff =i+"";
			if(ff.contains("0")||ff.contains("1")||ff.contains("2")||ff.contains("9")) {
				sum+=i;
			}
		}
		System.out.println(sum);
	}
}

第二题:四平方和

 

package 打卡;

import java.util.Scanner;

public class 四平方数和 {
	/*
	 * 四平方和定理,又称为拉格朗日定理:
	 * 
	 * 每个正整数都可以表示为至多 4 个正整数的平方和。
	 * 
	 * 如果把 0 包括进去,就正好可以表示为 4 个数的平方和
	 */

//  从0 开始枚举 到根号n  因为最大不能超过根号n 
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		for (int i1 = 0; i1*i1 <= n; i1++) {
			for (int i2 = i1; i2*i2 <= n ; i2++) {
				for (int i3 =i2 ;i3*i3 <= n ; i3++) {
					for (int i4 = i3; i4 *i4<= n; i4++) {
						if (i1 * i1 + i2 * i2 + i3 * i3 + i4 * i4 == n) {
							System.out.printf("%d %d %d %d", i1, i2, i3, i4);
							return;
						}
					}
					
				}
			}
		}
	}
}

第三题:饮料换购

方法一:

package 打卡.标准;


import java.util.Scanner;

public class 饮料换购 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();//输入n
        int res = num;  //备份 num  用于统计换了的饮料
        while(num >= 3){ 
        	res += num/3;//大于三瓶了后 就能多换一瓶  (多喝一瓶)
        	num = num/3 + num % 3;//能换多少瓶 加上剩余的饮料
        }
        System.out.println(res);
	}
}

方法二:

import java.util.Scanner;

public class Min {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
      Scanner sc= new Scanner(System.in);
      int n = sc.nextInt();
      int ans=  n;  

     //看n有多少个三瓶,然后每次增量为3 ,就可以换一瓶,同样又多了个瓶盖 i-- 
      for(int i = 3 ; i <=n ;i+=3){
          ans++;
          i--;
      }
      System.out.println(ans);
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值