1005D. Polycarp and Div 3

7 篇文章 0 订阅
4 篇文章 0 订阅

题目链接:http://codeforces.com/problemset/problem/1005/D

题目大意:给一个数字串,问其中有多少个子串能被3整除,每个数字都只能和相邻数字组合且只能组合一次,特别的,0算一种

思路:将数字串中的所有数字都拆成一个个数字,然后依次组合判断,因为被3除得到的余数就只有0,1,2三种,所以组合的数字就只有这些情况(0,12,21,111,222)(前后12省略),然后对这些情况分别判断就行了

代码:

#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
char str[30000000];
int s[30000000];
int main()
{
    memset(str,0,sizeof(str));
    scanf("%s", str);
    int len = strlen(str);
    int cnt = 0;
    for(int i = 0; i < len; i++)//字符串转数字串
    {
        s[i] = (str[i]-'0')%3;
    }
    int c1 = 0, c2 = 0;//1的个数,2的个数
    for(int i = 0; i < len; i++)
    {
        if(s[i]==0)
        {
            cnt++;
            c1 = 0;
            c2 = 0;
            continue;
        }else
        {
            if(s[i]==1)c1++;
            if(s[i]==2)c2++;
            if(c1>=1&&c2==1){cnt++,c1=0,c2=0;}
            if(c1==3){cnt++,c1=0,c2=0;}
            if(c2==2&&c1==1){cnt++,c1=0,c2=0;}
            if(c2==3){cnt++,c1=0,c2=0;}
        }
    }
    printf("%d\n", cnt);
    return 0;

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
E/AndroidRuntime: FATAL EXCEPTION: Thread-2 Process: com.example.cameradiary, PID: 21990 java.lang.IllegalStateException: Room cannot verify the data integrity. Looks like you've changed schema but forgot to update the version number. You can simply fix this by increasing the version number. Expected identity hash: d0562aadd9063ca2a0141765861a8b25, found: 903c8b01c1005d9d530310defd042e19 at androidx.room.RoomOpenHelper.checkIdentity(RoomOpenHelper.kt:147) at androidx.room.RoomOpenHelper.onOpen(RoomOpenHelper.kt:128) at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.onOpen(FrameworkSQLiteOpenHelper.kt:287) at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:428) at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:317) at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.getWritableOrReadableDatabase(FrameworkSQLiteOpenHelper.kt:232) at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.innerGetDatabase(FrameworkSQLiteOpenHelper.kt:190) at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.getSupportDatabase(FrameworkSQLiteOpenHelper.kt:151) at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper.getWritableDatabase(FrameworkSQLiteOpenHelper.kt:104) at androidx.room.RoomDatabase.inTransaction(RoomDatabase.kt:638) at androidx.room.RoomDatabase.assertNotSuspendingTransaction(RoomDatabase.kt:457) at com.example.cameradiary.userDAO_Impl.getALLUsers(userDAO_Impl.java:70) at com.example.cameradiary.SecondActivity$1.run(SecondActivity.java:60)
最新发布
05-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值