一个结论:一个数,如果它的所有数字之和能被3整除,那么这个数也能被3整除。

最后一位肯定是0或者5,如果没有就impossible。

剩下的就是,如何删除尽量少的数,使所有数字之和为3的倍数。

情况比较多,注意考虑全面。

 #include <cstdio>
#include <cstring>
#include <cstdlib> using namespace std; const int MAXN = ; char str[MAXN];
int cnt[];
int end; void OutPut()
{
bool ok = false;
for ( int i = ; i > ; --i )
{
for ( int j = ; j < cnt[i]; ++j )
{
if (i) ok = true;
printf( "%d", i );
}
} if ( ok )
{
for ( int i = ; i < cnt[]; ++i )
putchar('');
} if ( end == ) puts("");
else puts(""); return;
} bool solved( int sum )
{
int left = sum % ; //删一个数
if ( cnt[left] )
{
--cnt[left];
return true;
}
if ( cnt[ + left] )
{
--cnt[left + ];
return true;
}
if ( cnt[ + left ] )
{
--cnt[ + left ];
return true;
} //删两个数
if ( left == )
{
if ( cnt[] >= )
{
cnt[] -= ;
return true;
}
if ( cnt[] && cnt[] )
{
--cnt[], --cnt[];
return true;
}
if ( cnt[] >= )
{
cnt[] -= ;
return true;
}
if ( cnt[] && cnt[] )
{
--cnt[], --cnt[];
return true;
}
if ( cnt[] && cnt[] )
{
--cnt[], --cnt[];
return true;
}
if ( cnt[] >= )
{
cnt[] -= ;
return true;
}
}
else
{
if ( cnt[] >= )
{
cnt[] -= ;
return true;
}
if ( cnt[] && cnt[] )
{
--cnt[], --cnt[];
return true;
}
if ( cnt[] >= )
{
cnt[] -= ;
return true;
}
if ( cnt[] && cnt[] )
{
--cnt[], --cnt[];
return true;
}
if ( cnt[] && cnt[] )
{
--cnt[], --cnt[];
return true;
}
if ( cnt[] >= )
{
cnt[] -= ;
return true;
}
}
return false;
} int main()
{
//freopen( "in.txt", "r", stdin );
//freopen( "out.txt", "w", stdout );
int T;
scanf( "%d", &T );
while ( T-- )
{
scanf( "%s", str );
memset( cnt, , sizeof(cnt) );
for ( int i = ; str[i]; ++i )
++cnt[ str[i] - '' ]; if ( !cnt[] && !cnt[] )
{
puts("impossible");
continue;
} int sum = ;
for ( int i = ; i < ; ++i )
sum += i * cnt[i]; if ( cnt[] )
--cnt[], end = ;
else
--cnt[], end = ; if ( sum % == )
{
if ( sum == )
{
puts("");
continue;
}
OutPut();
}
else
{
if ( solved( sum ) ) OutPut();
else puts("impossible");
}
}
return ;
}

SPOJ 416 Divisibility by 15 细节题的更多相关文章

  1. SPOJ 416 - Divisibility by 15(贪心)

    糟烂的代码啊...  这个题目思路很简单——末位只可能为0和5,所有数字的和肯定被3整除 没有0和5的肯定不行 否则,把所有数字求和 如果被3整除,则从大到小输出 如果除3余1,则按以下顺序——删1: ...

  2. Codeforces Round #392 (Div. 2)-758D. Ability To Convert(贪心,细节题)

    D. Ability To Convert time limit per test 1 second Cmemory limit per test 256 megabytes input standa ...

  3. 【线段树 细节题】bzoj1067: [SCOI2007]降雨量

    主要还是细节分析:线段树作为工具 Description 我们常常会说这样的话:“X年是自Y年以来降雨量最多的”.它的含义是X年的降雨量不超过Y年,且对于任意Y<Z<X,Z年的降雨量严格小 ...

  4. zoj 3745 Salary Increasing(坑爹的细节题!)

    题目 注意题目中的,引用绝望的乐园中的进一步解释如下: 这是一道浙大月赛的题,一如既往的坑爹,好好一道水题,被搞成一道坑题!!! //注意:r(i) < l(i+1) !细节啊细节! #incl ...

  5. 【20171027中】alert(1) to win 第13,14,15,16题

    第13题 题目: function escape(s) { var tag = document.createElement('iframe'); // For this one, you get t ...

  6. 【交互 细节题 思维题】cf1064E. Dwarves, Hats and Extrasensory Abilities

    第一次做交互真有趣……:挺好的细节思维题 This is an interactive problem. In good old times dwarves tried to develop extr ...

  7. 【2019.10.7 CCF-CSP-2019模拟赛 T2】绝对值(abs)(线段树细节题)

    找规律 设\(p_i=a_{i+1}-a_i\),则答案就是\(\sum_{i=1}^{n-1}p_i\). 考虑若将\(a_i\)加上\(x\)(边界情况特殊考虑),就相当于是将\(p_{i-1}\ ...

  8. bzoj1067——SCOI2007降雨量(线段树,细节题)

    题目描述 我们常常会说这样的话:"X年是自Y年以来降雨量最多的".它的含义是X年的降雨量不超过Y年,且对于任意\(Y<Z<X\),Z年的降雨量严格小于X年.例如2002 ...

  9. Codeforces 571E - Geometric Progressions(数论+阿巴细节题)

    Codeforces 题目传送门 & 洛谷题目传送门 u1s1 感觉此题思维难度不太大,不过大概是细节多得到了精神污染的地步所以才放到 D1E 的罢((( 首先我们对所有 \(a_i,b_i\ ...

随机推荐

  1. 【First Missing Positive】cpp

    题目: Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2 ...

  2. Testing Multi-Tenancy on a Local Machine

    If you are running locally and do not have a domain to map, you can edit your\Windows\System32\drive ...

  3. 【BZOJ】【2705】【SDOI2012】Longge的问题

    欧拉函数/狄利克雷卷积/积性函数 2705: [SDOI2012]Longge的问题 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 1275  Solv ...

  4. Java获取项目中的路径 分类: Java Game 2014-08-14 10:17 122人阅读 评论(0) 收藏

    在项目中经常需要获取某个文件的路径: 在这里提供一些获取路径的方法.. 1.此种方式获取的路径,是当前类所在的路径: UserDAOTest.class.getResource("UserD ...

  5. 【bzoj1010】[HNOI2008]玩具装箱toy

    1010: [HNOI2008]玩具装箱toy Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 9281  Solved: 3719[Submit][St ...

  6. GameMap地图初始化

    init_map(res_path) .初始化mapbase的基本信息 pos2d screen_area = {, }; //普通屏幕大小 m_spBase->init(screen_area ...

  7. [工作记录] Android OpenSL ES: references & AAC related

    AAC V.S. MP3 http://en.wikipedia.org/wiki/Advanced_Audio_Coding#AAC.27s_improvements_over_MP3 AAC pa ...

  8. Websphere中静态资源配置

    Tomcat, Jetty, JBoss, GlassFish 自带的默认Servlet的名字 default Google App Engine 自带的 默认Servlet的名字 ah_defaul ...

  9. 【翻译】Sencha Touch2.4 The Layout System 布局

    [翻译]The Layout System 布局 In Sencha Touch there are two basic building blocks: componentsand containe ...

  10. Best Practices for Web Apps

    Mobile Web Best Practices Exceptional Performance Let's make the web faster