1035 topic description
Known: Sn=1+1/2+1/3+…+1/n. Obviously for any integer k, when n is large enough, S_n>k.
Given an integer k, it is required to calculate a minimum n such that S_n>k.
input format
A positive integer k.
output format
A positive integer n.
Input and output samples
enter
1
output
2
Instructions/Tips
【data range】
For 100% of the data, 1≤k≤15.
analyze:
Enter a positive integer K
S_n = 1+ 1/2 + 1/3 +...+1/n;
such that S_n>K
output a positive integer n
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double sum=1;
int k = sc.nextInt();
for (int i = 2; i <= 9999999 ; i++) {
sum+=(double)1/i;
if(sum>k){
System.out.println(i);
break;
}
}
}
}
1980 Title Description
Try to count how many times the number x appears among all integers in the interval 1 to n? For example, in 1 to 11, that is, in 1,2,3,4,5,6,7,8,9,10,11, the number 11 occurs 4 times.
input format
2 integers n and x are separated by a space.
output format
1 integer representing the number of occurrences of x.
Input and output samples
enter
11 1output
4
Instructions/Tips
For 100\%100% of the data 1≤n≤10^6
analyze:
Here, it is only necessary to judge the remainder of each judgment to judge the value of the single digit.
y=i;
while(y>0){
if(y%10==x) {
sum++;
}
y/=10;
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int sum=0;
int y;
int n = sc.nextInt();
int x = sc.nextInt();
for (int i = 1; i <=n ; i++) {
y=i;
while(y>0){
if(y%10==x) {
sum++;
}
y/=10;
}
}
System.out.println(sum);
}
}