输出三角形
#include<iostream>
#include<string>
using namespace std;
int main()
{
char n;
cin>>n;
if(n>='A'&&n<='Z')
{
for(int i=1;i<=n-'A'+1;i++)
{
for(int j=1;j<=n-'A'+1-i;j++)
{
cout<<" ";
}
for(int j=1;j<=i;j++)
{
cout<<(char)('A'+j-1);
}
for(int j=i-1;j>=1;j--)
{
cout<<(char)('A'+j-1);
}
cout<<endl;
}
}
else if(n>='1'&&n<='9')
{
for(int i=1;i<=n-'1'+1;i++)
{
for(int j=1;j<=n-'1'+1-i;j++)
{
cout<<" ";
}
for(int j=1;j<=i;j++)
{
cout<<(char)('1'+j-1);
}
for(int j=i-1;j>=1;j--)
{
cout<<(char)('1'+j-1);
}
cout<<endl;
}
}
return 0;
}
输出方格
#include<iostream>
using namespace std;
int main()
{
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
cout<<"+-";
}
cout<<"+"<<endl;
for(int j=1;j<=m;j++)
{
cout<<"|*";
}
cout<<"|"<<endl;
}
for(int j=1;j<=m;j++)
{
cout<<"+-";
}
cout<<"+";
return 0;
}
字符串s1中查找子串s2出现的次数
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
char s1[1005],s2[1005];
int main()
{
fgets(s1,1004,stdin);
fgets(s2,1004,stdin);
int len1=strlen(s1)-1,len2=strlen(s2)-1;
int ans=0;
for(int i=0;i+len2-1<len1;i++)
{
bool matched=true;
for(int j=0;j<len2;j++)
{
if(s1[i+j]!=s2[j])
{
matched=false;
break;
}
}
if(matched)
{
ans++;
}
}
printf("%d\n",ans);
return 0;
}
-
fgets()函数用于从标准输入流stdin中读取字符串,读取的字符串存储在s1和s2中。其中,1004表示最多读取的字符数,因为最后一个字符需要用来存储字符串结束符’\0’。
-
计算了s1和s2的长度,并定义了一个变量ans来记录s2在s1中出现的次数。
-
然后,代码使用两个for循环来实现字符串匹配。外层循环控制s1中的起始位置,内层循环用于比较s1和s2中的字符是否相等。如果s1和s2中的字符不相等,则matched变量置为false,跳出内层循环;如果s1和s2中的字符全部相等,则matched变量为true,ans加1。
-
最后,代码输出ans的值,即s2在s1中出现的次数。
fgets(s1,1004,stdin);
从标准输入流(stdin)中读取最多1003个字符(第二个参数减1),并将其存储在字符数组s1中。如果读取到换行符或文件结束符EOF,读取操作就会停止。fgets()函数会将读取到的字符串以及换行符一起存储到s1中,因此需要注意s1数组的大小,以免发生溢出。
生日
#include<iostream>
#include<string>
using namespace std;
int whatday(int y,int m,int d)
{
int ans=0;
for(int i=1;i<y;i++)
{
if((i%100!=0&&i%4==0)||i%400==0)
{
ans+=366%7;
ans%7;
}else
{
ans+=365%7;
ans%7;
}
}
for(int i=1;i<m;i++)
{
if(i==1||i==3||i==5||i==7||i==8||i==10||i==12)
{
ans+=31%7;
ans%7;
}
else if(i==4||i==6||i==9||i==11)
{
ans+=30%7;
ans%7;
}
else if((y%100!=0&&y%4==0)||y%400==0)
{
ans+=29%7;
ans%7;
}
else
{
ans+=28%7;
ans%7;
}
}
ans+=(d-1)%7;//减1与数组下标匹配
ans%=7;
return ans;
}
string weekday[7]={"monday","tuesday","wednesday","thursday","friday","saturday","sunday"};
int main()
{
int y,m,d;
cin>>y>>m>>d;
cout<<weekday[whatday(y,m,d)]<<endl;
return 0;
}
- 首先,程序定义了一个函数 whatday,该函数接受三个参数 y、m 和 d,分别表示年、月、日。函数返回一个整数,表示该日期是星期几。
- 接下来,函数中定义了一个变量 ans,用于记录从公元元年到给定日期之间的天数。在 for 循环中,程序先遍历从公元元年到给定年份之间的所有年份,计算这些年份的天数,并将天数累加到 ans 中。具体来说,如果该年份是闰年,则加上 366 天,否则加上 365 天。注意,这里的 %7 是为了将天数转换为星期几的表示方式(0 表示星期日,1 表示星期一,以此类推)。
- 接着,程序遍历从给定年份的 1 月到给定月份之前的所有月份,计算这些月份的天数,并将天数累加到 ans 中。具体来说,如果该月份是 1、3、5、7、8、10 或 12 月,则加上 31 天;如果该月份是 4、6、9 或 11 月,则加上 30 天;如果该月份是 2 月,且给定年份是闰年,则加上 29 天;否则加上 28 天。
- 最后,程序将给定日期的天数减去 1(因为前面已经计算了该日期之前的天数),并将结果对 7 取模,得到该日期是星期几的表示方式。函数返回该值。
公式法
#include<iostream>
#include<string>
using namespace std;
int whatday(int y,int m,int d)
{
if(m<=12)
{
m+=12;
y--;
}
return (d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7;
}
string weekday[7]={"monday","tuesday","wednesday","thursday","friday","saturday","sunday"};
int main()
{
int y,m,d;
cin>>y>>m>>d;
cout<<weekday[whatday(y,m,d)]<<endl;
return 0;
}
恋爱纪念日
#include<cstdio>
int day[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int main()
{
int y,m,d,k;
scanf("%d%d%d%d",&y,&m,&d,&k);
for(int i=1;i<=k;i++)
{
if((y%4==0&&y%100!=0)||y%400==0)
{
day[2]=29;
}
d++;
if(d==day[m]+1)
{
d=1;
m++;
}
if(m==13)
{
m=1;
y++;
}
}
printf("%04d-%02d-%02d\n",y,m,d);
return 0;
}
节假日
#include<cstdio>
int mm[10]={1,5,10,10,10,12};
int dd[10]={1,1,1,2,3,25};
int day[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
void nextday(int &y,int &m,int &d)
{
d++;
if(d==day[m]+1)
{
d=1;
m++;
}
}
int main()
{
int y,w,m,d,sf,ans;
scanf("%d",&y);
for(int i=6;i<=9;i++)
{
scanf("%d%d",&mm[i],&dd[i]);
}
scanf("%d",&w);
if((y%100!=0&&y%4==0)||y%400==0)
{
day[2]++;
}
m=1;
d=1;
sf=0;
ans=0;
while(m<13)
{
if(m==mm[6]&&d==dd[6])
{
ans++;
sf=2;
}
else if(sf)
{
ans++;
sf--;
}else if(w==6||w==7)
{
ans++;
}
else
{
for(int i=0;i<10;i++)
{
if(m==mm[i]&&d==dd[i])
{
ans++;
break;
}
}
}
nextday(y,m,d);
w++;
if(w==8)
{
w=1;
}
}
printf("%d",ans);
return 0;
}
-
首先,定义了三个数组 mm、dd 和 day,分别用于存储月份、日期和每个月的天数。
-
定义了一个函数 nextday,用于计算下一天的日期。该函数的参数是年、月和日,通过修改这三个参数的值来计算下一天的日期。
-
在主函数中,首先读入年份 y,然后读入 4 个日期,最后读入星期几 w。
-
如果该年是闰年,则将二月的天数加 1。
-
初始化月份 m 和日期 d,以及一个计数器 ans 和一个标志变量 sf,分别表示节假日的天数和是否处于节假日中。
-
进入循环,循环条件是月份小于 13。在循环中,首先判断当前日期是否是第一个输入的日期,如果是,则将标志变量 sf 设为 2,表示处于节假日中,并将计数器 ans 加 1;如果不是,则判断标志变量 sf 是否为 0,如果不为 0,则将标志变量 sf 减 1,并将计数器 ans 加 1;如果标志变量 sf 为 0,则判断当前日期是否是周末或者其他输入的日期,如果是,则将计数器 ans 加 1。
-
最后,调用 nextday 函数计算下一天的日期,并将星期几加 1,如果星期几等于 8,则将其设为 1。
-
循环结束后,输出计数器 ans 的值,即节假日的天数