日记

2024 年 3 月 13 日

学习

C 语言 switch case 结构

switch(a/10){
    case 10:printf("A");break;
	default:printf("");
}
  • 以上为 switch 使用的一个例子.
  • 如下为今日程序竞赛一个数组遍历取最大值的好方法
int a, b, c, i;
scanf("%d", &a);
for (i = 0; i < a; i++)
{
    scanf("%d", &b);
    if (b > c)
    {
        c = b;
    }
}

饮食建议

  • 少吃辣椒
  • 多吃水果

2024年3月13日

学习

数组采用定式,嵌入的数组不会轻易改变
如斐波那契数列:
for(i=0;i<100;i++){
    a[i+2]=a[i]+a[i+1];
}
然后直接读取即可得出

洛谷注意事项:

- 洛谷中全部无法通过的需要改数据类型、没有输入的数进行初始化等等。
- 洛谷测评只有一部分对时考虑特殊情况,如是否可以=等待。
- 输入有问题时检查输入类型,比如我曾把%lf写成%.lf,这是错的。
- 输入的数据类型对应固定的格式字符,比如double对应%lf,float对应%f,但对于输出的却没有固定格式。
- 洛谷中90分可能是m=0没有考虑。

while用法:

while(m!=0){
	if(m%10==3){
		c++;
	}
	m=m/10;
}

可以用数组里套数组来记录一个数出现的次数,并去重,如:

for(i=0;i<a;i++)
	{
		scanf("%d",&b[i]);
		c[b[i]]++;
		if(c[b[i]]<2)
		printf("%d ",b[i]);
	}

2024年3月14日

学习

我也是呵呵了

  • 关于求质数:
bool check(int x){  
	for(int i=2;i*i<=x;i++){  
		if(x%i==0)return 0;  
	}  
	return 1;  
}
  • 这个函数用于检查一个数x是否为质数。它通过遍历从$2$到$sqrt(x)$的所有整数,并检查$x$是否可以被这些整数整除。如果$x$可以被其中的任何一个数整除,那么$x$就不是质数,函数返回$0$。如果遍历完所有的数都没有找到可以整除x的数,那么$x$就是质数,函数返回$1$。
  • 注意:这里只遍历到$sqrt(x)$是因为,如果一个数$x$不是质数,那么它必定有一个因数不大于$sqrt
    (x)$。
    判断一个区间的素数有多少个:
#include<stdio.h>
#include<math.h>
#include<stdbool.h>
int l,m,c=0;
bool check(int x){
	if(x<2) return 0;
	for(int i=2;i<=sqrt(x);i++){
	if(x%i==0) return 0;}
return 1;}
int main(){
	scanf("%d%d",&l,&m);
	if(l>m){
		l=l+m;m=l-m;l=l-m;
	}
	for(int j=l;j<=m;j++){
		if(check(j)){
		c++;
	}
	}
	printf("%d",c);
	return 0;
}

饮食建议

多吃辣椒,以毒攻毒

2024年3月15日

学习

啥也没干

如果有多层循环嵌套,对于原数组的遍历放在for的最里层;
a[g[i]]++;是默认从a[0]开始计数的;

无限输入并一直循环:

while(scanf("%s",s)){
	
}

对于if和else:

if(){}
if(){}说明都两个if进入;

if(){}
else(){}说明进入if后就不进入else,否则就进入else;

B2126 连续出现的字符:

#include <stdio.h>
#include <string.h>
int main()
{
	int m,c = 1;
	scanf("%d", &m);
	char a[1010000];
	scanf("%s",a);
	for (int i = 0; i < strlen(a); i++)
	{
		if (a[i] == a[i - 1])
			c++;
		else c = 1;
		if (c == m)
		{
			printf("%c", a[i]);
			return 0;
		}
	}
	printf("No");
	return 0;
}
输入
3
abcccaaab
输出
c

B2122 单词翻转:

#include<stdio.h>
#include<string.h>
int main()
{
	char a[1010000];
	while(~scanf("%s",a)){
		for(int i=strlen(a)-1;i>=0;i--)
		putchar(a[i]);
		putchar('\n');
	}
	return 0;
}
输入 
olleh dlrow
输出 
hello
world

