首页 > 其他分享 >P7911 CSP J 2021 T3 纯模拟 无map 代码

P7911 CSP J 2021 T3 纯模拟 无map 代码

时间:2022-10-23 18:57:36浏览次数:74  
标签:map return int P7911 T3 sprintf str false sscanf

目录

申明

解释在注释里
注释掉的不用管 写错的代码 可借鉴

原题:洛谷P7911

前置知识

sscanf与sprintf

/*
sscanf与sprintf 
sscanf(str,"%d",&n) 其实就是把str的内容以"%d"的格式写入到n中(从左到右)
同理 sprintf(str,"%d",n)就是把n以"%d"的格式写入到str (从右到左) 
*/
#include<cstdio>
#include<iostream>
using namespace std;
int main(){
	int n;
	char str[10]="666";
	char str1[10]; 
	sscanf(str,"%d",&n);
	printf("%d\n",n);
	sprintf(str1,"%d",n);
	printf("%s\n",str1); 
	return 0;
}

输出结果为

666
666

应用

一般都是使用sprinf将整型数据写入到字符串数组,再将字符串数组转为string,从而使用string的方法函数

字符串常用函数

函数总结

代码

#include<bits/stdc++.h>
using namespace std;

//机器数量
int n;

//机器结构体
struct node
{

    //服务机 1 客户机 0
    int type;

    char ip[10000];
};

node SC[1020];

/*//下一个数
int number(int l,int r,string a)
{

    //结果
    int ret=0;

    for(int i=l;i<=r;i++)
    {
        if(i==l&&a[i]=='0'&&a[i+1]>='0'&&a[i+1]<='9')
        {
            return -1;
        }

        else
        {
            do
            {
                ret=ret*10+(a[l]-'0');
                l++;
            }while(l<=r);
        }
    }

    return ret;
}*/

/*//ip地址判断
bool ippd(string a)
{

    //. 和 数字 的数量
    int num1,num2;

    //五个数字
    int ipnum[20];
    int ipi=0;

    for(int i=0;i<a.size();i++)
    {
        if(a[i]=='.')
        {
            num1++;
        }

        if(a[i]==':')
        {
            if(num1!=3)
            {
                return false;
            }
        }
    }

    //从左到右
    int L=0,R;

    for(int i=0;i<a.size();i++)
    {

        if(a[i]=='.'||a[i]==':')
        {
            R=i-1;
            ipnum[++ipi]=number(L,R,a);

            cout<<ipnum[ipi]<<" ";

            if(number(L,R,a)==-1)
            {
                return false;
            }

            num2++;
        }
        L=R+2;

    }

    if(num2!=5)
    {
        return false;
    }

    for(int i=1;i<=4;i++)
    {
        if(!(ipnum[i]>=0&&ipnum[i]<=255))
        {
            return false;
        }
    }

    if(!(ipnum[5]>=0&&ipnum[5]<=65535))
    {
        return false;
    }
    
    return true;
}*/

//ip地址判断
bool ippd(char a[])
{

    //五个数字
    int a1=-1,b1=-1,c1=-1,d1=-1,e1=-1;

    //sscanf输入字符串中的数字
	int t=sscanf(a,"%d.%d.%d.%d:%d",&a1,&b1,&c1,&d1,&e1);

    //必须5个数
	if(t!=5) 
        return false;

    //判断
	if(a1<0||a1>255) 
        return false;
	if(b1<0||b1>255) 
        return false;
	if(c1<0||c1>255) 
        return false;
	if(d1<0||d1>255) 
        return false;
	if(e1<0||e1>65535) 
        return false;


	char a2[35];

    //保存至s2中
	sprintf(a2,"%d.%d.%d.%d:%d",a1,b1,c1,d1,e1);
   
    //接下来判断s2和s是否一样
	int lensa=strlen(a);
    int lensb=strlen(a2);

    return (lensa==lensb);

}

//判重
bool cfpd(int now)
{
    for(int i=1;i<now;i++)
    {
        if(SC[i].type==1&&strcmp(SC[now].ip,SC[i].ip)==0)
        {
            return false;
        }
    }

    return true;
}

//判存在
bool czpd(int now,int* index)
{
    for(int i=1;i<now;i++)
    {
        if(SC[i].type==1&&strcmp(SC[now].ip,SC[i].ip)==0)
        {  
            *index=i;

            return true;
        }
    }

    return false;
}

