51nod 1770数数字(找规律)
2017-10-09 13:57
253 查看
基准时间限制:1 秒 空间限制:262144 KB 分值: 20 难度:3级算法题 收藏 关注
统计一下 aaa ⋯ aaa n个a × b 的结果里面有多少个数字d,a,b,d均为一位数。
样例解释:
3333333333*3=9999999999,里面有10个9。
Input
多组测试数据。
第一行有一个整数T,表示测试数据的数目。(1≤T≤5000)
接下来有T行,每一行表示一组测试数据,有4个整数a,b,d,n。 (1≤a,b≤9,0≤d≤9,1≤n≤10^9)
Output
对于每一组数据,输出一个整数占一行,表示答案。
Input示例
2
3 3 9 10
3 3 0 10
Output示例
10
0
题解:大体上分三种情况。
第一:n=1的时候,比较特殊需要单独讨论
第二:a*b<10的时候,此时不涉及进位
第三:a*b>=10的时候,此时涉及进位,进一步可以分为一次进位及二次进位。比如5*3与7*7情况不同
代码:
#include<iostream>
#include<fstream>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<stdio.h>
#include<utility>
#include<algorithm>
#include<map>
#include<stack>
#include<set>
#include<queue>
using namespace std;
typedef long long ll;
const int maxn = 1000;
const int mod = 1e9+7;
const int INF = 1<<30;
const ll llINF = 1e18+999;
int a, b, d, n, T, arr[10];
int main( )
{
//freopen("input.txt", "r", stdin);
scanf("%d", &T);
int counter = 0;
while(T--)
{
counter++;
memset(arr, 0, sizeof(arr));
scanf("%d%d%d%d", &a, &b, &d, &n);
int one = a*b%10, ten = a*b/10;
if(n == 1)
{
arr[one]++;
if(ten>0)
arr[ten]++;
}
else if(ten == 0) //a*b没有超过10
arr[one] = n;
else
{
arr[one]++; //个位的数字是不受影响的
if(one+ten < 10)
{
arr[ten]++;
arr[one+ten] += n-1;
}
else //出现了二次进位情况
{
int k = (one+ten)%10;
arr[ten+1]++;
arr[k]++;
arr[k+1] += n-2;
}
}
printf("%d\n", arr[d]);
}
return 0;
}
相关文章推荐
- 51nod--1770数数字
- 51nod--1770数数字
- 51nod 1770 数数字(规律)
- ACM学习历程—51NOD 1770数数字(循环节)
- 51nod 1770 数数字 找规律,注意进位,时间复杂度O(n)
- 51nod 1387 移数字(多项式除法)
- 51Nod-1441-士兵的数字游戏
- oracle中无规律数字和汉字组合取数字|函数小结|
- 51nod 1070 Bash游戏 V4(博弈——找规律)
- 经典-输出规律的数字序列
- Java输入带空格的数字字符串,输出数字的和(找规律停止程序)
- 51nod 1009 数字1的数量(思维)
- 51NOD 1042 数字0-9的数量 数位DP
- 51nod 1031 骨牌覆盖 (找规律题_数学)
- 51nod-【1042 数字0-9的数量】
- 51nod 1042数字0-9的数量
- 51nod 1161 组合数,规律
- 51nod 1042 数字0-9的数量【数位dp】
- 51nod 1004 n^n的末尾数字
- 51nod 1004 n^n的末位数字