HW-找7(测试ok满分注意小于等于30000的条件)
输出7有关数字的个数,包括7的倍数,还有包含7的数字(如17,27,37...70,71,72,73...)的个数 |
|
知识点 | 循环 |
---|---|
运行时间限制 | 0M |
内存限制 | 0 |
输入 |
一个正整数N。(N不大于30000) |
输出 |
不大于N的与7有关的数字个数,例如输入20,与7有关的数字包括7,14,17. |
样例输入 | 20 |
样例输出 | 3 |
import java.util.Scanner; import java.util.Vector; public class Main{ static int count; public static void main(String[] args) { Scanner scStr = new Scanner(System.in); //从键盘获取字符串 count= scStr.nextInt(); //将Scanner对象中的内容以字符串的形式取出来 int count1=count;int count2=0; Vector<Integer> store=new Vector<Integer>(); if(count1<=30000) { for(int i=1;i<=count1;i++){ if(i%7==0){ store.addElement(i); count2++; } else{ //将i拆分成字符串形式 int sqy=0;int mxf=0; mxf=i; while(true){ sqy=mxf-(mxf/10)*10; if(sqy==7){ store.addElement(i); count2++; break; } //一位一位开始break,一旦各位开始就break mxf=(mxf/10); if(mxf==0){ //如果为0跳出循环 break; } } }//else } System.out.println(count2); } //if } }
用自带包含的函数
将整形转换成字符串
import java.util.Scanner; public class Main{ public static void main(String[] args) { //System.out.println("请输入数字:"); Scanner scStr = new Scanner(System.in); //从键盘获取字符串 int count= scStr.nextInt(); //将Scanner对象中的内容以字符串的形式取出来 int count2=0; if(count<=30000) { for(int i=1;i<=count;i++){ int flag=0; String str=Integer.toString(i); for(int j=0;j<str.length();j++){ if(str.charAt(j)=='7') flag=1; } if(i%7==0||flag==1){ //是或者的关系 count2++; } } } System.out.println(count2); } }
每天都做更好的自己,不纠结于输赢成败