//操作数组(服务机)
void doitforS(int now)
{
    if(ippd(SC[now].ip)&&cfpd(now))
    {
        cout<<"OK"<<endl;
    }

    else if(ippd(SC[now].ip)&&!cfpd(now))
    {
        cout<<"FAIL"<<endl;
    }

    else
    {
        cout<<"ERR"<<endl;
    }
}

//操作数组(客户机)
void doitforC(int now)
{

    int index;

    if(ippd(SC[now].ip)&&czpd(now,&index))
    {
        cout<<index<<endl;
    }

    else if(ippd(SC[now].ip)&&!czpd(now,&index))
    {
        cout<<"FAIL"<<endl;
    }

    else
    {
        cout<<"ERR"<<endl;
    }
}

//输入
void init()
{
    cin>>n;

    string a;

    for(int i=1;i<=n;i++)
    {
        cin>>a;

        //cout<<a<<endl;

        if(a[0]=='S')
        {
            SC[i].type=1;
        }else
        {
            SC[i].type=0;
        }


        cin>>SC[i].ip;

        //cout<<SC[i].ip<<endl;

        if(SC[i].type==1)
        {
            doitforS(i);
        }

        else
        {
            doitforC(i);
        }
    }
}

int main()
{

    //freopen("internet.in","r",stdin);
    //freopen("internet.out","w",stdout);

    init();

    system("pause");
    return 0;
}

后记

2h做出 ippd部分采用luogu题解 气死我了 做出脑溢血

标签:map,return,int,P7911,T3,sprintf,str,false,sscanf
From: https://www.cnblogs.com/YzaCsp/p/16819125.html

相关文章

  • List和Map、Set的区别
    List和Map、Set的区别结构特点List和Set是存储单列数据的集合,Map是存储键和值这样的双列数据的集合;List中存储的数据是有顺序,并且允许重复;Map中存储的数据是没有顺序的,......
  • Madoka and the Sixth-graders (全排列队列,每一个点可以向外连1条线题型+倍增法处理
    题意:Madoka的教室里有 nn 个座位,一开始,编号为 ii 的座位上坐着编号为 b_i(1\leb_i\len)bi​(1≤bi​≤n) 的同学。门外有排成一队的,编号从 n+1n+1 开始的,......
  • Add AutoMapper
    (注:本文示例使用的是.NETCore3.1)1.配置(Configuration)Reference:https://docs.automapper.org/en/latest/Configuration.html多种方法配置,这里推荐使用配置文件......
  • Flink如何使用DataStreamAPI消费Kafka
    1、到官网查询所在版本的依赖,导入pom.xml(在此用Flink1.13)官网->教程->connectors->datastream->kafka网址:https://nightlies.apache.org/flink/flink-docs-release-1.1......
  • sqlmap的使用方法(linux)
    原文来自:https://blog.csdn.net/weixin_52084568/article/details/123839776sqlmap的使用方法(linux)sqlmap--version查看sqlmap版本sqlmap-h查看sqlmap帮助  ......
  • js map和reduce
    map举例说明,比如我们有一个函数f(x)=x2,要把这个函数作用在一个数组​​[1,2,3,4,5,6,7,8,9]​​上,就可以用​​map​​实现如下:由于​​map()​​方法定义在JavaScr......
  • 今天聊下Java中的HashMap---Java中用的就很多的集合框架
    先说下HashMap的定义HashMap是一个散列表,存储的内容是键值对(key-value)映射。HashMap实现了Map接口,根据键的HashCode值存储数据,具有很快的访问速度,最多允许一条记录的键......
  • HashMap 源码分析(五)
    ......
  • HashMap
    在jdk1.7版本其底层结构是:数组+链表在jdk1.8版本之后底层结构修改成为:数组+链表+红黑树在扩容机制上:jdk1.7:当满足扩容条件后-->其初始默认的容量为16,每次扩容都×2;......
  • countDownLatch和Semaphore
     countDOwnLatch:当线程1调用await方法那么就会阻塞,线程2,线程3,线程4分别调用countDown方法,当线程4调用countDown方法那么阻塞的队列不管有多少个都会依此唤醒,并不会像AQS......