2020-10-18

7-1 判断素数 (10分)
 

本题的目标很简单,就是判断一个给定的正整数是否素数。

输入格式:

输入在第一行给出一个正整数N 10),随后N行,每行给出一个小于231的需要判断的正整数。

输出格式:

对每个需要判断的正整数,如果它是素数,则在一行中输出Yes,否则输出No

输入样例:

2
11
111
 

输出样例:

Yes
No
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cmath>
 4 #include <string>
 5 #include <cstring>
 6 #include <algorithm>
 7 #include <iomanip>
 8 #include <cstdlib>
 9 #include <cctype>
10 #define ll long long
11 #define ull unsigned long long
12 #define PI 3.14159265358979323846
13 using namespace std;
14 const int maxn = 1e6+7;
15 
16 bool isprime(ull n) {
17     if(n<2){    
18         return false;
19     }
20     if(n==2){
21         return true;
22     }
23     ll x=(ll)sqrt(n);
24     for(ll i=2;i<x;i++){
25         if(n%i==0){
26             return false;
27         }
28     }
29     return true;
30 }
31 int main() {
32     int t;
33     cin>>t;
34     while(t--) {
35         ull n;
36         cin>>n;
37         if(isprime(n)){
38             cout<<"Yes"<<endl;
39         }
40         else{
41             cout<<"No"<<endl;
42         }
43     }
44     return 0;
45 }
7-5 6翻了
 

“666”是一种网络用语,大概是表示某人很厉害、我们很佩服的意思。最近又衍生出另一个数字“9”,意思是“6翻了”,实在太厉害的意思。如果你以为这就是厉害的最高境界,那就错啦 —— 目前的最高境界是数字“27”,因为这是 3 个 “9”!

本题就请你编写程序,将那些过时的、只会用一连串“6666……6”表达仰慕的句子,翻译成最新的高级表达。

输入格式:

输入在一行中给出一句话,即一个非空字符串,由不超过 1000 个英文字母、数字和空格组成,以回车结束。

输出格式:

从左到右扫描输入的句子:如果句子中有超过 3 个连续的 6,则将这串连续的 6 替换成 9;但如果有超过 9 个连续的 6,则将这串连续的 6 替换成 27。其他内容不受影响,原样输出。

输入样例:

it is so 666 really 6666 what else can I say 6666666666

输出样例:

it is so 666 really 9 what else can I say 27
/*
https://blog.csdn.net/weixin_43889841/article/details/104025509
*/

#include <iostream> #include <bits/stdc++.h> using namespace std; int main() { string str=""; getline(cin,str); int i=0; int cnt=0; while(i<str.length()) { if(str[i]=='6') { cnt++; } else { cnt=0; } if(str[i+1]!='6') { if(cnt>9) { str.replace(i-cnt+1,cnt,"27"); i=0; } else if(cnt>3&&cnt<=9) { str.replace(i-cnt+1,cnt,"9"); i=0;//i=0很关键,当转化一串6后,下标可能已经超了,就不再执行。 } } i++; } cout<<str; return 0; }
原文地址:https://www.cnblogs.com/-gcq/p/13837419.html