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(String.valueOf(i).contains("7")||i%7==0 ){
count++;
// System.out.println(i);
}
}
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 n = in.nextInt();
int count = 0;
if (n<7) {
System.out.println(0);
continue;
}
for (int i = 6;i <=n;i++){
String str = i+"";
if (i % 7 == 0 || str.indexOf('7') !=-1){//7的倍数
count++;
}
// for (char c: str.toCharArray()){//含有7
// if (c=='7'){
// count++;
// break;
// }
// }
}
System.out.println(count);
}
}
} import java.util.Scanner;
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||String.valueOf(i).indexOf('7')!=-1){
count++;
}
}
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 的区别
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];
}
}