B2096 直方图:

#include <stdio.h>
int main()
{
	int m, c = 0;
	scanf("%d", &m);
	int x = 0;
	int a[1000000000];
	for (int i = 0; i < m; i++)
	{
		scanf("%d", &x);
		a[x]++;
		if (x > c)
		{
			c = x;
		}
	}
	for (int i = 0; i <= c; i++)
		printf("%d\n", a[i]);
	return 0;
}
输入
5
1 1 2 3 1
输出
0
3
1 
1

B2137 判决素数个数:

#include<stdio.h>
#include<math.h>
#include<stdbool.h>
int l,m,c=0;
bool check(int x){
	if(x<2) return 0;
	for(int i=2;i<=sqrt(x);i++){
	if(x%i==0) return 0;}
return 1;}
int main(){
	scanf("%d%d",&l,&m);
	if(l>m){
		l=l+m;m=l-m;l=l-m;
	}
	for(int j=l;j<=m;j++){
		if(check(j)){
		c++;
		//printf("%d ",j);
	}
	}
	printf("%d",c);
	return 0;
}
输入 
1 100
输出 
25

B2073 求小数的某一位:

#include <stdio.h>
int main()
{
  int a,b,n; 
  scanf("%d%d%d",&a,&b,&n);
  for(int i=1;i<=n;i++){
  a%=b;
  a*=10;
}
a/=b;
printf("%d",a);
	return 0;
}
输入 
1 2 1
输出 
5

B2098 整数去重:

#include<stdio.h>
int main()
{
	int n,a[22222],b[2000];
	scanf("%d",&n);
	int i;
	for(i=0;i<n;i++){
		scanf("%d",&a[i]);
		b[a[i]]++;
		if(b[a[i]]<2) 
		printf("%d ",a[i]);
	}
	return 0;
}
输入 
5
10 12 93 12 75
输出 
10 12 93 75

B2065 鸡尾酒疗法:

#include <stdio.h>
int main()
{
	int a, i;
	double b, c;
	double d[20];
	scanf("%d", &a);
	for (i = 0; i < 20; i++)
	{
		d[i] = 0.0;
	}
	for (i = 0; i < a; i++)
	{
		scanf("%lf %lf", &b, &c);
		d[i] = c / b;
	}
	for (i = 1; i < a; i++)
	{

		if (d[0] - d[i] > 0.05)
			printf("worse\n");
		else if (d[i] - d[0] > 0.05)
			printf("better\n");
		else if (d[i] - d[0] < 0.05)
			printf("same\n");
	}
	return 0;
}
输入 
5
125 99
112 89
145 99
99 97
123 98
输出 
same
worse
better
same

待做:B2074,B2077,B2093,B2094,B2095,B2100,B2115

2024.3.17

学习

忙了一天,好累

2024.3.18

洛谷打卡

2024.3.19

我以为我已经几天没写md了;
记住最小公因数的暴力穷举法
记住阶乘求和只需要一个for循环

  • 如下是一个计算一串字符串中的字母,数字,空格,其他字符的数量:
#include<stdio.h>
#include<ctype.h>
int main(){
	char str[1000];
	int alpha=0,digit=0,space=0,other=0;
	fgets(str,sizeof(str),stdin);
	for(int i=0;str[i]!='\0';i++){
	if(isalpha(str[i])){alpha++;}
	else if(isdigit(str[i])){digit++;}
	else if(isspace(str[i])){space++;}
	else {other++;}
	}
	printf("%d %d %d %d",alpha,digit,space,other);
	return 0;
}

2024.3.20

感觉c学到瓶颈了,学学前端吧,先写html;顺便弄一下java;

2024.3.21

java大计:启动

2024.3.25

java ,html

通过输出平行四边形,我发现顺序表的输出我忽略了一个关键的问题,就是数据元素会一个一个输出,不像数组,随着不断地运算可能会占用原先就算出结果的空间。
posted @ 2024-03-25 21:44  pengfu_xin  阅读(12)  评论(0编辑  收藏  举报