Gym 100095 Problem - Automated Telephone Exchange
题目类型:思维
题意
有一种电话号码的格式为 xxx-xx-xx ,当第一部分构成的十进制数减去后两部分构成十进制数的和后结果为 0 时则称这个电话号码为幸运的。现在给出一个电话号码的第一部分,求由该部分构成的 10000 个电话号码中为幸运号码的个数。
分析
因为后两部分构成的和的区间为 [0, 198],所以当 n 的值不在这个区间时则结果一定为 0,否则进行枚举。我们从 i = 0 开始枚举,一直枚举到 i = 99, 每次枚举时判断 n - i 的值是否在区间 [0, 99],如果在则 ans++。
具体细节参考代码
代码
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class Main {
static final int MAX_N = 100010;
static final int INF = 0x3f3f3f3f;
static final int mod = 1000000007;
public static void main(String[] args) throws IOException {
initReader(System.in);
solve();
printWriter.flush();
}
/*******************************************************************************************************************************/
public static void solve() throws IOException {
int n = nextInt();
if (n > 99 + 99) {
printWriter.println(0);
return;
}
int ans = 0;
for (int i = 0; i < 100; i++) {
int t = n - i;
if (t >= 0 && t <= 99) {
ans++;
}
}
printWriter.println(ans);
}
/*******************************************************************************************************************************/
static BufferedReader reader;
static StringTokenizer tokenizer;
static PrintWriter printWriter;
public static void initReader(InputStream input) throws IOException {
// reader = new BufferedReader(new InputStreamReader(input));
// tokenizer = new StringTokenizer("");
// printWriter = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
reader = new BufferedReader(new FileReader("ate.in"));
tokenizer = new StringTokenizer("");
printWriter = new PrintWriter(new BufferedWriter(new FileWriter("ate.out")));
}
public static boolean hasNext() {
try {
while (!tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
} catch (Exception e) {
return false;
}
return true;
}
public static String next() throws IOException {
while (!tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
public static String nextLine() {
try {
return reader.readLine();
} catch (Exception e) {
return null;
}
}
public static int nextInt() throws IOException {
return Integer.parseInt(next());
}
public static long nextLong() throws IOException {
return Long.parseLong(next());
}
public static double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public static char nextChar() throws IOException {
return next().charAt(0);
}
}