Divisible by Seven
来源:互联网 发布:python 数据库框架 编辑:程序博客网 时间:2024/04/29 12:21
You have number a, whose decimal representation quite luckily contains digits 1, 6, 8, 9. Rearrange the digits in its decimal representation so that the resulting number will be divisible by 7.
Number a doesn't contain any leading zeroes and contains digits 1, 6, 8, 9 (it also can contain another digits). The resulting number also mustn't contain any leading zeroes.
The first line contains positive integer a in the decimal record. It is guaranteed that the record of numbera contains digits: 1, 6, 8, 9. Numbera doesn't contain any leading zeroes. The decimal representation of numbera contains at least4 and at most 106 characters.
Print a number in the decimal notation without leading zeroes — the result of the permutation.
If it is impossible to rearrange the digits of the number a in the required manner, print 0.
Input
1689
Output
1869
Input
18906
Output
18690
题意:给你一个至少四位的数,里面必包含1,6,8,9可以有其他的数字,通过调整问你能把它变成整除7的数并输出来,并且不带前导0。
思路:里面除了1,6,8,9这一个组合,之外的的数,算他们除7之后的余数假设为haha,要整除7肯定是他们除7之后的余数haha+(1,6,8,9)这个组合除7之后的余数==7才能整除7,用7减去haha就是(1,6,8,9)这个组合要除以7得到的余数,所以我们就要找这个组合怎么组合才能得到这个余数,找到后先把除了(1,6,8,9)这个组合之外的数输出来,然后再输出(1,6,8,9)这个组合,最后把0输出来,就ok了
#include<stdio.h>#include<iostream>#include<string.h>using namespace std;int vis[10010000];int main(){ char a[1001000]; scanf("%s",a); memset(vis,0,sizeof(vis)); int mod=0; int l=strlen(a); int p=0; int w=0,e=0,r=0,t=0;//用来标记1,6,8,9这个组合 for(int i=0;i<l;i++) { int we=(a[i]-'0'); if(we==0) { p++; vis[i]=1; continue; } if(we==1&&!w) { w=1; vis[i]=1; continue; } if(we==6&&!e) { vis[i]=1; e=1; continue; } if(we==8&&!r) { r=1; vis[i]=1;; continue; } if(we==9&&!t) { vis[i]=1; t=1; continue; } else mod=(mod*10+we)%7; } mod=(mod*10000)%7; for(int i=0;i<l;i++) { if(!vis[i])//把(1,6,8,9)这一个组合之外的数输出来 { printf("%c",a[i]); } } if(mod==0)//找适合剩余余数的(1,6,8,9)这个组合 { printf("1869"); } else if(mod+1==7) { printf("1968"); } else if(mod+2==7) { printf("1689"); } else if(mod+3==7) { printf("6198"); } else if(mod+4==7) { printf("1698"); } else if(mod+5==7) { printf("1986"); } else if(mod+6==7) { printf("1896"); } while(p--)输出0 { printf("0"); }}
- Divisible by Seven
- C. Divisible by Seven
- Divisible by Seven CodeForces
- CodeForces 374A. Divisible by Seven
- CodeForces 375A Divisible By Seven
- codeforce 221 Div2 C - Divisible by Seven
- CF-375A Divisible by Seven
- Code forces 376 C Divisible by Seven
- CodeForces 375A Divisible by Seven
- CodeForces 375A Divisible by Seven
- Codeforces Round #221 (Div. 2)-C. Divisible by Seven
- A. Divisible by Seven----打表暴力/数学思维
- CF 376C Divisible By Seven 同余+暴力
- codeforces 376C Divisible by Seven(能被7整除的数)
- Codeforces Round #221 (Div. 2) B. I.O.U. C. Divisible by Seven D. Maximum Submatrix 2 解题报告
- 问题 B: Divisible by 3
- 问题 B: Divisible by 3 规律题
- seven
- 香港青少年香港机场快闪演出,全程高燃!(指挥高德仪很棒)
- 【转】 数据库之mac上mysql root密码忘记或权限错误的解决办法
- CentOS 7 安装 Storm 0.9.3
- javascript挑战编程技能-第一题:将问题转变成代码
- C++从.c到.out
- Divisible by Seven
- 网易MySQL数据库工程师微专业学习笔记(十)
- Xcode启动失败,提示"Loading a plug-in failed The plug-in or one of its prerequisite plug-ins may be missin
- java提高篇(十三)-----字符串
- LeetCode3 Search a 2D Matrix
- idea
- java使用redis
- Python3.6 zip lambda
- (蓝桥杯第六届B组)加法变乘法