7.1秋天的第一杯奶茶
7-2 统计单词
#include <stdio.h>
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
int n;
scanf("%d", &n);
getchar();
char str[100];
fgets(str, 100, stdin);
int count=0, i=0, j=0;
while(str[i]!='\0')
{
if(str[i]!=' ')
j++;
else
{
if(j==n)
{
count++;
j=0;
}
else
j = 0;
}
i++;
}
if((--j)==n)
count++;
printf("%d\n", count);
}
return 0;
}
7-3 找最大字符串
#include<stdio.h>
#include<string.h>
int main ()
{
char str[4][100];
for (int i = 0; i < 4;i++)
{
fgets(str[i], 100, stdin);
}
char max[100];
strcpy(max, str[0]);
for (int i = 1; i < 4;i++)
{
if(strcmp(max,str[i])<0)
{
strcpy(max, str[i]);
}
}
printf("%s", max);
return 0;
}
7-4 字符串排序
#include <stdio.h>
#include <string.h>
int main()
{
printf("After sorted:\n");
char str[5][100];
for (int i = 0; i < 5; i++)
{
scanf(" %s", str[i]);
}
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4 - i; j++)
{
if (strcmp(str[j], str[j + 1]) > 0)
{
char temp[100];
strcpy(temp, str[j]);
strcpy(str[j], str[j + 1]);
strcpy(str[j + 1], temp);
}
}
}
for (int i = 0; i < 5;i++)
{
printf("%s\n", str[i]);
}
return 0;
}
7-5 缩写期刊名
#include<stdio.h>
#include<string.h>
#include<ctype.h>
int main()
{
int n,i,j,term=0;
scanf("%d\n",&n);
char str[86];
for(i=0;i<n;i++){
gets(str);
for(j=0;j<strlen(str);j++){
term++;
if(str[j]==' ')
term=0;
if(isupper(str[j]))
str[j]=tolower(str[j]);
if(term<=4)
printf("%c",str[j]);
else if(term==5)
printf(".");
}
printf("\n");
term=0;
}
return 0;
}
7-6 魔镜
#include <stdio.h>
#include <string.h>
int jing(char *str, int n)
{
if (n % 2 != 0)
return n;
else
{
int i = 0, j = n – 1;
while (i < j)
{
if (str[i] != str[j])
return n;
i++;
j–;
}
return jing(str, n / 2);
}
}
int main()
{
char str[100];
while (scanf(“%s”, str) != EOF)
{
int len = strlen(str);
int res = jing(str, len);
printf(“%d\n”, res);
}
return 0;
}
7-8 检查密码
#include <stdio.h>
#include <string.h>
int main()
{
int n;
scanf("%d", &n);
getchar();
while (n--)
{
int num = 0, letter = 0, spe = 0;
char str[100];
fgets(str, 100, stdin);
int len = strlen(str)-1;
for (int i = 0; i < len; i++)
{
if (str[i] >= '0' && str[i] <= '9')
num++;
else if (str[i] >= 'a' && str[i] <= 'z' || str[i] >= 'A' && str[i] <= 'Z')
letter++;
else if (str[i] != '.')
spe++;
}
if (len < 6)
printf("Your password is tai duan le.\n");
else if (num == 0)
printf("Your password needs shu zi.\n");
else if (letter == 0)
printf("Your password needs zi mu.\n");
else if (spe != 0)
printf("Your password is tai luan le.\n");
else
printf("Your password is wan mei.\n");
}
return 0;
}
7-9 六大门派身份识别
#include <stdio.h>
#include<math.h>
int is_prime(int n)
{
if(n==0||n==1)
return 0;
if(n==2)
return 1;
int i;
for (i = 2; i <sqrt(n)+1; i++)
{
if (n % i == 0)
return 0;
}
return 1;
}
int gewei(int n)
{
int sum=0;
while(n>0)
{
sum+=n%10;
n/=10;
}
return sum;
}
int main()
{
int c, a, b, n;
scanf("%d", &n);
while (n--)
{
scanf("%1d%4d%4d", &c, &a, &b);
int sum = 0;
if(a>b)
{
int temp;
temp = a;
a = b;
b = temp;
}
for (int i = a; i <= b; i++)
{
if (is_prime(i))
{
sum += gewei(i);
}
}
int r = sum % 6;
// shaolin,wudang,kunlun,emei,huashan,kongtong
if (r == c)
{
switch (c)
{
case 0:
printf("shaolin %d", sum);
break;
case 1:
printf("wudang %d", sum);
break;
case 2:
printf("kunlun %d", sum);
break;
case 3:
printf("emei %d", sum);
break;
case 4:
printf("huashan %d", sum);
break;
case 5:
printf("kongtong %d", sum);
break;
}
}
else
printf("Fighting!");
if(n>0)
printf("\n");
}
return 0;
}
8-1 身高排序
#include<stdio.h>
void bs(int arr[],int n)
{
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - i - 1; j++)
{
if (arr[j] < arr[j + 1])
{
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main()
{
int n;
scanf("%d",&n);
int a[n];
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
bs(a, n);
for(int i=0;i<n;i++)
{
printf("%d ",a[i]);
}
return 0;
}
8-2 密码是否符合要求(2021)
#include <stdio.h>
#include <string.h>
int main()
{
char str[100];
fgets(str, 100, stdin);
int flag_len = 0,flag_num=0,flag_sym=0;
int len = strlen(str);
if (len >= 8 && len <= 16)
flag_len++;
for (int i = 0; i < len; i++)
{
if (str[i] >= 'A' && str[i] <= 'Z')
flag_sym++;
if (str[i] >= '0' && str[i] <= '9')
flag_num++;
}
if (flag_len && flag_sym && flag_num)
printf("YES");
else
printf("NO");
return 0;
}
8-3 字符统计
#include <stdio.h>
#include <string.h>
int main()
{
char str[100];
fgets(str, 100, stdin);
int flag_let = 0,flag_num=0,flag_sym=0;
int len=strlen(str);
for(int i=0;i<len;i++)
{
if(str[i]>='a'&&str[i]<='z'||str[i]>='A'&&str[i]<='Z')
{
flag_let++;
}
else if(str[i]>='0'&&str[i]<='9')
{
flag_num++;
}
else
{
flag_sym++;
}
}
printf("Letter:%d\nDigit :%d\nSymbol:%d", flag_let, flag_num, flag_sym);
return 0;
}
8-4 化验诊断
#include <stdio.h>
#include <string.h>
int main()
{
double a[5];
char g[10];
int n, i, flag;
scanf(“%d”, &n);
for (i = 0; i < n; i++)
{
scanf(“%s”, &g);
flag = 0;
scanf(“%lf %lf %lf %lf %lf”, &a[0], &a[1], &a[2], &a[3], &a[4]);
if (a[0] < 4.0 || a[0] > 10.0)
flag++;
if (a[1] < 3.5 || a[1] > 5.5)
flag++;
if (a[4] < 100 || a[4] > 300)
flag++;
if (!strcmp(g, “male”))
{
if (a[2] < 120 || a[2] > 160)
flag++;
if (a[3] < 42 || a[3] > 48)
flag++;
}
else
{
if (a[2] < 110 || a[2] > 150)
flag++;
if (a[3] < 36 || a[3] > 40)
flag++;
}
if (flag)
{
printf(“%d\n”, flag);
}
else
{
printf(“normal\n”);
}
}
return 0;
}
8-5 谁拿了最多奖学金
#include <stdio.h>
#include <string.h>
typedef struct
{
char name[21];
int avgScore;
int classScore;
char isCadre;
char isWestern;
int paperNum;
int scholarship;
} Student;
int main()
{
int N;
scanf(“%d”, &N);
Student students[N];
for (int i = 0; i < N; i++)
{
scanf(“%s %d %d %c %c %d”, students[i].name, &students[i].avgScore, &students[i].classScore, &students[i].isCadre, &students[i].isWestern, &students[i].paperNum);
students[i].scholarship = 0;
if (students[i].avgScore > 80 && students[i].paperNum >= 1)
{
students[i].scholarship += 8000;
}
if (students[i].avgScore > 85 && students[i].classScore > 80)
{
students[i].scholarship += 4000;
}
if (students[i].avgScore > 90)
{
students[i].scholarship += 2000;
}
if (students[i].avgScore > 85 && students[i].isWestern == ‘Y’)
{
students[i].scholarship += 1000;
}
if (students[i].classScore > 80 && students[i].isCadre == ‘Y’)
{
students[i].scholarship += 850;
}
}
int maxScholarship = 0;
char maxName[21];
for (int i = 0; i < N; i++)
{
if (students[i].scholarship > maxScholarship)
{
maxScholarship = students[i].scholarship;
strcpy(maxName, students[i].name);
}
}
int totalScholarship = 0;
for (int i = 0; i < N; i++)
{
totalScholarship += students[i].scholarship;
}
printf(“%s\n”, maxName);
printf(“%d\n”, maxScholarship);
printf(“%d\n”, totalScholarship);
return 0;
}
8-6 学分统计
#include <stdio.h>
#include <string.h>
int main()
{
int t;
scanf("%d", &t);
int count = 0;
while (t--)
{
char str[100];
char name[100];
scanf("%s", str);
scanf("%s", name);
int score = 0;
scanf("%d", &score);
if (score >= 85)
printf("%s %s A\n", str, name);
else if (score >= 70)
{
printf("%s %s B\n", str, name);
}
else if (score >= 60)
printf("%s %s C\n", str, name);
else
{
printf("%s %s D\n", str, name);
count++;
}
}
printf("%d", count);
return 0;
}
8-7 统计平均成绩和不及格人数
#include <stdio.h>
#include <string.h>
typedef struct stu
{
int std;
char name[20];
float score;
} stu;
int main()
{
int n;
scanf("%d", &n);
float sum = 0, avg = 0;
int count = 0;
stu s[100];
for (int i = 0; i < n; i++)
{
scanf(" %d %s %f", &s[i].std, s[i].name, &s[i].score);
sum += s[i].score;
if (s[i].score < 60)
count++;
}
avg = sum / n;
printf("%.2f %.2f %d", sum, avg, count);
return 0;
}