C. Divisible by Seven
来源:互联网 发布:js打断点 编辑:程序博客网 时间:2024/04/29 06:12
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. Number a doesn't contain any leading zeroes. The decimal representation of numbera contains at least 4 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 numbera in the required manner, print 0.
1689
1869
18906
18690
题解:题目中告诉我们给出的字符串必须包含1 6 8 9这四个数字,我们对这四个数字进行排列组合不难发现,其排列结果对7取余的结果包含0,1,2,3,4,5,6。要想字符串可以整除7,只需把字符串分成1 6 8 9 和剩下的两部分并且让他们的结果可以对7整除。分为两种情况,一种是除了1 6 8 9之外都是零,这样只需把1 6 8 9 放在前面即可;第二种为除了1 6 8 9 外,字符串还有其他非零数字,这种用剩下的先对7取余,然后如果不能整除再用1 6 8 9 补齐即可……
#include<stdio.h>#include<iostream>#include<algorithm>#include<string.h>#include<stdlib.h>#include<time.h>#include<math.h>#include<queue>;#include<stack>;using namespace std;char f[8][5]={"1869","1968","1689","6198","1698","1986","1896"}; //1689 对7取余结果分别为0 1 2 3 4 5 6的几种排列char x[1000005]; // 储存字符串int y[1000005],v[10]; // V数组标记1 6 8 9;int main(){ scanf("%s",x); memset(v,0,sizeof(v)); int len=strlen(x); for(int i=0;i<strlen(x);i++) { y[i]=x[i]-'0'; if(y[i]==1&&!v[1]) { y[i]=0;v[1]=1; } if(y[i]==6&&!v[6]) { y[i]=0;v[6]=1; } if(y[i]==8&&!v[8]) { y[i]=0;v[8]=1; } if(y[i]==9&&!v[9]) { y[i]=0;v[9]=1; } } sort(y,y+strlen(x)); int ant=0; for(int i=len-1;i>=0;i--) { ant=ant*10+y[i]; ant%=7; } if(y[len-1]==0) // 情况一 { printf("%s",f[(7-ant)%7]); for(int i=0;i<len-4;i++) printf("0"); printf("\n"); } else // 情况二 { for(int i=len-1;i>=4;i--) printf("%d",y[i]); printf("%s\n",f[(7-ant)%7]); }}
- C. Divisible by Seven
- codeforce 221 Div2 C - Divisible by Seven
- Code forces 376 C Divisible by Seven
- Divisible by Seven
- Divisible by Seven CodeForces
- Codeforces Round #221 (Div. 2)-C. Divisible by Seven
- CF 376C Divisible By Seven 同余+暴力
- CodeForces 374A. Divisible by Seven
- CodeForces 375A Divisible By Seven
- CF-375A Divisible by Seven
- CodeForces 375A Divisible by Seven
- CodeForces 375A Divisible by Seven
- codeforces 376C Divisible by Seven(能被7整除的数)
- A. Divisible by Seven----打表暴力/数学思维
- 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
- C语言学习笔记二
- 三步完美完成wordpress外链跳转
- Qqbet88 Malaysia Casino Deposit Bonus Up To Max 7%(deposit bonus, qqbet88, Qqbet88 Malaysia Casino,
- Spring面试问答Top 25
- Unity官方示例教程《Stealth》学习笔记
- C. Divisible by Seven
- Unity学习笔记
- TSP问题(状压DP求解)
- Python Collections库使用
- 去除html文本中所有html标签的看法
- 雄关漫道真如铁,而今迈步从头越
- Weex(一)- 环境创建与第一个小程序
- jdbc 连接池 and 数据源 笔记
- VS2013/VS2015搭建wxWidgets开发环境