import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 int n = in.nextInt(); int count = 0; for (int i = 1; i <= n; i++) { if (i % 7 == 0) { count++; continue; } int temp = i; boolean flag = false; while (temp > 0) { if (temp % 10 == 7) { flag = true; break; } temp /= 10; } if (flag) { count++; } } System.out.println(count); } }
private static int getCount (int target) { int count = 0; while (target > 6) { if (target % 7 == 0) { count++; target--; continue; } int temp = target; while(temp > 0) { if (temp % 10 == 7) { count++; break; } temp /= 10; } target--; } return count; }
private static int getCount (int target) { int count = 0; while (target > 6) { if (target % 7 == 0) { count++; target--; continue; } int temp = target; int R = 1; boolean flag = false; while(temp > 0) { if (temp % 10 == 7) { flag = true; break; } R *= 10; temp /= 10; } if (flag) { R = (target - (target / R) * R) + 1; target -= R; count += R; } else { target--; } } return count; }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int count = 0; for(int i = 1; i <= n; i++){ if( i % 7 == 0 || (i+"").contains("7")) count++; } System.out.println(count); } }
import java.util.Scanner; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line = br.readLine(); int n = Integer.parseInt(line); int cnt = 0; for (int i = 1; i <= n; i++) { if (String.valueOf(i).contains("7") || i % 7 == 0) { cnt++; } } System.out.println(cnt); } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNextInt()) { // 注意 while 处理多个 case int a = in.nextInt(); int sum = 0; for (int i = 1; i <= a; i++) { if(String.valueOf(i).contains("7")){ sum++; }else { if(i%7==0){ sum++; } } } System.out.println(sum); } } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 int sum=0; int num=in.nextInt(); for(int i=1;i<=num;i++){ if(getResult(i)==true){ sum++; } } System.out.print(sum); } public static boolean getResult(int num){ if(num%7==0){ return true; }else{ String str=num+""; for (char c : str.toCharArray()) { if(c=='7'){ return true; } } } return false; } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 int a = in.nextInt(); int count = 0; for(int i = 1;i <= a;i++){ if(i % 10 == 7 || i % 7 ==0 || String.valueOf(i).contains("7")){ count++; } } System.out.println(count); } }
我这逻辑比最热题解复杂,列举各种情况,结果耗时比最热题解耗时还长! 看来递归是真的耗时,也许方法层级太多真的会拖慢运行速度吧。 public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNextInt()) { int n = in.nextInt(); HashSet<Integer> set = new HashSet<>(); int m= 7; while (m<=n){ set.add(m); m += 7; } m =n; int count =0; while (m>0){ count++; m /= 10; } for (int i = 0; i < count; i++) { test(n, count, i, "", 0, set); } System.out.println(set.size()); } } private static void test(int n, int count, int k, String str, int m, HashSet<Integer> set) { if (m==1 && n/Math.pow(10,count-1)<Integer.parseInt(str)){ return; } if (m>count-1){ int num = Integer.parseInt(str); if (num<=n){ set.add(num); } return; } if (k == m){ test(n, count, k, str+"7", m+1, set); }else { test(n, count, k, str+"0", m+1, set); test(n, count, k, str+"1", m+1, set); test(n, count, k, str+"2", m+1, set); test(n, count, k, str+"3", m+1, set); test(n, count, k, str+"4", m+1, set); test(n, count, k, str+"5", m+1, set); test(n, count, k, str+"6", m+1, set); test(n, count, k, str+"7", m+1, set); test(n, count, k, str+"8", m+1, set); test(n, count, k, str+"9", m+1, set); } }
import java.util.*; //利用位权重跳过部分数据遍历 //如遍历到1700时,直接跳到1800,因为17xx均为目标数 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNext()) { int n =in.nextInt(); int k = 7; int count = 0; out: for (int i = k; i <= n;i++) { if(i%k==0){ count++; continue ; } int index = 1;//从个位开始寻找是否有7,index为每一位的权重 while(index<i){ if(i/(index)%10==k){ int limit = Math.min((i/index+1)*index-1,n);//当前权重上限不能比n大!!!!! count+=limit-i+1;//直接记录i与当前权重上限中有多少个数 i=limit;//直接跳到权重上限 continue out; } index*=10;//权重增加,代表将要判定左边一位的数字 } } System.out.println(count); } } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNextInt()) { // 注意 while 处理多个 case int a = in.nextInt(); int count = 0; for(int i=1; i<=a;i++){ if(i%7==0){ count++; continue; } int x = i; int m = 0; while(x>0){ if(x%10==7){ count++; break; } x=x/10; } } System.out.println(count); } } }
import java.util.Scanner; import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); //7的倍数or 含有7,前者好计算,除法就行 //后者使用记忆化搜索,主动构造出所有可能的数字 Main m = new Main(n); //别忘了加上7的倍数,但是7的倍数里也要有包含7的需要排除 int numHava7 = m.memoryDfs(0, true); //计算是7的倍数,但是不包含数字7的个数 int numNo7 = 0; for (int x = 7; x <= n; x += 7) { if (!String.valueOf(x).contains("7")) { numNo7 += 1; } } System.out.print(numHava7 + numNo7); } public Main(int n) { this.maxStr = String.valueOf(n); this.numOf7 = new int[maxStr.length()]; Arrays.fill(numOf7, -1); this.powOf10 = new int[maxStr.length()]; powOf10[0] = 1; for (int i = 1; i < powOf10.length; ++i) { powOf10[i] = 10 * powOf10[i-1]; } } //需要记忆化的是:没有受到isLimit最大值限制的后续低位包含7的可能个数, //如果当前构造的数字已经包含7了,则只需要加上全部后续的数字的个数 //如果受到isLimit的最大值限制,则需要计算,但不需要记忆,因为这种情况只会出现一次 public int[] numOf7; private int[] powOf10; private String maxStr; private int memoryDfs(int index, boolean isLimit) { if (index == maxStr.length()) { //来到结尾, return 0; } //求 index .. n 存在多少个包含7的数字 if (isLimit || numOf7[index] == -1) { //如果受到最大数限制&nbs***bsp;没有记忆,则计算 int maxCurNum = isLimit ? (maxStr.charAt(index) - '0') : 9; int sumNum = 0; for (int cur = 0; cur <= maxCurNum; ++cur) { //如果当前数字就是7 if (cur == 7) { //如果受到最大值限制,则数量=剩余位的最大值 + 1(包括000) //如果不受到最大值限制,则数量=10^剩余位 String lowMaxStr = maxStr.substring(index+1); int lowMaxInt = "".equals(lowMaxStr) ? 0 : Integer.parseInt(lowMaxStr); sumNum += (isLimit ? (lowMaxInt + 1) : powOf10[maxStr.length()-index-1]); } else { sumNum += memoryDfs(index + 1, isLimit && (cur == maxCurNum)); } } if (!isLimit) { //注意,只记忆不受到最大值限制的结果 numOf7[index] = sumNum; } else { //如果受到最大值限制,直接返回结果了 return sumNum; } } return numOf7[index]; } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); int ans = 0; // 答案 // i从7遍历到n,判断i是否能被7整除,即i % 7 == 0,是则ans+1,否则再判断i是否包含7,是则ans+1 for (int i = 7; i <= n; i++) { // 跳过1-6,与7无关 if (i % 7 == 0) { ans++; } else { // 使用整数的计算,比使用String.contains快 // 通过将cur不断除以10再求余,可获得cur每一位的数 // 如:171,171/10 == 17, 17 % 10 == 7 int cur = i; while (cur > 0) { int mod = cur % 10; if (mod == 7) { ans++; break; } cur /= 10; } } } System.out.println(ans); } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int num = Integer.parseInt(br.readLine()); int arr[] = {10, 100, 1000}; get7(num, arr); // get8(num); } public static void get7(int num, int[] arr){ int count = 0; for (int i = 1; i <= num; ++i){ if (i % 7 == 0){ count++; continue; } for (int j = 0; j < arr.length; ++j){ if (i % arr[0] == 7){ count ++; break; } if ((i / arr[j]) % arr[0] == 7){ count++; break; } } } System.out.println(count); } public static void get8(int num){ int count = 0; String str; for (int i = 1; i <= num; ++i){ str = String.valueOf(i); if (str.contains("7") || i % 7 == 0){ count++; } } System.out.println(count); } }
import java.io.*; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str = br.readLine(); int n = Integer.parseInt(str); int count = 0; for(int i = 1; i <= n; i++){ if(String.valueOf(i).contains("7")||i%7==0){ count++; } } System.out.println(count); } }
//一直在想能不能不遍历或者优化遍历跨度 实在不好想想抄答案 结果大家都是在遍历 那我放心留 import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int res = 0; for (int i = 1; i <= n; i++) { if (String.valueOf(i).contains("7") || i % 7 == 0) { res++; } } System.out.println(res); } }