A. Divisible by Seven----打表暴力/数学思维
2017-08-23 15:01
288 查看
A. Divisible by Seven
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
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.
Input
The first line contains positive integer a in the decimal record. It is guaranteed that the record of number a contains
digits: 1, 6, 8, 9. Number a doesn't contain any leading zeroes. The decimal representation of number a contains
at least 4 and at most 106 characters.
Output
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.
Examples
input
output
input
output
题目链接:http://codeforces.com/contest/375/problem/A
题目的意思是说给你一个字符串,里面一定包括1,6,8,9这四个数,让你任意组合这个字符串,使它成为7的倍数。
题目既然这样说了,那么肯定就和这四个数有关系,除以7,余数只能是0~6,我们针对每一种情况,暴力出来一个答案,其余的把0放在最后面,剩下的直接输出。
代码:
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
char s[1000010];
int a[10];
int main(){
int len,k;
scanf("%s",&s);
len=strlen(s);
k=0;
for(int i=0;i<len;i++){
if(s[i]=='0')
a[0]++;
else if(s[i]=='1'&&!a[1]){
a[1]=1;
}
else if(s[i]=='6'&&!a[6]){
a[6]=1;
}
else if(s[i]=='8'&&!a[8]){
a[8]=1;
}
else if(s[i]=='9'&&!a[9]){
a[9]=1;
}
else{
printf("%c",s[i]);
k=k*10+s[i]-'0';
k%=7;
}
}
if(k==0){
printf("1869");
}
else if(k==1){
printf("6198");
}
else if(k==2){
printf("1896");
}
else if(k==3){
printf("6918");
}
else if(k==4){
printf("1986");
}
else if(k==5){
printf("1968");
}
else if(k==6){
printf("1698");
}
//cout<<"-----"<<endl;
while(a[0]){
printf("0");
a[0]--;
}
cout<<endl;
return 0;
}
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
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.
Input
The first line contains positive integer a in the decimal record. It is guaranteed that the record of number a contains
digits: 1, 6, 8, 9. Number a doesn't contain any leading zeroes. The decimal representation of number a contains
at least 4 and at most 106 characters.
Output
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.
Examples
input
1689
output
1869
input
18906
output
18690
题目链接:http://codeforces.com/contest/375/problem/A
题目的意思是说给你一个字符串,里面一定包括1,6,8,9这四个数,让你任意组合这个字符串,使它成为7的倍数。
题目既然这样说了,那么肯定就和这四个数有关系,除以7,余数只能是0~6,我们针对每一种情况,暴力出来一个答案,其余的把0放在最后面,剩下的直接输出。
代码:
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
char s[1000010];
int a[10];
int main(){
int len,k;
scanf("%s",&s);
len=strlen(s);
k=0;
for(int i=0;i<len;i++){
if(s[i]=='0')
a[0]++;
else if(s[i]=='1'&&!a[1]){
a[1]=1;
}
else if(s[i]=='6'&&!a[6]){
a[6]=1;
}
else if(s[i]=='8'&&!a[8]){
a[8]=1;
}
else if(s[i]=='9'&&!a[9]){
a[9]=1;
}
else{
printf("%c",s[i]);
k=k*10+s[i]-'0';
k%=7;
}
}
if(k==0){
printf("1869");
}
else if(k==1){
printf("6198");
}
else if(k==2){
printf("1896");
}
else if(k==3){
printf("6918");
}
else if(k==4){
printf("1986");
}
else if(k==5){
printf("1968");
}
else if(k==6){
printf("1698");
}
//cout<<"-----"<<endl;
while(a[0]){
printf("0");
a[0]--;
}
cout<<endl;
return 0;
}
相关文章推荐
- 数学 | 推理 | 思维 | 打表 | 找规律 | 贪心 | 暴力
- Codeforces Round #304 (Div. 2) D 思维/数学/质因子/打表/前缀和/记忆化
- 【数学思维 && ax + by = n 给你n让你求有多少对(a,b) a<b 满足方程其中x,y是正整数】HackerRank Satisfactory Pairs
- cf 376C - Divisible by Seven
- Codeforces Round #336 (Div. 2)【A.思维,暴力,B.字符串,暴搜,前缀和,C.暴力,D,区间dp,E,字符串,数学】
- Divisible by Seven CodeForces - 376C (数论)
- C. Divisible by Seven
- CodeForces - 376C~Divisible by Seven(思路)
- 数学/找规律/暴力 Codeforces Round #306 (Div. 2) C. Divisibility by Eight
- Divisible by Seven
- Codeforces 550C Divisibility by Eight【数学思维题】好题!
- Codeforces Round #221 (Div. 2) C. Divisible by Seven(构造 大数除法 )
- Code forces 376 C Divisible by Seven
- cf C. Divisible by Seven
- CF#306-C - Divisibility by Eight-数学+暴力
- Codeforces Round #221 (Div. 2)-C. Divisible by Seven
- codeforce 221 Div2 C - Divisible by Seven
- hdu 5194 组合数学or暴力
- 玲珑杯 A 枚举 思维 数学
- 【CodeForces - 849】A【思维】 B【暴力】C【思维+构造】