寻找2020的个数(蓝桥杯第十一届)

 

 

#include<bits/stdc++.h>
using namespace std;
char text[500][500];
void copytext(){//将文本内容复制到数组text中
	char c[500];
	int line = 0;
	FILE *fp = fopen("2020.txt","r");
	if (fp==NULL)
	{
		printf("failed to open file!\n");
	}
	else
	{
		while(fgets(c,500,fp))
			strcpy(text[line++],c);
	}
	fclose(fp);

}
void copy2020()
{
    FILE *fp;
    int i=0;
    char c[500];

    if( (fp=fopen("2020.txt","r"))==NULL )
    {
        printf("open file failed.");
        return;
    }
    else
    {
        while(fgets(c,500,fp))
        {
            strcpy(text[i++],c);
        }
    }
    fclose(fp);
}
void C_plus2020()
{
    char c[500];
    int i=0;

    ifstream ifile("2020.txt");
    while(ifile.good())
    {
        ifile.getline(c,500);
        strcpy(text[i++],c);

    }
}
int main()
{
	int i,j=0,count=0;
	C_plus2020();//将文本内容复制到数组text中
//求每行的2020
	for(i=0;i<500;i++)
		for(j=0;j<500;j++)
			if(text[i][j+3]!=NULL){
				if(text[i][j]=='2' && text[i][j+1]=='0' && text[i][j+2]=='2' && text[i][j+3]=='0'){
					count++;
					j=j+1;
				}
			}else{
				break;
			}
//求每列的2020
	for(j=0;j<500;j++)
		for(i=0;i<500;i++)
			if(text[i+3][j]!=NULL){
				if(text[i][j]=='2' && text[i+1][j]=='0' && text[i+2][j]=='2' && text[i+3][j]=='0'){
					count++;
					i=i+1;
				}
			}else{
				break;
			}
//求对角线的2020
	for(i=0;i<500;i++)
		for(j=0;j<500;j++)
			if(text[i+3][j+3]!=NULL){
				if(text[i][j]=='2' && text[i+1][j+1]=='0' && text[i+2][j+2]=='2' && text[i+3][j+3]=='0')
					count++;
			}else{
				break;
			}
	printf("%d\n",count);

	return 0;
}
//#include <stdio.h>
//#include <stdlib.h>
//
//#define N 100

//int main() {
//    FILE *fp;
//    char str[N + 1];
//
//    //判断文件是否打开失败
//    if ( (fp = fopen("2020.txt", "rt")) == NULL ) {
//        puts("Fail to open file!");
//        exit(0);
//    }
//
//    //循环读取文件的每一行数据
//    while( fgets(str, N, fp) != NULL ) {
//        printf("%s", str);
//    }
//
//    //操作结束后关闭文件
//    fclose(fp);
//    return 0;
//}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值