A.Chuanpai
题目:
Chuanpai(川牌) is a kind of traditional playing cards in Sichuan. Each card is marked with two integers
x and y where 1 ≤ x ≤ y ≤ 6.
Some samples of Chuanpai.
The first one is marked with 3 and 4, while the second one is marked with 2 and 5.
Given an integer k, please count the number of different types of cards satisfying x + y = k.
We say two cards with integers x1, y1 and x2, y2 are of different types if x1 6= y1 or x2 6= y2.
Input
There are multiple test cases. The first line of the input contains an integer T (1 ≤ T ≤ 100) indicating
the number of test cases. For each test case:
The first and only line contains an integer k (1 ≤ k ≤ 100).
Output
For each test case output one line containing one integer, indicating the number of types of cards satisfying
x + y = k.
题意:
川牌上有两个数,他们属于是1到6的。给出一个数k,求满足条件1 ≤ x ≤ y ≤ 6且x + y = k 的牌有多少种。
思路:
这道题先判断一下k,k大于12的话就没有满足条件的x,y了。然后在一个一个判断就行,满足条件计数器就加一。
代码:
点击查看代码
import java.util.Scanner;
public class MainA {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int count;
for(int i = 0;i < n;i++) {
int k = scanner.nextInt();
count = 0;
if(k>12) {
System.out.println(count);
continue;
}
for(int x = 1;x <= 6;x++) {
for(int y = x;y <= 6;y++) {
if(x+y==k) {
count++;
}
}
}
System.out.println(count);
}
}
}
K.K-skip Permutation
题目:
For a permutation P=p1,p2,⋯,pn of n, let f(P,k) be the number of i satisfying 1≤i<n and pi+k=pi+1.
Given two integers n and k, your task is to find a permutation P of n such that f(P,k) is maximized.
Recall that in a permutation of n, each integer from 1 to n (both inclusive) appears exactly once.
题意:
就是给一个范围n和等差d,这个范围内的每个数都要出现一次,求其中最长的那个,如果有多个,找到其中一个就行。
思路:
这个题就是求一个最长的等差数列。如果k是奇数的话,我们每次加上k并取模,这样的话n个数就更好能够取完且不重复。但是如果k是偶数,就取不到n个了,只能取n/k个了。但是最后一个测试只有用C++写才能过,其他语言我都过不了。
代码:
点击查看java代码
import java.util.Scanner;
public class MainK_1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int k = scanner.nextInt();
for(int i = 1;i <= k;i++) {
for(int j = i;j <= n;j += k) {
System.out.println(j+" ");
}
}
}
}
点击C++查看代码
#include <iostream>
using namespace std;
int main() {
int n,k;
cin >> n >> k;
for(int i = 1; i <= k;i++){
for(int j = i;j <= n;j += k ){
cout << j << ' ';
}
}
return 0;
}