文章目录
- 1.静静的推荐
- 2.打印沙漏(找规律)
- L1-006 连续因子(数学⭐+暴力)
- L1-009 N个数求和(code⭐⭐⭐gcd() + lcm())
- L1-020 帅到没朋友(map的使用)
- L1-023 输出GPLT
- L1-025 正整数A+B(字符串读入+stoi的使用)
- L1-027 出租(数组标记/思维⭐)
- L1-030 一帮一(结构体)
- L1-032 Left-pad(读入的控制)(20points)
- L1-033 出生年(输出格式控制)
- L1-034 点赞(简单题)20'
- L1-035 情人节(统计个数)15’
- L1-039 古风排版(坑点/输出格式控制)
- L1-043 阅览室(模拟/巨坑❗)
- L1-044 稳赢(map)
- L1-046 整除光棍(思维/数学模拟⭐⭐⭐)
- L1-048 矩阵A乘以B(矩阵相乘/注意局部变量要初始化)
- L1-049 天梯赛座位分配(for循环的巧妙使用⭐⭐⭐)
- L1-050 倒数第N个字符串(26进制⭐)
- L1-054 福到了(for循环)
- L1-056 猜数字
- L1-058 6翻了(while+for循环的妙用)
- L1-059 敲笨钟(字符串)![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/b3b3d5648fc4f5363ed693214c81f715.png)
- L1-062 幸运彩票(字符转数字)
- L1-064 估值一亿的AI核心代码(待做)
- L1-069 胎压监测
- L1-070 吃火锅(字符串)
- L1-071 前世档案(JAVA进制转换)
- L1-072 刮刮彩票(模拟)
- L1-077 大笨钟的心情
- L1-078 吉老师的回归
- L1-079 天梯赛的善良(注意局部变量要初始化)
- L1-080 乘法口诀数列(思维✔)
1.静静的推荐
想的太复杂了,直接用了两个优先队列模拟过程,结果超时了,实际上很简单:
当一个人分数大于等于175并且参加过PAT并达到要求的线,是肯定可以推荐的,不需要考虑单调的要求,所以只要看其他人最多可以推荐几个,对于其他人的分数直接记住出现的次数,只要这个分数出现的次数不大于k就是可以的(可以分在不同的批次)
#include<bits/stdc++.h>
#include<string.h>
using namespace std;
int num[300];
int main(){
int n,k,s;
cin >> n >> k >> s;
int ans = 0;
for(int i = 1; i <= n; i++){
int x,y;
cin >> x >> y;
if(x >= 175){
if(y >= s){
ans++;
}
else if(num[x] < k){
num[x]++;
ans++;
}
}
}
cout << ans;
}
2.打印沙漏(找规律)
思路:找规律
首先我们看上部分发现是一个公差为2的等差数列,我们可以求出上部分的个数和为nn ,下半部分比上部分少一个,所以总共有2nn-1个,所以求出row为2rowrow-1=n,接下来画图看规律就🆗了。
#include<bits/stdc++.h>
using namespace std;
int n;
int main(){
char ch;
cin >> n >> ch;
int row = sqrt((n + 1)/2);
int rest = n - 2 *row * row + 1;
int i = 0, j = 0;
for(i = 1; i <= row; i++){
for(j = 1; j < i; j++){
cout << " ";
}
for(j = i; j <= 2 * row - i; j++){
cout << ch;
}
if(j == 2 * row - i + 1){
cout << endl;
}
}
for(i = row - 1; i >= 1; i --){
for(j = 1; j < i; j++){
cout << " ";
}
for(j = i; j <= 2 * row - i; j++){
cout << ch;
}
if(j == 2 * row - i + 1){
cout << endl;
}
}
cout << rest;
}
L1-006 连续因子(数学⭐+暴力)
思路: 首先最简单的肯定是为素数的情况,如果是素数的话,长度肯定是1,直接输出这个数本身即可 其他
情况我们从每个数作为开头,从2开头,看可以的最长长度,并进行更新(我们需要保证这几个数乘积的整数倍为n,这样的话那个倍数就是不连续的另外的数)
#include<bits/stdc++.h>
#include<math.h>
using namespace std;
int is_prime(int n)
{
int i;
for(i = 2;i <= n/i;i++ )
{
if(n%i==0)
{
return 0;
}
}
return 1;
}
int main()
{
int i,j,n,a = 0,b = 0;
scanf("%d",&n);
if(is_prime(n)==1)
{
printf("1\n%d\n",n);
return 0;
}else{
int i, j, num, ct, start = 0, maxn = 0;
for(i = 2; i < sqrt(n); i++){
num = 1;
ct = 0;
for(int j = i; n % (num*j) == 0;j++){
ct++;
num *= j;
}
if(ct > maxn){
start = i;
maxn = ct;
}
}
cout << maxn << endl;
for(i = start; i < start + maxn - 1; i++){
cout << i << "*";
}
cout << i;
}
}
L1-009 N个数求和(code⭐⭐⭐gcd() + lcm())
思路还是很简单的,就是同分相加然后约分,但是坑点比较多,我们需要注意
(1)首先是数据类型,需要long long
(2)如果分母为负,分子为正,我们需要将负号放在最前面
(3)我们需要判断的是分子的绝对值小于分母就不会有整数部分
#include<bits/stdc++.h>
using namespace std;
#define int long long
int sum = 0;
int gcd(int x,int y) {
if (y == 0) {
return x;
}
return gcd(y,x%y);
}
int lcm(int x,int y) {
return x*y/gcd(x,y);
}
signed main() {
int n;
cin >> n;
int sumx = 0, sumy = 0;
for(int i = 1; i <= n; i++) {
int x,y;
scanf("%lld/%lld",&x,&y);
if(i == 1) {
sumx = x,sumy = y;
} else {
int comy = lcm(sumy,y);
sumx = sumx*(comy/sumy) + x*(comy/y);
sumy = comy;
}
}
// cout <<gcd(-1, -2);
int p1 = 0;
int d = gcd(sumx,sumy);
sumx /= d; sumy /= d;
if(sumx % sumy == 0){
cout << sumx/sumy << endl;
}else if(abs(sumx) < abs(sumy)){
if(sumx > 0 &&sumy < 0){
cout << "-" << sumx << "/" << -1*sumy << endl;
} else {
cout << sumx << "/" << sumy;
}
}else{
if(sumx > 0 &&sumy < 0){
cout << sumx/sumy << " -" << sumx % sumy <<"/" << -1*sumy << endl;
}else
cout << sumx/sumy << " " << sumx % sumy <<"/" << sumy << endl;
}
}
L1-020 帅到没朋友(map的使用)
只要在多人的朋友圈中出现即可标记,此人是有朋友的(需要注意的是朋友圈的人数必须>1,既不能是自己一个人,注意审题,为了保证查询时只再第一次查询时输出没朋友的,我们第一次查询时,把没朋友的标记为1,下次在查询相同的人就不会输出)
#include<bits/stdc++.h>
using namespace std;
map<string,int>mp;
int idx;
string ans[110000];
int main(){
int n;
cin >> n;
for(int i = 1; i <= n; i++){
int k;
cin >> k;
for(int j = 1; j <= k; j++){
string id;
cin >> id;
if(k > 1){ //注意此条件
mp[id] = 1;
}
}
}
int m;
cin >> m;
for(int i = 1; i <= m; i++){
string id;
cin >> id;
if(mp.count(id) == 0){
ans[++idx] = id;
mp[id] = 1;
}
}
if(idx == 0){
cout << "No one is handsome";
}else{
for(int i = 1;i <= idx; i++){
if(i != 1){
cout << " ";
}
cout << ans[i];
}
}
}
L1-023 输出GPLT
注意不区分大小写
#include<bits/stdc++.h>
using namespace std;
int ct[10];
char ch[5] = {' ','G','P','L','T'};
//GPLT
int main(){
string s;
cin >> s;
for(int i = 0; i < s.length(); i++){
if(s[i] == 'G'|| s[i] == 'g'){
ct[1]++;
}else if(s[i] == 'P' || s[i] == 'p'){
ct[2]++;
}else if(s[i] == 'L' || s[i] == 'l'){
ct[3]++;
}else if(s[i] == 'T'|| s[i] == 't'){
ct[4]++;
}
}
while(1){
bool ok = false;
for(int i = 1; i <= 4; i++){
if(ct[i] > 0){
ok = true;
}
}
if(!ok){
break;
}
for(int i = 1; i <= 4; i++){
if(ct[i] > 0){
cout << ch[i];
ct[i]--;
}
}
}
}
L1-025 正整数A+B(字符串读入+stoi的使用)
判断是否满足条件即可 需要注意A,B的区间范围(仔细读题在开始做)
#include<bits/stdc++.h>
using namespace std;
bool judge(string x) {
if(x[0] == '0'){
return false;
}
for(int i = 0; i < x.length(); i++) {
if(x[i] < '0' || x[i] > '9') {
return false;
}
}
int num = stoi(x);
if(num < 1 || num > 1000){
return false;
}
return true;
}
int main() {
string x1,x2;
cin >> x1;
getchar();
getline(cin,x2);
if(judge(x1) && judge(x2)) {
int x = stoi(x1);
int y = stoi(x2);
cout << x << " + " << y << " = " << x + y ;
}else if(judge(x1) && !judge(x2)){
int x = stoi(x1);
cout << x << " + " << "?" << " = " << "?";
}else if(!judge(x1) && judge(x2)){
int y = stoi(x2);
cout << "?" << " + " << y << " = " << "?";
}else{
cout << "?" << " + " << "?" << " = " << "?";
}
}
L1-027 出租(数组标记/思维⭐)
首先我们可以标记下来手机号中出现的数字,那么这个可以推断中arr数组,
注意读题,arr数组是递减的,所以此时我们可以得到arr数组啦,
然后通过每个数字在arr数组中的位置就可以得到index数组啦
需要注意的是:index不知道为什么提交的时候编译有问题......
#include<bits/stdc++.h>
using namespace std;
int vis[10];
int arr[20];
int in[20];
map<int,int> mp;
int idx1;
int idx2;
int main() {
string s;
cin >> s;
for(int i = 0; i < s.length(); i++) {
vis[s[i] - '0']++;
}
for(int i = 9; i >= 0; i--) {
if(vis[i]) {
arr[idx1] = i;
idx1++;
}
}
for(int i = 0; i < idx1; i++) {
mp[arr[i]] = i;
}
idx2 = 0;
for(int i = 0; i < s.length(); i++) {
in[idx2++] = mp[s[i] - '0'];
}
cout << "int[] arr = new int[]{";
for(int i = 0; i < idx1; i++) {
if(i != 0) {
cout << ',';
}
cout << arr[i];
}
cout << "};";
cout << endl;
cout << "int[] index = new int[]{";
for(int i = 0; i < s.length(); i++) {
if(i != 0) {
cout << ',';
}
cout << in[i];
}
cout << "};";
}
L1-030 一帮一(结构体)
只需要前面部分去找后面的第一个与他异性且未被他人找过的,找到后要标记上,防止被他人再次寻找,并结束循环,去找下一对。
#include<bits/stdc++.h>
using namespace std;
struct node{
int sex;
string name;
}stu[60];
bool vis[60];
int main(){
int n;
cin >> n;
for(int i = 1; i <= n; i++){
cin >> stu[i].sex >> stu[i].name;
}
for(int i = 1; i <= n/2; i++){
for(int j = n; j > n/2; j--){
if(!vis[j] && stu[i].sex != stu[j].sex){
vis[j] = true;
cout << stu[i].name << " " << stu[j].name << endl;
break;
}
}
}
}
L1-032 Left-pad(读入的控制)(20points)
用getchar()读入字符串之前需要吸收上面的换行
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;char ch;
cin >> n >> ch;
string str;
getchar();
getline(cin,str);
// cout << str;
if(n < str.length()){
for(int i = str.length() - n; i < str.length(); i++){
cout << str[i];
}
}else{
for(int i = 1; i <= n - str.length(); i++){
cout << ch;
}
cout << str;
}
}
L1-033 出生年(输出格式控制)
注意仔细读题,1年被认为是0001,是有两种不同的数字的,需要注意<1000的情况并且原数位中没有0的情况,这种情况下,我们需要+1;
#include<bits/stdc++.h>
using namespace std;
int x,num;
set<int> st;
int check(int n){
int nn = n;
st.clear();
int t;bool ok = false;
while(n != 0){
t = n % 10;
if(t == 0){
ok = true;
}
st.insert(t);
n = n / 10;
}
if(nn < 1000 && !ok){
return st.size() + 1;
}
return st.size();
}
int main() {
cin >> x >> num;
for(int i = x; ; i++) {
if(check(i) == num) {
printf("%d %04d",i-x,i);
break;
}
}
}
L1-034 点赞(简单题)20’
#include<bits/stdc++.h>
using namespace std;
int ct[1100];
int n;
int main(){
cin >> n;
for(int i = 1; i <= n; i++){
int k;
cin >> k;
for(int j = 1; j <= k; j++){
int x;
cin >> x;
ct[x]++;
}
}
int maxn = 0;
for(int i = 1; i <= 1000; i++){
maxn = max(maxn,ct[i]);
}
int id = 0;
for(int i = 1; i <= 1000; i++){
if(ct[i] == maxn){
id = i;
}
}
cout << id << " " << maxn;
}
L1-035 情人节(统计个数)15’
#include<bits/stdc++.h>
using namespace std;
int main(){
string a,b,str;
int ct = 0;
while(1){
cin >> str;
if(str == "."){
break;
}
++ct;
if(ct == 2){
a = str;
}else if(ct == 14){
b = str;
}
}
if(ct < 2){
cout << "Momo... No one is for you ...";
}else if(ct < 14){
cout << a << " is the only one for you...";
}else{
cout << a << " and "<< b << " are inviting you to dinner...";
}
}
L1-039 古风排版(坑点/输出格式控制)
当最后不足的时候,我们需要输出空格,…要不过不了,找了半年
#include<bits/stdc++.h>
using namespace std;
char ch[100][100];
int main() {
int n;
cin >> n;
getchar();
string str;
getline(cin,str);
int l = str.length();
int col;
if(l % n == 0) {
col = l/n;
} else {
col = l/n + 1;
}
int ct = 0;
for(int j = col; j >= 1; j--) {
for(int i = 1; i <= n; i++) {
ch[i][j] = str[ct++];
}
}
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= col; j++) {
if(!ch[i][j]){
cout << ' ';
}else{
cout << ch[i][j];
}
}
if(i != n) {
cout << endl;
}
}
}
L1-043 阅览室(模拟/巨坑❗)
容易入坑的点比较多,题意需要仔细理解
(1)多次借一次还(我们需要使用最后一次借的时间)个人理解应该是:在任何时间内,同一本书只能被一个人借阅,由于有的记录不全,所以有可能还了,我们不知道。
(2)一次借,多次还,以最早的时间为准(这个很好理解啦)
(3)标记借没借不能用开始时间为0,因为可能在0点0分借出,所以需要初始化为-1,并且还书的时候,在标记为-1,表示此书现在空闲。
#include<bits/stdc++.h>
using namespace std;
int n;
int tol;
struct node {
int st;
int end;
} a[1100];
int idx;
int sum,ave,ct;
void init() {
for(int i = 0; i <= 1000; i++) {
a[i].st = -1;
a[i].end = -1;
}
ct = 0;
sum = 0;
}
void print() {
if(ct == 0) {
cout << 0 << " " << 0 << endl;
} else {
cout << ct << " " << (int)(sum*1.00000/ct + 0.5) << endl;
}
init();
}
int main() {
cin >> n;
init();
while(1) {
if(tol >= n) {
break;
}
int id,t;
char op;
int h,m ;
cin >> id >> op;
scanf("%d:%d",&h,&m);
if(id == 0) {
++tol;
print();
} else {
if(op == 'S') {
a[id].st = h * 60 + m;
} else {
if(a[id].st != -1) {
ct++;
a[id].end = h * 60 + m;
sum = sum + a[id].end - a[id].st;
a[id].st = -1;
}
}
}
}
}
L1-044 稳赢(map)
#include<bits/stdc++.h>
using namespace std;
map<string,string> mp;
int main(){
mp["ChuiZi"] = "Bu";
mp["JianDao"] = "ChuiZi";
mp["Bu"] = "JianDao";
int res;
cin >> res;
int ct = 0;
while(1){
string str;
cin >> str;
if(str == "End"){
break;
}
++ct;
if(ct == res + 1){ //判断是否过去了res轮
ct = 0;
cout << str << endl;
}else{
cout << mp[str] << endl;
}
}
}
L1-046 整除光棍(思维/数学模拟⭐⭐⭐)
首先很明显一位一位累加肯定会超时,而且数值也会很大,超出数据范围,在网上看一种很妙的解题方法,模拟此过程,一位一位输出,并记录长度。
大概就是不断取余,补1,直到余数为0停止
#include<bits/stdc++.h>
using namespace std;
int x;
int main(){
cin >> x;
int ct = 1,num = 1;
while(num < x){
num = num * 10 + 1;
ct++;
}
while(1){
cout << num/x;
if(num % x == 0){
break;
}else{
num = num % x * 10 + 1;
ct++;
}
}
cout << " " << ct;
}
L1-048 矩阵A乘以B(矩阵相乘/注意局部变量要初始化)
#include<bits/stdc++.h>
using namespace std;
int main() {
int ra,ca,rb,cb;
cin >> ra >> ca;
int a[ra + 10][ca + 10];
for(int i = 1; i <= ra; i++) {
for(int j = 1; j <= ca; j ++) {
cin >> a[i][j];
}
}
cin >> rb >> cb;
int b[rb + 10][cb + 10];
int c[ra + 10][cb + 10];
for(int i = 1; i <= rb; i++) {
for(int j = 1; j <= cb; j++) {
cin >> b[i][j];
}
}
if(ca != rb) {
cout << "Error: " << ca <<" != "<< rb;
} else {
for(int row = 1; row <= ra; row ++) {
for(int col = 1; col <= cb; col++) {
c[row][col] = 0;
for(int i = 1,j = 1; i <= ca && j <= rb; i++,j++) {
c[row][col] += a[row][i] * b[j][col];
}
}
}
cout << ra << " " << cb << endl;
for(int i = 1; i <= ra; i++) {
for(int j = 1; j <= cb; j++) {
if(j != 1){
cout << " ";
}
cout << c[i][j];
}
if(i != ra) {
cout << endl;
}
}
}
}
L1-049 天梯赛座位分配(for循环的巧妙使用⭐⭐⭐)
第一次看到以为需要模拟,没想到可以使用for循环巧妙控制,太妙了
借鉴博客
主要是因为编号在不同学校间来回编号,所以可以将学校编号作为最内层循环
#include<bits/stdc++.h>
using namespace std;
int n,ct;
int num[110];
int id[110][12][12];//学校编号第x队第x个人
int maxn = 0, pre = -1;
int main(){
cin >> n;
for(int i = 1; i <= n; i++){
cin >> num[i];
maxn = max(maxn,num[i]);
}
for(int j = 1; j <= maxn; j++){ //队伍数量 (先按照最大队伍量计算,再在具体的循环中看是否存在这么多)
for(int k = 1; k <= 10; k++){ //每位队员
for(int i = 1; i <= n; i++){//学校编号
if(j <= num[i]){
if(pre == i){ //标记上次编号的学校是否是自己学校
ct += 2;
} else{
ct += 1;
}
id[i][j][k] = ct;
pre = i;
}
}
}
}
for(int i = 1; i <= n; i++){
cout << "#" << i << endl;
for(int j = 1; j <= num[i]; j ++){
for(int k = 1; k <= 10; k ++){
if(k != 1){
cout << " ";
}
cout << id[i][j][k];
}
cout << endl;
}
}
}
L1-050 倒数第N个字符串(26进制⭐)
将aaa看作000,那么z就是25,倒数第n个就是正着第pow(26,l)-n个,我们只需要求这个数并将其转换为对应的字母即可,需要注意当不足位的时候,其实前面是0,即代表的是a,
#include<bits/stdc++.h>
using namespace std;
int main(){
int l,n;
cin >> l >> n;
int k = pow(26,l) - n; //正着第几个
int ct = k;
stack<int> sa;
//把aaa看作000
for(int i = 1; i <= l; i++){
sa.push(ct%26);
ct /= 26;
}
while(sa.size()){
cout << char(sa.top() + 'a');
sa.pop();
}
}
L1-054 福到了(for循环)
#include<bits/stdc++.h>
using namespace std;
char a[110][110];
char c[110][110];
int main() {
char ch;
int n;
cin >> ch >> n;
getchar();
string str;
for(int i = 1; i <= n; i++) {
getline(cin,str);
// cout << str << endl;
for(int j = 1; j <= n; j++) {
a[i][j] = str[j - 1];
}
}
for(int i = 1,i1 = n; i <= n, i1 >= 1; i++,i1--) {
for(int j = 1,j1 = n; j <= n,j1 >= 1; j++,j1--) {
if(a[i1][j1] != ' ') {
c[i][j] = ch;
}else{
c[i][j] = ' ';
}
}
}
bool ok = true;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) {
if(a[i][j] != ' '){
if(c[i][j] == ' '){
ok = false;
}
}
}
}
if(ok){
cout << "bu yong dao le" <<endl;
}
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
cout << c[i][j];
}
if(i != n){
cout <<endl;
}
}
}
L1-056 猜数字
#include<bits/stdc++.h>
using namespace std;
const int N = 1e4 + 100;
int score[N];
string name[N];
int main(){
int n;
cin >> n;
int sum = 0;
for(int i = 1; i <= n; i++){
cin >> name[i] >> score[i];
sum += score[i];
}
int aver = sum/n/2;
int pos = 0;
int minn = 1e9;
cout << aver << " ";
for(int i = 1; i <= n; i++){
if(abs(score[i] - aver) < minn){
minn = abs(score[i] - aver);
pos = i;
}
}
cout << name[pos];
}
L1-058 6翻了(while+for循环的妙用)
用for循环枚举每一位,while循环统计连续的6的个数
#include<bits/stdc++.h>
using namespace std;
int main(){
int ct;
string str;
getline(cin,str);
for(int i = 0; i < str.length(); i++){
ct = 0;
while(str[i] == '6'){
++ct;
i++;
}
if(ct <= 3){
for(int j = 1; j <= ct; j++){
cout << '6';
}
}else if(ct > 3 && ct <= 9){
cout << '9';
}else if(ct > 9){
cout << "27";
}
cout << str[i];
}
}
L1-059 敲笨钟(字符串)![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/b3b3d5648fc4f5363ed693214c81f715.png)
坑点:上半句有可能字符数达不到3
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
public class Main {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
static int n = 0;
public static void main(String[] args) throws Exception{
n = Integer.parseInt(br.readLine());
for(int i = 1; i <= n; i++) {
String[] sen = br.readLine().split(" ");
if(check(sen)) {
for(int j = 0; j < sen.length - 3; j++) {
System.out.print(sen[j] + " ");
}
System.out.println("qiao ben zhong.");
}else {
System.out.println("Skipped");
}
}
}
private static boolean check(String[] sen) {
String last = sen[sen.length - 1];
for(int i = 0; i < sen.length; i++) {
String s = sen[i];
if(s.charAt(s.length() - 1) == ',') {
if(s.length() < 4) {
return false;
}
if(s.contains("ong")) {
if(last.contains("ong")) {
return true;
}else {
return false;
}
}else {
return false;
}
}
}
return false;
}
}
/*
1
a, go to ong.
*/
不知道为什么最后一个点不过
L1-062 幸运彩票(字符转数字)
#include<bits/stdc++.h>
using namespace std;
int n;
string str;
bool check(string s){
int x,y;
for(int i = 0; i < 3; i++){
x += (s[i] - '0');
}
for(int i = 3; i < 6; i++){
y += (s[i] - '0');
}
// cout << x << " " << y;
if(x == y){
return true;
}else{
return false;
}
}
int main(){
cin >> n;
for(int i = 1; i <= n; i++){
cin >> str;
if(check(str)){
cout << "You are lucky!" << endl;
}else{
cout << "Wish you good luck." << endl;
}
}
}
L1-064 估值一亿的AI核心代码(待做)
L1-069 胎压监测
#include<bits/stdc++.h>
using namespace std;
int ct;
int num[5];
int main(){
int maxn = 0,minn;
for(int i = 1; i <= 4; i++){
cin >> num[i];
maxn = max(maxn,num[i]);
}
int d;
cin >> minn >> d;
int pos = 0;
for(int i = 1; i <= 4; i++){
if(num[i] < minn || maxn - num[i] > d){
++ct;
pos = i;
}
}
if(ct == 0){
cout << "Normal";
}else if(ct == 1){
cout <<"Warning: please check #"<<pos <<"!";
}else{
cout <<"Warning: please check all the tires!";
}
}
L1-070 吃火锅(字符串)
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
public class Main {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
static int n = 0;
public static void main(String[] args) throws Exception{
int ct = 0;
int pos = 0;
int tol = 0;
while(true) {
String str = br.readLine();
if(str.equals(".")) {
break;
}
++ct;
if(str.contains("chi1 huo3 guo1")) {
++tol;
if(pos == 0) pos = ct;
}
}
System.out.println(ct);
if(pos != 0) {
System.out.println(pos +" " + tol);
}else {
System.out.println("-_-#");
}
}
}
L1-071 前世档案(JAVA进制转换)
很明显像递归搜索树,就是从全0到全1,所以与2进制很像,我们可以采用进制转换的形式,先表示为二进制,再将其转换为对应的十进制。
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
public class Main {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
static int n = 0;
public static void main(String[] args) throws Exception{
int m = 0;
String nm[] = br.readLine().split(" ");
n = Integer.parseInt(nm[0]);
m = Integer.parseInt(nm[1]);
int x = (int) Math.pow(2, n);
for(int i = 1; i <= m; i++) {
String s = "";
String str = br.readLine();
for(int j = 0; j < str.length(); j++) {
if(str.charAt(j) == 'y') {
s += "1";
}else {
s += "0";
}
}
int cur = Integer.parseInt(s,2);
System.out.println(x - cur);
}
}
}
L1-072 刮刮彩票(模拟)
真的会无语,select竟然是关键词,不能用
#include<bits/stdc++.h>
using namespace std;
int a[10][10];
int sel[8];
bool vis[10];
int arr[50] = {0,0,0,0,0,0,10000,36,720,360,80,252,108,72,54,180,
72,180,119,36,306,1080,144,1800,3600};
int main() {
for(int i = 1; i <= 3; i++) {
for(int j = 1; j <= 3; j++) {
cin >> a[i][j];
vis[a[i][j]] = true;
}
}
for(int i = 1; i <= 3; i++) {
for(int j = 1; j <= 3; j++) {
if(a[i][j] == 0){
for(int k = 1; k <= 9; k++){
if(!vis[k]){
a[i][j] = k;
break;
}
}
}
}
}
for(int i = 1; i <= 3; i++) {
for(int j = 1; j <= 3; j++) {
sel[i] += a[i][j];
}
}
sel[4] = a[1][1] + a[2][1] + a[3][1];
sel[5] = a[1][2] + a[2][2] + a[3][2];
sel[6] = a[1][3] + a[2][3] + a[3][3];
sel[7] = a[1][1] + a[2][2] + a[3][3];
sel[8] = a[1][3] + a[2][2] + a[3][1];
for(int i = 1; i <= 3; i++) {
int x,y;
cin >> x >> y;
cout << a[x][y] << endl;
}
int idx;
cin >> idx;
cout << arr[sel[idx]];
}
L1-077 大笨钟的心情
#include<bits/stdc++.h>
using namespace std;
int mod[30];
int main(){
for(int i = 0; i < 24; i++){
cin >> mod[i];
}
while(1){
int x;
cin >> x;
if(x < 0 || x > 23){
break;
}
if(mod[x] > 50){
cout << mod[x] << " Yes" << endl;
}else{
cout << mod[x] << " No" << endl;
}
}
}
L1-078 吉老师的回归
#include<bits/stdc++.h>
using namespace std;
string s[50];
int n,m;
int main() {
cin >> n >> m;
getchar();
string str;
int idx;
int pos;
for(int i = 1; i <= n; i++) {
getline(cin,str);
s[i] = str;
if(str.find("qiandao") != str.npos || str.find("easy") != str.npos) {
continue;
} else {
++idx;
if(idx == m) {
pos = i;
}
}
}
if(idx <= m){
cout << "Wo AK le";
}else{
cout << s[pos + 1];
}
}
L1-079 天梯赛的善良(注意局部变量要初始化)
#include<bits/stdc++.h>
using namespace std;
const int N = 2e4 + 10;
int num[N];
int main() {
int n;
cin >> n;
for(int i = 1; i <= n; i++) {
cin >> num[i];
}
int maxn = num[1];
int minn = num[1];
for(int i = 1; i <= n; i++) {
maxn = max(maxn,num[i]);
minn = min(minn,num[i]);
}
int mx = 0,mi = 0; //记得初始化
for(int i = 1; i <= n; i++) {
if(num[i] == maxn) {
mx ++;
}
if(num[i] == minn) {
mi ++;
}
}
cout << minn << " " << mi << endl;
cout << maxn << " " << mx;
}
L1-080 乘法口诀数列(思维✔)
注意:两位数相乘最大是81,所以当乘积>=10的时候分开存两位就可以,想到这一点实现起来就很简单了
#include<bits/stdc++.h>
using namespace std;
int num[1100];
int n,x,y;
int a1,a2;
int main(){
cin >> a1 >> a2 >> n;
num[1] = a1; num[2] = a2;
int mul = 0;
int ct = 2;
for(int i = 1; i <= n; i++){
mul = num[i] * num[i + 1];
if(mul < 10){
num[++ct] = mul;
}else{
num[++ct] = mul / 10;
num[++ct] = mul % 10;
}
if(ct > n){
break;
}
}
for(int i = 1; i <= n; i++){
if(i != 1){
cout << " ";
}
cout << num[i];
}
}