B-幸运数字转换
题意:输入:n(字符串长度)、k(转换次数)、以及一个字符串d,要求当d[x]=='4' && d[x+1]=='7’时 x==奇数 那么d[x]=d[x+1]='4' 否则 d[x]=d[x+1]='7'
n[1,1e5] k[1,1e9]
思路:直接模拟判断d[x]和d[x+1]的状况,但是要留意 447和477这两种会反复折腾的情况
①、
#include <iostream>
using namespace std;
int main()
{
char a[100005];
int n,k;
cin>>n>>k;
scanf("%s",a+1);
for(int i=1;i<n && k;i++)
{
if(a[i]=='4' && a[i+1]=='7')
{
k--;
if(i%2==1)
{
a[i+1]='4';
if(a[i+2]=='7')//447
{
if(k%2)
a[i+1]='7';
break;
}
}
else
{
a[i]='7';
if(a[i-1]=='4')//477
{
if(k%2)//偶数次保持原样 奇数次i会再变化一次
a[i]='4';
break;
}
}
}
}
printf("%s\n",a+1);
system("pause");
return 0;
}
//观察后发现奇数位出现447或477后就会循环这两个状态,按这个模拟就行了
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
#include<iostream>
using namespace std;
int a[1001000];
char str[1000100];
int main()
{
int n,m;
scanf("%d%d",&n,&m);
scanf("%s",str+1);
int p=-1;
for(int i=1;i<=n-2;i++)
{
if(i%2==1&&str[i]=='4'&&(str[i+1]=='4'||str[i+1]=='7')&&str[i+2]=='7')
{
p=i;
break;
}
}
if(p==-1)
{
for(int i=1;i<=n-1&&m;i++)
{
if(str[i]=='4'&&str[i+1]=='7')
{
if(i&1)str[i+1]='4';
else str[i]='7';
m--;
}
}
printf("%s\n",str+1);
}
else
{
for(int i=1;i<=p-2&&m;i++)
{
if(str[i]=='4'&&str[i+1]=='7')
{
if(i&1)str[i+1]='4';
else str[i]='7';
m--;
}
}
m%=2;
if(m)str[p+1]=(str[p+1]=='4'?'7':'4');
printf("%s\n",str+1);
}
return 0;
}
H-数数字
题意:输入a.b.d.n 意思是有n个a 就是aaaaaaaaaaa这样有n个 然后再乘一个b 问这个积中有几个d
(1≤a,b≤9,0≤d≤9,1≤n≤10^9)
思路:初看是大数乘法,再看发现比大数简单的很多。因为无论怎么乘中间一定有一大段是重复的。我们只要考虑怎么判断重复就可以了。
那么,现在从a*b开始判断 要么比10小不用进位,如果a*b正好等于d那么n个a*b就会有n个d,如果要进位,那么就保存进位再加到下一个a*b中去,剩下的余数就是积中的一位。
#include<iostream>
#include<string>
#include<sstream>
using namespace std;
int main()
{
int t;
cin >> t;
while (t--)
{
int a, b, d, n;
cin >> a >> b >> d >> n;
if (a*b < 10)
{
if (a*b == d)
cout << n << endl;
else
cout << 0 << endl;
}
else
{
int c[10] = { 0 };
int jw = a * b / 10;
int res = a * b % 10;
c[res]++;
n--;
while (n--)
{
res = (a*b + jw) % 10;
jw = (a*b + jw) / 10;
if (c[res])//如果开始重复,就会一直重复,然后可能第一位是不同于重复的数
{
c[res] += n+1;
break;
}
else
c[res]++;
}
if (jw)//如果进位不为0就证明积有n+1位那么就需要把第一位也要加进去
c[jw]++;
cout << c[d] << endl;
}
}
return 0;
}