A.字符統計
題解:模擬,比賽的時候在思考怎么輸出最后一個樣例,結果發現比賽上面自測能輸出,應該是遇到‘\0’結束,然后注意下didn’t是一個單詞,根據空格統計單詞,而且輸入字串應當合法,
#include<stdio.h>
#include<string.h>
char s[1100];
int main()
{
int t;scanf("%d", &t);
getchar();
while (t--){
int line = 0, word = 0, ch = 0;
while (gets(s) && strcmp(s, "=====")){
int l = strlen(s), ok = 0;
line++, ch += l;
for (int i = 1; i < l; i++){
if (s[i] == ' ' && s[i - 1] != ' ') ok = 1, word++;
}
if (ok) word++;//最后一個空格后面有單詞
}
printf("%d %d %d\n", line, word, ch);
}
}
B.連分數
題解:思維,類似于輾轉相除法,除數作被除數,余數作除數,
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;scanf("%d", &t);
while (t--){
int a, b;scanf("%d%d", &a, &b);
printf("%d/%d = ", a, b);
if (a % b == 0){
printf("%d\n", a / b);
continue;
}
int cnt = 0;
while (a % b){
cnt++;
printf("%d", a / b);
int num1 = b, num2 = a % b;
a = num1, b = num2;
if (a % b == 0){
printf("+1/%d", a / b);
for (int i = 1; i < cnt; i++)
printf("}");//‘}’對應‘{’的數量
printf("\n");
break;
}
else printf("+1/{");
}
}
}
D.購物
題解:模擬,
#include<bits/stdc++.h>
using namespace std;
struct node
{
char str[150];
int num;
}k[110];
int main()
{
int t;scanf("%d", &t);
while (t--){
int s, n;scanf("%d%d", &s, &n);
for (int i = 0; i < s; i++)
scanf("%s%d", k[i].str, &k[i].num);
while (n--){
map<string, int>mp;
int a;char b[150];
scanf("%d", &a);
while (a--){
scanf("%s", b);
mp[b] = 1;
}
for (int i = 0; i < s; i++)
if (!mp[k[i].str] && k[i].num > 0) k[i].num--;
}
int res = 0;
for (int i = 0; i < s; i++)
if (k[i].num) res++;
if (!res) printf("Need to be lucky\n");
else printf("%d\n", res);
}
}
E.喝可樂
題解:列舉,資料小,列舉蜂蜜和生姜可樂的數量,
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;scanf("%d", &t);
while (t--){
int n, a, b;scanf("%d%d%d", &n, &a, &b);
int maxn = 0;
for (int i = 0; i <= n; i++){
int na = i, nb = n - i;
int num = n;
while (na >= a || nb >= b){
nb += na / a, num += na / a, na %= a;
na += nb / b, num += nb / b, nb %= b;
}
maxn = max(maxn, num);
}
printf("%d\n", maxn);
}
}
F.天旋地轉
題解:模擬,寫出四個方向對應的移動方式,這里r是逆時針,l是順時針,
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;scanf("%d", &t);
while (t--){
int n;scanf("%d", &n);
int p = 0;
long long x = 0, y = 0;
while (n--){
char s;int k;
cin >> s >> k;
if (s == 'r'){
p -= k;
while (p < 0) p += 4;
continue;
}
if (s == 'l'){
p += k;
if (p >= 4) p %= 4;
continue;
}
if (!p){
if (s == 'w') y += k;
else if (s == 's') y -= k;
else if (s == 'a') x -= k;
else x += k;
}
else if (p == 1){
if (s == 'w') x += k;
else if (s == 's') x -= k;
else if (s == 'a') y += k;
else y -= k;
}
else if (p == 2){
if (s == 'w') y -= k;
else if (s == 's') y += k;
else if (s == 'a') x += k;
else x -= k;
}
else{
if (s == 'w') x -= k;
else if (s == 's') x += k;
else if (s == 'a') y -= k;
else y += k;
}
//printf("%d %d\n", x, y);
}
printf("%lld %lld\n", x, y);
}
}
I.三角尼姆
題解:1.打表,和為奇,Alice贏;和為偶,Bob贏,
n<=2,每人每次只能放一個,都是A贏;n=3,考慮最優解,每人每次拿的球應當盡可能的多,那么另外一個人拿到最后一個球的可能性就越大,此時B先拿最底下三個必贏;n=4,同理B拿最底下三個必贏,通過觀察不難發現與和有關,
2.思維,每人每次拿一個或三個,那么一輪就拿偶數個,所以,如果和為偶數,恰好輪到A,A拿完最后一個球,A輸;如果和為奇數,B多拿一次,B拿完最后一個球,B輸,
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;scanf("%d", &t);
while (t--){
int n;scanf("%d", &n);
int sum = (n + 1) * n / 2;
if (sum % 2) printf("Alice\n");
else printf("Bob\n");
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/278160.html
標籤:其他
上一篇:MCU學習筆記_C語言基礎
