L1-001 Hello World
代码:
#include
using namespace std;
int main() {
cout << "Hello World!" << endl;
return 0;
}
L1-002 打印沙漏
#include
using namespace std;
int main() {
int n, t, sum = -1, row = 1;
char c;
cin >> n >> c;
for(int i = 1; ; i+=2) {
if(n < sum) {
row = i - 4;
sum -= (i - 2) * 2;
break;
}
sum += i * 2;
}
for(int i = 0; i <= row/2; i++) {
for(int j = 0; j < i; j++)
cout << " ";
for(int j = 0; j < row-i*2; j++)
cout << c;
cout << endl;
}
for(int i = 0; i < row/2; i++) {
for(int j = 0; j < row/2-i-1; j++)
cout << " ";
for(int j = 0; j < 3+2*i; j++)
cout << c;
cout << endl;
}
cout << n-sum << endl;
return 0;
}
L1-003 个位数统计
#include
using namespace std;
int f[10];
int main() {
string s;
cin >> s;
for(int i = 0; i < s.size(); i++)
f[s[i]-'0']++;
for(int i = 0; i < 10; i++)
if(f[i]) cout << i << ":" << f[i] << endl;
return 0;
}
L1-004 计算摄氏温度
#include
using namespace std;
int main() {
double f, c;
cin >> f;
c = 5 * (f - 32) * 1.0 / 9;
printf("Celsius = %d\n", (int)c);
return 0;
}
L1-005 考试座位号
/*
*
* UM.
* J@B@1 iO@1
* Y@@@B@BB. 7B@B@B@
* :@B@i,B@B@O ,Z@B@B@B@Br
* @B@q i@B@BS 7@B@@@O5vMB@q
* 8@@B LB@B@i FB@@@BNjYjLE@B@
* ,B@B: 0@@@Z P@B@BM1JJ125JPB@B
* B@BB :@B@B XB@B@Z2LuU52F2u2@B@.
* :@B@ @@@B: v@B@B8uJj51F1525uUB@B7
* @B@O 0@@B. ..::ir7vvYUuU777r::. B@B@OULU2F2F151F11Y@B@S
* B@B, 8B@B :ruXMB@B@B@B@B@B@B@B@B@B@@@B@B@B@B@B@B@5Jj1211F1F1F2FUJO@BB
* U@B@ @B@B@B@B@B@@@B@B@B@MMqPS5JuYL7rq@B@OBB@B@B8Yu211F1515251515YGB@@
* @B@u v@@@@MSur:. LB@MvvjJuU5YU252F1F1F25251F2uX@@@
* @@@. N@BML2U2UUU12F15252525251515Jk@@B
* r@B@ YB@Bju52121252515252F15251F2u5@B@
* PB@B @@@PYUF151F25151F152F2F1F15jF@@B
* @@BS N@@@UJ2F25252F251525151F1F1u5@B@
* @@@7 B@B@5Yj12F152F1F1F25252515jFB@B
* B@Bi M@B@O2Luu52525212F151121UY1@B@7
* O@B@: v@B@BMSuYJJuuUu2u2uujjYJJXB@B@M
* 7B@B@, 1B@@@B@GPF1uujuu21PNMB@B@B@B@@
* qB@B2 i8B@B@B@B@B@@@@@B@B@B@q: @@@B
* MB@B: 7SBB@B@B@B@B@Zu: @B@B
* ZB@B. ,v. @B@L
* LB@B, Y7 @B@Bu 7@B@
* :B@B@@B2: @@B7 @B@Z r@B@B@BP: B@BE
* BB@@@B@B@B@BE r@B@B 7@B@B@B@Ou: iB@B
* :uM@@B@@2. :7::::ivk@B@B@0 :5B@B@B@B@B@B@G. @B@i
* BB@@@B@@ :@B@B@@@B@B@@1 .i5M@B@B@@@5 M@@2
* B@B ,@B1 L0EZZG0F7: .:, uB@MrP@M7
* 2@B@ ,O@B@B@B@B
* @B@1 :@B@@@r :@@@@B@BL:,,
* B@Bi :2ZS; :@B@B@B@r L@B@B@BU
* @B@. @@@B@B@ vB@B@B@B5 @B@i
* B@B 7B@B@B@BM OB@B@B@ ,B@B
* @B@ @B@B@@@i rL7. B@BM
* B@B7.: NB@@M. .@B@.
* .;JEB@@@B@B@B@B@. . @B@u
*@@@B@B@B@B@@@B@18U :B@B@B@BU,
*7@BOui. ,@@B SP@B@B@B@B@Or
* @@@U [email protected]@B@B@i
* r@B@ :B@Bk .k@B@
* B@B@ LB@@k 2i
* B@BM .7jXEGqF7: OB@@L
* .B@BM .B@B@B@B@B@B@. :@B@B:
* .B@B@ @@MYr::ivG@B .M@B@G
* B@@@S ,MB@B@,
* v@@@BF .1B@B@Br
* 2@@B@BL ,FB@@@B8,
* r@B@B@BF, :YBB@B@B@B
* L@B@B@B@P7, .ivXB@B@B@B@B@M@B@
* ,1B@B@B@B@@@BOP2L7i:,. ..,:i7LSNB@@B@B@@@B@B@B@Z5v;.LB@@
* @B@OEB@B@@@B@B@B@B@B@B@B@B@@@B@B@B@B@B@@@B@B@B@B@BM0SJ7i::::i:,u@B@
* B@Bu ::i;7vu2XNGOMB@B@BMB@B@B@B@B@B@@@B1UFuj77ii:::::::iir;r;i.YB@B
* @B@L.:i:i:i::::::::::..Y@B@BMYi:i;SB@B@N:.::i:iirir;r;rii::::ivO@B@
* B@@X::,::::iirir;riri:E@B@1 ,@B@Br:;;r;rii:i::::i7JEB@@@@@B
* @@@B@BBq5v7ii:::::::.2@@@i ..,.. @B@@,,:::::irv2XMB@B@B@B@2@B@:
* .B@BBB@@@B@B@B@BMNP5u7@B@1 .,,:,, :. @B@P50MB@B@B@B@B@@@BS: @@B1
* E@B@ ijGB@B@B@B@B@B@B@Bi .,:,,..@@B@7 B@B@B@B@B@B@BM57. kB@B
* .@B@: .,ivu5Nq@B@u ..,.. SB@B@@@B@PL7i, ,@B@
* @@@8 i@B@: . :B@B@@ B@@2
* i@@@@ 0@B@u B@@B. vB@B
* ,@B@G L@B@BOv:.:iFB@B@M @B@Bi
* vNi S@@B@B@B@B@BM: MB@N
* 758BMqJ,
*
* . YO. vq :G Z:
* SqOMBB@B@Br @@r rBE @B B@@@@@B@ONX8k i::::.OB1.:::.u@O.::::i @B@B@U:@@B@@BPEBu
* B@@NB@k. 5@i uB@E. BM 1U2uUJvirB@@Z r@@B@B@@@B@B@B@B@B@@@B@Bi LB@B@1 BX :@k uLLLvr@BJ:
* iB iBi 7@ .@M8@BGMZZ @@F ,B Pi v@ Bq @i v@ B@
* vuL7r8@S7vJL7N@Z7LLri;72. F7@Bvvv@@ @BX @@@B@B@@@@@B@@@B@B@B 7@ @F Bi @q @B@Bu @B
* N@B@G@@@8@BBOMB@G@BMNXG@, B@ @@ .Bk .:u; i@: Zv 7@ Bk @,;@ ,BY @B B@
* r@ @G 5. ,@v BZ :::,.r@E .::i, @B B@ .@BL 7@ @F B:i@. .@ @M @B
* 7B: ,vO, @@ iB@: @B 7@:MB@B@B@@@B@B@BM @@. B: 2@q 7@ BS @i 0@ B. @O B@
* ,r2EBB@B@B@Bi G@ @BB B@ @B @S : r@ .. 7B @F @7 B7 @ @B @B
* E@B@UOBr @B@Bi L@0PB .BZ .@B@B@B@B@B@B@B@B@B@B@B@, r@ BF @i @G B@B@B B@
* 7@, kB@U ;r @@@. .@Z GBuL@iBBi vB@B@q BP:5@7 @u,. @B
* LBi YB@BrB@ @@ @B:L@Br BM .M@B rB rB@J v@. Pi @XZ8r . B@
* . G@i B@BM. ,B@, @B iB@B N, 7r..q@k ,LB@B8 J@, i@B@B1r Br @@
* MB@B@B ,i B@B@B, B@: @B@B@F .@BB: P@i :OBZ .@U B@B@B:
* .ll rB. :
*
*/
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.*;
public class Main {
public static InputReader in = new InputReader(new BufferedInputStream(System.in));
public static PrintWriter out = new PrintWriter(System.out);
public static int n, t, sj, zw, q;
public static String id;
public static HashMap m1 = new HashMap<>();
public static HashMap m2 = new HashMap<>();
public static void main(String[] args) {
n = in.nextInt();
for (int i = 0; i < n; i++) {
id = in.next();
sj = in.nextInt();
zw = in.nextInt();
m1.put(sj, id);
m2.put(sj, zw);
}
t = in.nextInt();
while (t-- > 0) {
q = in.nextInt();
out.println(m1.get(q) + " " + m2.get(q));
out.flush();
}
out.close();
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public String nextLine() {
String str = null;
try {
str = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public Double nextDouble() {
return Double.parseDouble(next());
}
public BigInteger nextBigInteger() {
return new BigInteger(next());
}
}
}
L1-006 连续因子
解法:用三层循环来遍历并加以剪枝优化就行了,外层循环代表连续因子的长度,中层循环代表连续因子的长度,里层循环则是对连续因子进行累乘。累乘结果如果是n的因子,则说明这是答案。
坑点:测试点5、6超时,其它正确:可能没有特判n为素数的情况。
代码:
#include
using namespace std;
int main() {
int n, a, b, flag = 0;
scanf("%d", &n);
for(int len = 11; len > 0; len--) {//连续因子串长度,优化1:由于n范围的限制,连续因子连乘的话最多只能到12的阶乘
for(int i = 2; i <= sqrt(n); i++) {//连续串起点,优化2:从2开始乘,最大乘到n开方就够了
int sum = 1;
for(int j = i; j < i+len; j++) {
sum *= j;
if(sum > n) break;//优化3:累乘结果大于n了,说明以i为起点的连乘不存在等于n的连续因子,直接break
}
if(n % sum == 0) {
a = i;
b = i+len-1;
flag = 1;
break;
}
}
if(flag) break;
}
if(flag) {
printf("%d\n", b-a+1);
for(int i = a; i <= b; i++) {
if(i == a) printf("%d", i);
else
printf("*%d", i);
}
printf("\n");
} else {
printf("1\n%d\n", n);//因为上面遍历不是从1开始的而是从2开始的,所以素数是找不到连续因子的
}
return 0;
}
L1-007 念数字
#include
using namespace std;
int main() {
string str, s[10] = {"ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"};
cin >> str;
if(str[0] == '-') {
cout << "fu";
for(int i = 1; i < str.size(); i++)
cout << " " << s[str[i]-'0'];
} else {
cout << s[str[0]-'0'];
for(int i = 1; i < str.size(); i++)
cout << " " << s[str[i]-'0'];
}
return 0;
}
L1-008 求整数段和
#include
using namespace std;
int main() {
int a, b, sum = 0, t = 1;
cin >> a >> b;
for(int i = a; i <= b; i++) {
printf("%5d", i);
sum += i;
if(t % 5 == 0 || i == b) printf("\n");
t++;
}
printf("Sum = %d\n", sum);
return 0;
}
L1-009 N个数求和
解法:模拟
坑点:测试点4:计算和的结果为负整数。测试点3:输入的分子和分母为长整型。测试点5:浮点错误,是因为输入的分子为0,如输入一个数0/2,答案应为0。
代码:
#include
using namespace std;
typedef long long ll;
ll n, zs, ansfz = 1, ansfm = 1, fz, fm;
int main() {
cin >> n;
for(ll i = 0; i < n; i++) {
scanf("%lld/%lld", &fz, &fm);
ansfz *= fm;
fz *= ansfm;
ansfz += fz;
ansfm *= fm;
}
ansfz -= ansfm;
if(ansfz == 0) cout << "0" << endl;
else {
if(abs(ansfz) >= ansfm) {
zs = ansfz / ansfm;
ansfz -= zs * ansfm;
if(ansfz == 0) cout << zs << endl;
else
cout << zs << " " << ansfz/__gcd(ansfz, ansfm) << "/" << ansfm/__gcd(ansfz, ansfm) << endl;
} else {
cout << ansfz/__gcd(ansfz, ansfm) << "/" << ansfm/__gcd(ansfz, ansfm) << endl;
}
}
return 0;
}
L1-010 比较大小
#include
using namespace std;
int main() {
int a[3];
cin >> a[0] >> a[1] >> a[2];
sort(a, a+3);
for(int i = 0; i < 3; i++)
printf(i == 0 ? "%d" : "->%d", a[i]);
return 0;
}
L1-011 A-B
解法:用java写的,直接就有替换子串的函数,要用快读才能不超时
java代码:
/*
*
* UM.
* J@B@1 iO@1
* Y@@@B@BB. 7B@B@B@
* :@B@i,B@B@O ,Z@B@B@B@Br
* @B@q i@B@BS 7@B@@@O5vMB@q
* 8@@B LB@B@i FB@@@BNjYjLE@B@
* ,B@B: 0@@@Z P@B@BM1JJ125JPB@B
* B@BB :@B@B XB@B@Z2LuU52F2u2@B@.
* :@B@ @@@B: v@B@B8uJj51F1525uUB@B7
* @B@O 0@@B. ..::ir7vvYUuU777r::. B@B@OULU2F2F151F11Y@B@S
* B@B, 8B@B :ruXMB@B@B@B@B@B@B@B@B@B@@@B@B@B@B@B@B@5Jj1211F1F1F2FUJO@BB
* U@B@ @B@B@B@B@B@@@B@B@B@MMqPS5JuYL7rq@B@OBB@B@B8Yu211F1515251515YGB@@
* @B@u v@@@@MSur:. LB@MvvjJuU5YU252F1F1F25251F2uX@@@
* @@@. N@BML2U2UUU12F15252525251515Jk@@B
* r@B@ YB@Bju52121252515252F15251F2u5@B@
* PB@B @@@PYUF151F25151F152F2F1F15jF@@B
* @@BS N@@@UJ2F25252F251525151F1F1u5@B@
* @@@7 B@B@5Yj12F152F1F1F25252515jFB@B
* B@Bi M@B@O2Luu52525212F151121UY1@B@7
* O@B@: v@B@BMSuYJJuuUu2u2uujjYJJXB@B@M
* 7B@B@, 1B@@@B@GPF1uujuu21PNMB@B@B@B@@
* qB@B2 i8B@B@B@B@B@@@@@B@B@B@q: @@@B
* MB@B: 7SBB@B@B@B@B@Zu: @B@B
* ZB@B. ,v. @B@L
* LB@B, Y7 @B@Bu 7@B@
* :B@B@@B2: @@B7 @B@Z r@B@B@BP: B@BE
* BB@@@B@B@B@BE r@B@B 7@B@B@B@Ou: iB@B
* :uM@@B@@2. :7::::ivk@B@B@0 :5B@B@B@B@B@B@G. @B@i
* BB@@@B@@ :@B@B@@@B@B@@1 .i5M@B@B@@@5 M@@2
* B@B ,@B1 L0EZZG0F7: .:, uB@MrP@M7
* 2@B@ ,O@B@B@B@B
* @B@1 :@B@@@r :@@@@B@BL:,,
* B@Bi :2ZS; :@B@B@B@r L@B@B@BU
* @B@. @@@B@B@ vB@B@B@B5 @B@i
* B@B 7B@B@B@BM OB@B@B@ ,B@B
* @B@ @B@B@@@i rL7. B@BM
* B@B7.: NB@@M. .@B@.
* .;JEB@@@B@B@B@B@. . @B@u
*@@@B@B@B@B@@@B@18U :B@B@B@BU,
*7@BOui. ,@@B SP@B@B@B@B@Or
* @@@U [email protected]@B@B@i
* r@B@ :B@Bk .k@B@
* B@B@ LB@@k 2i
* B@BM .7jXEGqF7: OB@@L
* .B@BM .B@B@B@B@B@B@. :@B@B:
* .B@B@ @@MYr::ivG@B .M@B@G
* B@@@S ,MB@B@,
* v@@@BF .1B@B@Br
* 2@@B@BL ,FB@@@B8,
* r@B@B@BF, :YBB@B@B@B
* L@B@B@B@P7, .ivXB@B@B@B@B@M@B@
* ,1B@B@B@B@@@BOP2L7i:,. ..,:i7LSNB@@B@B@@@B@B@B@Z5v;.LB@@
* @B@OEB@B@@@B@B@B@B@B@B@B@B@@@B@B@B@B@B@@@B@B@B@B@BM0SJ7i::::i:,u@B@
* B@Bu ::i;7vu2XNGOMB@B@BMB@B@B@B@B@B@@@B1UFuj77ii:::::::iir;r;i.YB@B
* @B@L.:i:i:i::::::::::..Y@B@BMYi:i;SB@B@N:.::i:iirir;r;rii::::ivO@B@
* B@@X::,::::iirir;riri:E@B@1 ,@B@Br:;;r;rii:i::::i7JEB@@@@@B
* @@@B@BBq5v7ii:::::::.2@@@i ..,.. @B@@,,:::::irv2XMB@B@B@B@2@B@:
* .B@BBB@@@B@B@B@BMNP5u7@B@1 .,,:,, :. @B@P50MB@B@B@B@B@@@BS: @@B1
* E@B@ ijGB@B@B@B@B@B@B@Bi .,:,,..@@B@7 B@B@B@B@B@B@BM57. kB@B
* .@B@: .,ivu5Nq@B@u ..,.. SB@B@@@B@PL7i, ,@B@
* @@@8 i@B@: . :B@B@@ B@@2
* i@@@@ 0@B@u B@@B. vB@B
* ,@B@G L@B@BOv:.:iFB@B@M @B@Bi
* vNi S@@B@B@B@B@BM: MB@N
* 758BMqJ,
*
* . YO. vq :G Z:
* SqOMBB@B@Br @@r rBE @B B@@@@@B@ONX8k i::::.OB1.:::.u@O.::::i @B@B@U:@@B@@BPEBu
* B@@NB@k. 5@i uB@E. BM 1U2uUJvirB@@Z r@@B@B@@@B@B@B@B@B@@@B@Bi LB@B@1 BX :@k uLLLvr@BJ:
* iB iBi 7@ .@M8@BGMZZ @@F ,B Pi v@ Bq @i v@ B@
* vuL7r8@S7vJL7N@Z7LLri;72. F7@Bvvv@@ @BX @@@B@B@@@@@B@@@B@B@B 7@ @F Bi @q @B@Bu @B
* N@B@G@@@8@BBOMB@G@BMNXG@, B@ @@ .Bk .:u; i@: Zv 7@ Bk @,;@ ,BY @B B@
* r@ @G 5. ,@v BZ :::,.r@E .::i, @B B@ .@BL 7@ @F B:i@. .@ @M @B
* 7B: ,vO, @@ iB@: @B 7@:MB@B@B@@@B@B@BM @@. B: 2@q 7@ BS @i 0@ B. @O B@
* ,r2EBB@B@B@Bi G@ @BB B@ @B @S : r@ .. 7B @F @7 B7 @ @B @B
* E@B@UOBr @B@Bi L@0PB .BZ .@B@B@B@B@B@B@B@B@B@B@B@, r@ BF @i @G B@B@B B@
* 7@, kB@U ;r @@@. .@Z GBuL@iBBi vB@B@q BP:5@7 @u,. @B
* LBi YB@BrB@ @@ @B:L@Br BM .M@B rB rB@J v@. Pi @XZ8r . B@
* . G@i B@BM. ,B@, @B iB@B N, 7r..q@k ,LB@B8 J@, i@B@B1r Br @@
* MB@B@B ,i B@B@B, B@: @B@B@F .@BB: P@i :OBZ .@U B@B@B:
* .ll rB. :
*
*/
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.text.DecimalFormat;
import java.util.*;
public class Main {
public static InputReader in = new InputReader(new BufferedInputStream(System.in));
public static PrintWriter out = new PrintWriter(System.out);
public static void main(String[] args){
String s1 = in.nextLine();
String s2 = in.nextLine();
HashSet hs = new HashSet<>();
int len = s2.length(), len2 = s1.length();
for (int i = 0; i < len; i++) {
hs.add(s2.charAt(i));
}
for (char ch : hs) {
s1 = s1.replace(String.valueOf(ch), "");
}
out.println(s1);
out.flush();
out.close();
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public String nextLine() {
String str = null;
try {
str = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public Double nextDouble() {
return Double.parseDouble(next());
}
public BigInteger nextBigInteger() {
return new BigInteger(next());
}
}
}
c++代码:
#include
using namespace std;
int main() {
string str1, str2;
int flag = 0, i, j, len1, len2;
getline(cin, str1);
getline(cin, str2);
len1 = str1.size();
len2 = str2.size();
for(i = 0; i < len1; i++) {
for(j = 0; j < len2; j++)
if(str1[i] == str2[j]) break;
if(j == len2) printf("%c", str1[i]);
}
printf("\n");
return 0;
}
L1-012 计算指数
#include
using namespace std;
int main() {
int n;
cin >> n;
cout << "2^" << n << " = " << pow(2, n) << endl;
return 0;
}
L1-013 计算阶乘和
#include
using namespace std;
int main() {
int n, sum = 0;
cin >> n;
for(int i = 1; i <= n; i++) {
int t = 1;
for(int j = 2; j <= i; j++)
t *= j;
sum += t;
}
cout << sum << endl;
return 0;
}
L1-014 简单题
#include
using namespace std;
int main() {
cout << "This is a simple problem." << endl;
return 0;
}
L1-015 跟奥巴马一起画方块
#include
using namespace std;
int main() {
int n;
char c;
scanf("%d %c", &n, &c);
for(int i = 0; i < (n+1)/2; i++) {
for(int j = 0; j < n; j++)
cout << c;
cout << endl;
}
return 0;
}
L1-016 查验身份证
#include
using namespace std;
string s;
int t, flag, z, i;
int q[17] = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
char m[11] = {'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'};
int main() {
cin >> t;
flag = 0;
while(t--) {
cin >> s;
z = 0;
for(i = 0; i < 17; i++) {
if(s[i] >= '0' && s[i] <= '9') z = (z + (s[i]-'0')*q[i]) % 11;
else {
flag = 1;
cout << s << endl;
break;
}
}
if(i == 17 && m[z] != s[17]) {
flag = 1;
cout << s << endl;
}
}
if(!flag) cout << "All passed" << endl;
return 0;
}
L1-017 到底有多二
#include
using namespace std;
int main() {
char s[55];
int len, count = 0;
double ans;
cin >> s;
len = strlen(s);
for(int i = 0; i < len; i++)
if(s[i] == '2') count++;
if(s[0] == '-') {
if((s[len-1]-'0') % 2 == 0) ans = count * 1.0 / (len-1) * 1.5 * 2 * 100;
else
ans = count * 1.0 / (len-1) * 1.5 * 100;
} else {
if((s[len-1]-'0') % 2 == 0) ans = count * 1.0 / len * 2 * 100;
else
ans = count * 1.0 / len * 100;
}
printf("%.2lf%\n", ans);
return 0;
}
L1-018 大笨钟
#include
using namespace std;
int main() {
int h, m;
scanf("%d:%d", &h, &m);
if(h <= 12) printf("Only %02d:%02d. Too early to Dang.\n", h, m);
else {
for(int i = 13; i <= h; i++)
cout << "Dang";
if(m != 0) cout << "Dang";
cout << endl;
}
return 0;
}
L1-019 谁先倒
#include
using namespace std;
int main() {
int j1, j2, n, a[105], b[105], c[105], d[105], sum, t1 = 0, t2 = 0;
cin >> j1 >> j2;
cin >> n;
for(int i = 0; i < n; i++)
cin >> a[i] >> b[i] >> c[i] >> d[i];
for(int i = 0; i < n; i++) {
if(j1 < 0 || j2 < 0) break;
sum = a[i] + c[i];
if(b[i] == sum && d[i] != sum) {
j1--;
t1++;
}
if(b[i] != sum && d[i] == sum) {
j2--;
t2++;
}
}
if(j1 < 0) cout << "A" << endl << t2 << endl;
if(j2 < 0) cout << "B" << endl << t1 << endl;
return 0;
}
L1-020 帅到没朋友
#include
using namespace std;
int main() {
int n, m, k, num, id[100005], a[100005], flag = 0, p[100005], t = 0;
cin >> n;
for(int i = 0; i < n; i++) {
cin >> k;
for(int j = 0; j < k; j++) {
cin >> num;
if(k > 1) a[num] = 1;
}
}
cin >> m;
for(int i = 0; i < m; i++)
cin >> id[i];
for(int i = 0; i < m; i++) {
if(a[id[i]] == 0) {
flag = 1;
p[t++] = id[i];
a[id[i]] = -1;
}
}
if(flag == 0) cout << "No one is handsome";
else {
printf("%05d", p[0]);
for(int i = 1; i < t; i++)
printf(" %05d", p[i]);
}
return 0;
}
L1-021 重要的话说三遍
#include
using namespace std;
int main() {
cout << "I'm gonna WIN!" << endl << "I'm gonna WIN!" << endl << "I'm gonna WIN!" << endl;
return 0;
}
L1-022 奇偶分家
#include
using namespace std;
int main() {
int n, num, j = 0, o = 0;
cin >> n;
for(int i = 0; i < n; i++) {
cin >> num;
if(num % 2) j++;
else
o++;
}
cout << j << " " << o << endl;
return 0;
}
L1-023 输出GPLT
#include
using namespace std;
int main() {
char s[10005];
int len, a[4] = {0}, t = 0, sum = 0;
cin >> s;
len = strlen(s);
for(int i = 0; i < len; i++) {
if(s[i] == 'G' || s[i] == 'g') a[0]++;
if(s[i] == 'P' || s[i] == 'p') a[1]++;
if(s[i] == 'T' || s[i] == 't') a[3]++;
if(s[i] == 'L' || s[i] == 'l') a[2]++;
}
for(int i = 0; i < 4; i++)
sum += a[i];
for(int i = 1; i <= sum; i++) {
if(a[0] != 0) {
cout << 'G';
a[0]--;
}
if(a[1] != 0) {
cout << 'P';
a[1]--;
}
if(a[2] != 0) {
cout << 'L';
a[2]--;
}
if(a[3] != 0) {
cout << 'T';
a[3]--;
}
}
cout << endl;
return 0;
}
L1-024 后天
#include
using namespace std;
int main() {
int d;
cin >> d;
if(d+2 > 7) cout << d-5 << endl;
else
cout << d+2 << endl;
return 0;
}
L1-025 正整数A+B
坑点:题目规定两个整数都在区间[1,1000]内,因此对于<1或>1000的整数也都应该输出?
代码:
#include
using namespace std;
string s, s1, s2;
int i, j, num1 = 0, num2 = 0, flag1 = 0, flag2 = 0;
int main() {
getline(cin, s);
for(i = 0; i < s.size(); i++)
if(s[i] == ' ') break;
s1 = s.substr(0, i);
for(j = i+1; j < s.size(); j++)
if(s[j] == ' ') break;
s2 = s.substr(i+1, j);
for(i = 0; i < s1.size(); i++) {
if(s1[i] >= '0' && s1[i] <= '9') num1 = num1 * 10 + (s1[i]-'0');
else {
flag1 = 1;
break;
}
}
if(num1 < 1 || num1 > 1000) flag1 = 1;
for(i = 0; i < s2.size(); i++) {
if(s2[i] >= '0' && s2[i] <= '9') num2 = num2 * 10 + (s2[i]-'0');
else {
flag2 = 1;
break;
}
}
if(num2 < 1 || num2 > 1000) flag2 = 1;
if(flag1 && flag2) cout << "? + ? = ?" << endl;
else if(flag1 && !flag2) cout << "? + " << num2 << " = ?" << endl;
else if(!flag1 && flag2) cout << num1 << " + ? = ?" << endl;
else
cout << num1 << " + " << num2 << " = " << num1+num2 << endl;
return 0;
}
L1-026 I Love GPLT
#include
using namespace std;
int main() {
printf("I\n \nL\no\nv\ne\n \nG\nP\nL\nT\n");
return 0;
}
L1-027 出租
坑点:注意事宜万能头文件的话,给数组取名不能取index,会与评测姬命名冲突。
代码:
#include
using namespace std;
string s;
int k = 0, num[10], f[10];
map m;
int cmp(int a, int b) {
return a > b;
}
int main() {
cin >> s;
for(int i = 0; i < s.size(); i++) {
if(!f[s[i]-'0']) {
num[k++] = s[i] - '0';
f[s[i]-'0'] = 1;
}
}
sort(num, num+k, cmp);
printf("int[] arr = new int[]{");
for(int i = 0; i < k; i++) {
printf(i == 0 ? "%d" : ",%d", num[i]);
m[num[i]] = i;
}
printf("};\nint[] index = new int[]{");
for(int i = 0; i < s.size(); i++)
printf(i == 0 ? "%d" : ",%d", m[s[i]-'0']);
printf("};\n");
return 0;
}
L1-028 判断素数
#include
using namespace std;
int isprim(long long n) {
if(n == 1) return 0;
if(n == 2) return 1;
for(long long i = 2; i <= sqrt(n); i++)
if(n % i == 0) return 0;
return 1;
}
int main() {
int t;
cin >> t;
while(t--) {
long long n;
cin >> n;
if(isprim(n)) printf("Yes\n");
else
printf("No\n");
}
return 0;
}
L1-029 是不是太胖了
#include
using namespace std;
int main() {
double h;
cin >> h;
printf("%.1lf\n", (h-100)*0.9*2);
return 0;
}
L1-030 一帮一
#include
using namespace std;
int n;
string name;
struct student {
int sex, vis;
string name;
}s[55];
int main() {
int n;
cin >> n;
for(int i = 0; i < n; i++) {
cin >> s[i].sex >> s[i].name;
s[i].vis = 0;
}
for(int i = 0; i < n/2; i++) {
cout << s[i].name << " ";
s[i].vis = 1;
for(int j = n-1; j >= 0; j--) {
if(s[i].sex == 0 && s[j].sex && !s[j].vis) {
cout << s[j].name << endl;
s[j].vis = 1;
break;
} else if(s[i].sex == 1 && !s[j].sex && !s[j].vis) {
cout << s[j].name << endl;
s[j].vis = 1;
break;
}
}
}
return 0;
}
L1-031 到底是不是太胖了
#include
using namespace std;
int main() {
int t;
cin >> t;
while(t--) {
double h, m, bm;
cin >> h >> m;
bm = (h-100)*0.9*2;
if(fabs(m-bm) < bm*0.1) cout << "You are wan mei!" << endl;
else if(m > bm) cout << "You are tai pang le!" << endl;
else
cout << "You are tai shou le!" << endl;
}
return 0;
}
L1-032 Left-pad
#include
using namespace std;
int n;
char c;
string s;
int main() {
cin >> n >> c;
getchar();
getline(cin, s);
if(s.size() >= n) {
for(int i = s.size()-n; i < s.size(); i++)
cout << s[i];
} else {
for(int i = 0; i < n-s.size(); i++)
cout << c;
cout << s;
}
return 0;
}
L1-033 出生年
#include
using namespace std;
int y, sum, n;
int f(int t) {
int a[10] = {0}, t1, t2, t3, t4;
sum = 0;
t1 = t % 10;
t2 = t % 100 / 10;
t3 = t / 100 % 10;
t4 = t / 1000;
a[t1]++, a[t2]++, a[t3]++, a[t4]++;
for(int i = 0; i < 10; i++)
if(a[i]) sum++;
if(sum == n) return 1;
else
return 0;
}
int main() {
int t, i;
cin >> y >> n;
i = y;
while(1){
if(f(i)) {
t = i;
break;
}
i++;
}
printf("%d %04d\n", t-y, t);
return 0;
}
L1-034 点赞
#include
using namespace std;
int main() {
int t, a[1005] = {0}, max = -1, p, n, k;
cin >> t;
while(t--) {
cin >> n;
for(int i = 0; i < n; i++) {
cin >> k;
a[k]++;
}
}
for(int i = 1000; i > 0; i--) {
if(a[i] > max) {
max = a[i];
p = i;
}
}
cout << p << " " << max << endl;
}
L1-035 情人节
#include
using namespace std;
int main() {
string s, str[1000];
int k = 0;
while(cin >> s) {
if(s == ".") break;
str[k++] = s;
}
if(k < 2) cout << "Momo... No one is for you ..." << endl;
if(k >= 2 && k < 14) cout << str[1] << " is the only one for you..." << endl;
if(k >= 14) cout << str[1] << " and " << str[13] << " are inviting you to dinner..." << endl;
return 0;
}
L1-036 A乘以B
#include
using namespace std;
int main() {
int a, b;
cin >> a >> b;
cout << a*b << endl;
return 0;
}
L1-037 A除以B
#include
using namespace std;
int main() {
int a, b;
cin >> a >> b;
if(b == 0) printf("%d/%d=Error", a, b);
else if(b < 0) printf("%d/(%d)=%.2lf", a, b, a*1.0/b);
else
printf("%d/%d=%.2lf", a, b, a*1.0/b);
return 0;
}
L1-038 新世界
#include
using namespace std;
int main() {
cout << "Hello World" << endl << "Hello New World" << endl;
return 0;
}
L1-039 古风排版
坑点:要把空格也填入模拟数组中。
#include
using namespace std;
char s[1010][1010];
int main() {
int n;
cin >> n;
getchar();
string str;
getline(cin, str);
int len = str.size(), k = 0, t = 0;
for(int j = 1; k < len; j++) {
t = j;
for(int i = 1; i <= n; i++) {
if(k >= len) s[i][j] = ' ';
else
s[i][j] = str[k];
k++;
}
}
for(int i = 1; i <= n; i++) {
for(int j = t; j >= 1; j--)
cout << s[i][j];
cout << endl;
}
return 0;
}
L1-040 最佳情侣身高差
#include
using namespace std;
int main() {
int t;
cin >> t;
while(t--) {
char c;
double n;
cin >> c >> n;
if(c == 'F') printf("%.2lf\n", n * 1.09);
else
printf("%.2lf\n", n / 1.09);
}
}
L1-041 寻找250
坑点:测试点1会有数之间出现多个空格的情况
代码:
#include
using namespace std;
int main() {
char c;
int num = 0, ans = 0, t = 0, flag = 0, f = 0;
while(scanf("%c", &c)) {
if(c == '\n') break;
if(c == ' ') {
if(num == 250 && flag == 0) {
ans = t;
flag = 1;
}
num = 0;
f = 0;
} else {
if(f == 0) {
t++;
f = 1;
}
if(c == '-') num = -1;
else
num = num * 10 + (c - '0');
}
}
if(num == 250 && flag == 0) ans = t;
cout << ans << endl;
return 0;
}
L1-042 日期格式化
#include
using namespace std;
int main() {
int y, m, d;
scanf("%d-%d-%d", &m, &d, &y);
printf("%d-%02d-%02d\n", y, m, d);
return 0;
}
L1-043 阅览室
解法:模拟
代码:
#include
using namespace std;
int main() {
int n, day = 0, num, h, m, booktot = 0, time = 0, k = 0;
int b[1010] = {0}, hh[1010], mm[1010], anst[1010];
double ansm[1010];
char ch;
cin >> n;
while(day < n) {
scanf("%d %c %d:%d", &num, &ch, &h, &m);
if(num == 0) {
anst[day] = booktot;
if(booktot != 0) {
ansm[day] = time * 1.0/ booktot;
}
booktot = 0;
time = 0;
day++;
} else {
if(ch == 'S') {
hh[num] = h;
mm[num] = m;
b[num] = 1;
} else if(ch == 'E'){
if(b[num] == 1) {
b[num] = 0;
booktot++;
time += (h-hh[num])*60+(m-mm[num]);
}
}
}
}
for(int i = 0; i < n; i++) {
printf("%d %.0lf\n", anst[i], ansm[i]);
}
return 0;
}
L1-044 稳赢
#include
using namespace std;
int k, t = 0;
string s;
int main() {
cin >> k;
while(cin >> s) {
if(s == "End") break;
if(t == k) {
cout << s << endl;
t = -1;
} else {
if(s == "ChuiZi") cout << "Bu" << endl;
else if(s == "JianDao") cout << "ChuiZi" << endl;
else
cout << "JianDao" << endl;
}
t++;
}
return 0;
}
L1-045 宇宙无敌大招呼
#include
using namespace std;
int main() {
string s;
cin >> s;
cout << "Hello " << s << endl;
return 0;
}
L1-046 整除光棍
解法:大呼,java大法好!直接java大数类,哈哈。
代码:
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.text.DecimalFormat;
import java.util.*;
public class Main {
public static InputReader in = new InputReader(new BufferedInputStream(System.in));
public static PrintWriter out = new PrintWriter(System.out);
public static void main(String[] args){
String s = "1";
String ss = "";
BigInteger x = in.nextBigInteger();
BigInteger ans;
int k = 1;
for (int i = 1; ; i++) {
ss += s;
BigInteger tmp = new BigInteger(ss);
if((tmp.mod(x)).equals(BigInteger.ZERO)) {
k = i;
ans = tmp.divide(x);
break;
}
}
out.println(ans + " " + k);
out.flush();
out.close();
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public String nextLine() {
String str = null;
try {
str = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public Double nextDouble() {
return Double.parseDouble(next());
}
public BigInteger nextBigInteger() {
return new BigInteger(next());
}
}
}
L1-047 装睡
#include
using namespace std;
int n, h, m;
string name;
int main() {
cin >> n;
for(int i = 0; i < n; i++) {
cin >> name >> h >> m;
if(h < 15 || h > 20 || m < 50 || m > 70) cout << name << endl;
}
return 0;
}
L1-048 矩阵A乘以B
#include
using namespace std;
int ra, ca, rb, cb, a[110][110], b[110][110], c[110][110];
string name;
int main() {
cin >> ra >> ca;
for(int i = 1; i <= ra; i++)
for(int j = 1; j <= ca; j++)
cin >> a[i][j];
cin >> rb >> cb;
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 << endl;
else {
for(int i = 1; i <= ra; i++)
for(int k = 1; k <= ca; k++)
if(a[i][k] != 0) {
for(int j = 1; j <= cb; j++)
c[i][j] += a[i][k] * b[k][j];
}
cout << ra << " " << cb << endl;
for(int i = 1; i <= ra; i++) {
for(int j = 1; j <= cb; j++)
printf(j == 1 ? "%d" : " %d", c[i][j]);
printf("\n");
}
}
return 0;
}
L1-049 天梯赛座位分配
解法:模拟
代码:
#include
using namespace std;
int m[110], t[110];
queue q[110];
queue sit[110];
int main() {
int n, sum, sumd, num = 1, last = 0, lasts = 0;
cin >> n;
sumd = n;
for(int i = 1; i <= n; i++) {
cin >> m[i];
sum += m[i];
m[i] *= 10;
t[i] = m[i];
}
while(sumd > 1) {
for(int i = 1; i <= n; i++) {//每个学校
if(m[i] == 0) continue;
q[i].push(num);
num++;
lasts = i;
m[i]--;
if(m[i] == 0) {
sumd--;
}
}
}
for(int i = 1; i <= n; i++) {
if(m[i] != 0) {
last = i;
break;
}
}
for(int i = 1; i <= n; i++) {
cout << "#" << i << endl;
int c = 0, dr = 0;
while(!q[i].empty()) {
if(c == 0) cout << q[i].front();
else
cout << " " << q[i].front();
q[i].pop();
c++;
dr++;
if(c == 10) {
c = 0;
if(dr < t[i]) cout << endl;
}
}
if(dr < t[i]) {
int h = 1;
if(last == lasts) h = 0;
for(int j = 1; j <= (t[i]-dr); j++) {
if(j%10 == 1) cout << num+j*2-1-h;
else
cout << " " << num+j*2-1-h;
if(j % 10 == 0 && j != (t[i]-dr)) cout << endl;
}
}
if(t[i] != 0) cout << endl;
}
return 0;
}
L1-050 倒数第N个字符串
#include
using namespace std;
int main() {
int n, num, a[110] = {0}, k = 0, tmp;
cin >> num >> n;
tmp = n-1;
while(tmp) {
a[k++] = tmp % 26;
tmp /= 26;
}
for(int i = num-1; i >= 0; i--) {
if(i == 0) {
if(a[i] == 0) cout << 'z';
else
cout << (char)('z'-a[i]);
}
else
cout << (char)('z'-a[i]);
}
cout << endl;
return 0;
}
L1-051 打折
#include
using namespace std;
int main() {
double a, b;
cin >> a >> b;
printf("%.2lf\n", a*b/10);
return 0;
}
L1-052 2018我们要赢
#include
using namespace std;
int main() {
printf("2018\nwo3 men2 yao4 ying2 !\n");
return 0;
}
L1-053 电子汪
#include
using namespace std;
int a, b;
int main() {
cin >> a >> b;
for(int i = 0; i < a+b; i++)
cout << "Wang!";
return 0;
}
L1-054 福到了
#include
using namespace std;
int n;
char ch, s1[110][110], s2[110][110];
int main() {
scanf("%c%d", &ch, &n);
for(int i = 1; i <= n; i++) {
getchar();
for(int j = 1; j <=n; j++)
scanf("%c", &s1[i][j]);
}
int ii = n, jj = n;
for(int i = 1; i <= n; i++) {
jj = n;
for(int j = 1; j <=n; j++) {
if(s1[i][j] == ' ') s2[ii][jj] = ' ';
else
s2[ii][jj] = ch;
jj--;
}
ii--;
}
int flag = 0;
for(int i = 1; i <= n; i++) {
if(flag == 1) break;
for(int j = 1; j <=n; j++) {
if(s1[i][j] == ' ' && s2[i][j] != ' ') {
flag = 1;
break;
}
if(s1[i][j] == '@' && s2[i][j] == ' ') {
flag = 1;
break;
}
}
}
if(flag == 0) printf("bu yong dao le\n");
for(int i = 1; i <= n; i++) {
for(int j = 1; j <=n; j++)
printf("%c", s2[i][j]);
printf("\n");
}
return 0;
}
L1-055 谁是赢家
#include
using namespace std;
int main() {
int pa, pb, a = 0, b = 0, x;
cin >> pa >> pb;
for(int i = 0; i < 3; i++) {
cin >> x;
if(x == 0) a++;
else
b++;
}
if((pa > pb && a > 0) || (pa < pb && a == 3)) cout << "The winner is a: " << pa << " + " << a << endl;
if((pb > pa && b > 0) || (pb < pa && b == 3)) cout << "The winner is b: " << pb << " + " << b << endl;
return 0;
}
L1-056 猜数字
#include
using namespace std;
char s1[110][110], s2[110][110];
struct play {
char name[10];
int num;
}p[10010];
int main() {
int sum = 0, ans, maxn = 101, n;
double ave;
cin >> n;
for(int i = 0; i < n; i++) {
cin >> p[i].name >> p[i].num;
sum += p[i].num;
}
ave = sum * 1.0 / (2*n);
for(int i = 0; i < n; i++) {
if(abs(p[i].num-ave) < maxn) {
maxn = abs(p[i].num-ave);
ans = i;
}
}
cout << (int)ave << " " << p[ans].name << endl;
return 0;
}
L1-057 PTA使我精神焕发
#include
using namespace std;
int main() {
printf("PTA shi3 wo3 jing1 shen2 huan4 fa1 !\n");
return 0;
}
L1-058 6翻了
注意:对于寻找'6'停止的位置,如果要开别的变量来标记break位置,注意'6'一直到字符串结束这种情况是不会break的,所以最好是把下标变量定义在循环外。
#include
using namespace std;
string s;
int k;
int main() {
getline(cin, s);
for(int i = 0; i < s.size(); i++) {
if(s[i] == '6') {
int j = i + 1;
for(j = i+1; j < s.size(); j++)
if(s[j] != '6') break;
if(j - i > 9) {
cout << "27";
i = j - 1;
} else if(j - i > 3) {
cout << "9";
i = j - 1;
} else {
cout << s[i];
}
} else {
cout << s[i];
}
}
return 0;
}
L1-059 敲笨钟
#include
using namespace std;
string s;
int t, p1, p2, num;
int main() {
cin >> t;
getchar();
while(t--) {
getline(cin, s);
p1 = (int)s.find("ong,");
p2 = (int)s.find("ong.");
if(p1 != -1 && p2 != -1) {
num = 1;
for(; p2 >= 0; p2--)
if(s[p2] == ' ') {
if(num == 3) break;
else
num++;
}
for(int i = 0; i <= p2; i++)
cout << s[i];
cout << "qiao ben zhong." << endl;
} else {
cout << "Skipped" << endl;
}
}
return 0;
}
L1-060 心理阴影面积
#include
using namespace std;
int main() {
int x, y;
cin >> x >> y;
cout << 5000 - x*y/2 - (100-x)*y - (100-x)*(100-y)/2 << endl;
return 0;
}
L1-061 新胖子公式
#include
using namespace std;
double m, h;
int main() {
cin >> m >> h;
printf("%.1lf\n", m/(h*h));
if(m/(h*h) > 25) cout << "PANG" << endl;
else
cout << "Hai Xing" << endl;
return 0;
}
L1-062 幸运彩票
#include
using namespace std;
int t;
string s;
int main() {
cin >> t;
while(t--) {
cin >> s;
if((s[0]-'0')+(s[1]-'0')+(s[2]-'0') == (s[3]-'0')+(s[4]-'0')+(s[5]-'0')) cout << "You are lucky!" << endl;
else
cout << "Wish you good luck." << endl;
}
return 0;
}
L1-063 吃鱼还是吃肉
#include
using namespace std;
int t, sex, h, m;
int main() {
cin >> t;
while(t--) {
cin >> sex >> h >> m;
if(sex) {
if(h > 130) {
cout << "ni li hai!";
if(m > 27) cout << " shao chi rou!" << endl;
else if(m < 27) cout << " duo chi rou!" << endl;
else
cout << " wan mei!" << endl;
} else if(h < 130) {
cout << "duo chi yu!";
if(m > 27) cout << " shao chi rou!" << endl;
else if(m < 27) cout << " duo chi rou!" << endl;
else
cout << " wan mei!" << endl;
} else {
cout << "wan mei!";
if(m > 27) cout << " shao chi rou!" << endl;
else if(m < 27) cout << " duo chi rou!" << endl;
else
cout << " wan mei!" << endl;
}
} else {
if(h > 129) {
cout << "ni li hai!";
if(m > 25) cout << " shao chi rou!" << endl;
else if(m < 25) cout << " duo chi rou!" << endl;
else
cout << " wan mei!" << endl;
} else if(h < 129) {
cout << "duo chi yu!";
if(m > 25) cout << " shao chi rou!" << endl;
else if(m < 25) cout << " duo chi rou!" << endl;
else
cout << " wan mei!" << endl;
} else {
cout << "wan mei!";
if(m > 25) cout << " shao chi rou!" << endl;
else if(m < 25) cout << " duo chi rou!" << endl;
else
cout << " wan mei!" << endl;
}
}
}
return 0;
}
L1-064 估值一亿的AI核心代码
解法:模拟。
先把原串的除"I”外的大写字母转成小写,把"?"转成"!",然后根据空格作为分隔,把分隔得到的子串放进vector中,再逐个进行处理。在输出的时候根据输出内容判断输出某子串时是否要先输出空格隔开(如果某子串第一个字符为标点符号则不需要输出空格,因为题目要求“把标点符号前面的空格删掉”)。
坑点:注意“独立”的意思—— 这里“独立”是指被空格或标点符号分隔开的单词;
也就是说“独立”包含四种情况(测试点1、3、4):
左边无任何字符,右边被空格或者标点符号分隔;如:"can you?"、"could you?"、"me?"、"I?"。
左右都被空格或者标点符号分隔;如:"?can you?"、"?could you?"、"?me?"、"?I?"。
右边无任何字符,左边被空格或者标点符号分隔;如:"?can you"、"?could you"、"?me"、"?I"。
左右都无字符。如:"can you"、"could you"、"me"、"I"。
上述例子都是要进行置换的!
代码:
#include
using namespace std;
int t, p, flag;
string s, tmp;
int main() {
cin >> t;
getchar();
while(t--) {
getline(cin, s);
vector v;
tmp = "";
cout << s << endl << "AI: ";
for(int i = 0; i < s.size(); i++) {
if(isupper(s[i]) && s[i] != 'I') s[i] = tolower(s[i]);
else if(s[i] == '?') s[i] = '!';
}
for(int i = 0; i < s.size(); i++) {//空格作为分隔符,将得到所有子串放入vector
if(s[i] != ' ') tmp += s[i];
else {
if(tmp != "") v.push_back(tmp);
tmp = "";
}
}
if(tmp != "") v.push_back(tmp);
for(int i = 0; i < v.size(); i++) {//逐个子串处理
if(i < v.size()-1 && v[i].size() >= 3 && v[i+1].size() >= 3 && v[i].substr(v[i].size()-3, v[i].size()) == "can" && v[i+1].substr(0, 3) == "you") {//处理can you的置换
flag = 0;
if((v[i].size() > 3 && !ispunct(v[i][v[i].size()-4])) || (v[i+1].size() > 3 && !ispunct(v[i+1][3]))) flag = 1;//can前一个字符或者you后一个字符不是标点符号
if(!flag) {//满足条件,进行置换
v[i+1] = "can" + v[i+1].substr(3, v[i+1].size());
v[i] = v[i].substr(0, v[i].size()-3) + "I";
}
} else if(i < v.size()-1 && v[i].size() >= 5 && v[i+1].size() >= 3 && v[i].substr(v[i].size()-5, v[i].size()) == "could" && v[i+1].substr(0, 3) == "you") {//处理could you的置换
flag = 0;
if((v[i].size() > 5 && !ispunct(v[i][v[i].size()-6])) || (v[i+1].size() > 3 && !ispunct(v[i+1][3]))) flag = 1;//could前一个字符或者you后一个字符不是标点符号
if(!flag) {//满足条件,进行置换
v[i+1] = "could" + v[i+1].substr(3, v[i+1].size());
v[i] = v[i].substr(0, v[i].size()-5) + "I";
}
} else if(v[i] == "I" || v[i] == "me") {//处理单独成子串的I和me
v[i] = "you";
} else {
p = v[i].find('I');
while(p != string::npos) {//处理不单独成子串的"单独"的I,如"I?"、"?I"、"?I?"
if(p == 0) {//I处于子串开头
if(ispunct(v[i][1])) v[i] = "you" + v[i].substr(1, v[i].size());
} else if(p == v[i].size()-1) {//I处于子串结尾
if(ispunct(v[i][p-1])) v[i] = v[i].substr(0, p) + "you";
} else {//I处于子串中间部分
if(ispunct(v[i][p+1]) && ispunct(v[i][p-1])) v[i] = v[i].substr(0, p) + "you" + v[i].substr(p+1, v[i].size());
}
p = v[i].find('I', p+1);
}
p = v[i].find("me");
while(p != string::npos) {//处理不单独成子串的"单独"的me,如"me?"、"?me"、"?me?"
if(p == 0) {
if(ispunct(v[i][2])) v[i] = "you" + v[i].substr(2, v[i].size());
} else if(p == v[i].size()-2) {
if(ispunct(v[i][p-2])) v[i] = v[i].substr(0, p-1) + "you";
} else {
if(ispunct(v[i][p+2]) && ispunct(v[i][p-2])) v[i] = v[i].substr(0, p-1) + "you" + v[i].substr(p+2, v[i].size());
}
p = v[i].find("me", p+2);
}
}
}
for(int i = 0; i < v.size(); i++) {
if(i == 0 || ispunct(v[i][0])) cout << v[i];//如果该子串第一个字符是标点符号,就不需要与前一个子串相隔空格
else
cout << " " << v[i];
}
cout << endl;
}
return 0;
}