蓝桥杯训练题 (一)

基础题

1. Hello, world!

题目描述

This is the first problem for test. Since all we know the ASCII code, your job is simple: Input numbers and output corresponding messages.

输入

The input will contain a list of positive integers separated by whitespaces(spaces, newlines, TABs). Please process to the end of file (EOF). The integers will be no less than 32.

输出

Output the corresponding message. Note there is NOT a newline character in the end of output.

样例输入

72 101 108 108 111 44
32 119 111 114 108 100 33

样例输出

Hello, world!

c语言编写

#include<stdio.h>
int main() {
 char c;
 int a[100], i = 0;
 while ((scanf("%d", &a[i])) != EOF) {//需要换行输入ctrl+z再回车
 i++;
 }
 for (int j = 0; j < i; j++) {
 printf("%c", a[j]);
 }
 return 0;
}

EOF,这里在专门的题训练系统里面才有效,编译器中无效
就像java的 sc.hasnext()一样。。emm

import java.util.Scanner;
/**
 * @author 小喵钓鱼
 * @date 2020-02-19 13:55
 * @veision 1.10
 */
public class Pratice {
 public static void main(String[] args) {
 // TODO Auto-generated method stub
 Scanner scanner = new Scanner(System.in);
 while (scanner.hasNext())
 {
 int a = scanner.nextInt();
 System.out.print((char)a);
 }
 }
}

2.用筛法求之N内的素数。

题目描述

用筛法求之N内的素数。

输入

N

输出

0~N的素数

样例输入

100

样例输出

2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97

#include<stdio.h>
#include<math.h>
int su(int n);
int main(void) {
 int n;
 scanf("%d", &n);
 for (int i = 2; i < n; i++)
 {
 if (su(i) != -1)
 {
 printf("%d\n", su(i));
 }
 }
}
// 素数就是 只被自己 和 1整除的数
int su(int n)
{
 int i;
 int k = (int)sqrt((double)n);
 for (i = 2; i <= k; i++)
 {
 if (n % i == 0)
 {
 break;
 }
 }
 if (i > k)
 {
 return n;
 }
 return -1;
}

3.字符逆序

#include <stdio.h>
#include <string.h>
int main(void)
{
 char strinput[100];
 scanf("%[^\n]", strinput); //除了换行符以外的字符全部接收
 char stroutput[100];
 int i = 0;
 int j = 0;
 int len = strlen(strinput);
 //逆序拷贝
 for (i = len - 1; i >= 0; i--)
 {
 stroutput[j++] = strinput[i];
 }
 stroutput[j] = '\0';
 printf("%s\n", stroutput);
 return 0;
}
import java.util.Scanner;
/**
 * @author 小喵钓鱼
 * @date 2020-02-19 13:55
 * @veision 1.10
 */
public class Pratice {
 public static void main(String[] args) {
 // TODO Auto-generated method stub
 Scanner scanner = new Scanner(System.in);
 String str = scanner.nextLine();
 for (int i = str.length() - 1; i >= 0; i--)
 {
 System.out.print(str.charAt(i));
 }
 System.out.println();
 }
}
```java

#### [蓝桥杯][2013年第四届真题]公式求值
时间限制: 1Sec 内存限制: 128MB 提交: 1131 解决: 13

题目描述

输入n,  m,  k,输出下面公式的值。  

其中C_n^m是组合数,表示在n个人的集合中选出m个人组成一个集合的方案数。组合数的计算公式如下:

数据规模和约定  
对于100%的数据,n在十进制下不超过1000位,即1≤n< 10^1000,1≤k≤1000,同时0≤m≤n,k≤n。  
提示  
999101是一个质数;  
当n位数比较多时,绝大多数情况下答案都是0,但评测的时候会选取一些答案不是0的数据;

输入

输入的第一行包含一个整数n;第二行包含一个整数m,第三行包含一个整数k。

输出

计算上面公式的值,由于答案非常大,请输出这个值除以999101的余数。 

样例输入

3 
1 
3

样例输出

162
```java
// 第一次尝试
import java.util.Scanner;
 public class Main {
 public static void main(String[] args) {
 int n, m, k;
 Scanner sc = new Scanner(System.in);
 String blank;
 n = sc.nextInt();
 blank = sc.nextLine();
 m = sc.nextInt();
 blank = sc.nextLine();
 k = sc.nextInt();
 long sum = 0;
 for (int i = 0; i <= n; i++)
 {
 sum += calcu(i, n) * calcu(m, n) * Math.pow(i, k);
 }
 System.out.println(sum % 999101);
 }
 public static long calcu(int m, int n)
 {
 long sum = 0;
 sum = Jie(n) / (Jie(m) * Jie(n - m));
 return sum;
 }
 public static long Jie(int n)
 {
 long sum = 1;
 if(n == 0)
 return 1;
 for (int i = 1; i <= n; i++)
 {
 sum *= i;
 }
 return sum;
 }
 }

逻辑上没有问题,就是数太大了,自动归零了,导致错误。

问题 1427: [蓝桥杯][2013年第四届真题]买不到的数目

时间限制: 1Sec 内存限制: 128MB 提交: 3255 解决: 1685

题目描述

小明开了一家糖果店。他别出心裁:把水果糖包成4颗一包和7颗一包的两种。糖果不能拆包卖。
小朋友来买糖的时候,他就用这两种包装来组合。当然有些糖果数目是无法组合出来的,比如要买  10  颗糖。
你可以用计算机测试一下,在这种包装情况下,最大不能买到的数量是17。大于17的任何数字都可以用4和7组合出来。
本题的要求就是在已知两个包装的数量时,求最大不能组合出的数字。

输入

两个正整数,表示每种包装中糖的颗数(都不多于1000)

输出

一个正整数,表示最大不能买到的糖数

样例输入

4 7

样例输出

17

 import java.util.Scanner;
 /**
 * @author 小喵钓鱼
 * @date 2020-02-19 16:46
 * @veision 1.10
 */
 public class A2013_4 {
 public static void check(int m, int n)
 {
 // 最大组合数是不超过 m * n 的
 if (m > n)
 {
 int temp = m;
 m = n;
 n = m;
 }
 for (int i = m * n; i > 0; i--)
 {
 if (!T(m, n , i))
 {
 System.out.println(i);
 break;
 }
 }
 }
 public static boolean T(int m, int n, int i)
 {
 if (i % m == 0 || i % n == 0)
 {
 return true;
 }
 while (i >= m)
 {
 if (i % m == 0)
 return true;
 i -= n;
 }
 return false;
 }
 public static void main(String[] args) {
 // TODO Auto-generated method stub
 int m, n; // 糖果数
 Scanner sc = new Scanner(System.in);
 m = sc.nextInt();
 n = sc.nextInt();
 check(m, n);
 }
 }

这里 最大不能组合数,应该 < 两数之积 又 大于或等于最小的那个数,且从两数之积向下拉,最先遇到的不能得到的 返回 False的即为最大不能组合数

posted @ 2020-02-19 17:39  小喵钓鱼  阅读(341)  评论(1编辑  收藏  举报