uva 10642 Super Number(dfs)

737 篇文章 0 订阅
232 篇文章 0 订阅

Super Number
Input: 
Standard Input

Output: Standard Output

Time Limit: 3 Seconds

 

Don't you think 162456723 very special? Look at the picture below if you are unable to find its speciality. (a | b means ‘b is divisible by a’)

Figure: Super Numbers

 

Given n, m (0 < n < m < 30), you are to find a m-digit positive integer X such that for every i (n <= i <= m), the first i digits of X is a multiple of i. If more than one such X exists, you should output the lexicographically smallest one. Note that the first digit of Xshould not be 0.

 

Input 

The first line of the input contains a single integer t(1 <= t <= 15), the number of test cases followed. For each case, two integers nand m are separated by a single space.

 

Output 

For each test case, print the case number and X. If no such number, print -1.

 

Sample Input                           Output for Sample Input

2

1 10

3 29

Case 1: 1020005640

Case 2: -1

 


题目大意:给出a、b,找到一个最小的数, 要求满足从左到右第a位开始到b位为止,前i位的数可以整出i。

解题思路:用递归遍历出每个位数上的数字,如果当前位数大于a,就对前面到现在的数字进行判断。

#include <stdio.h>
#include <string.h>

const int N = 50;
int begin, endin, flag;
int num[N];

bool judge(int cur) {
    int sum = 0;
    for (int i = 1; i <= cur; i++) {
	sum = sum * 10 + num[i];
	sum = sum % cur;
    }
    return sum?false:true;
}    

void dfs(int cur) {
    if (cur > endin) {
	flag = 1;
	return ;
    }

    for (num[cur] = 0; num[cur] < 10; num[cur]++) {
	if (cur >= begin) {
	    if (judge(cur))
		dfs(cur + 1);
	}
	else
	    dfs(cur + 1);
	if  (flag)  return;
    }
}

int main() {
    int cas, t = 1;
    scanf("%d", &cas);
    while (cas--) {
	// Init;
	memset(num, 0, sizeof(num));
	flag = 0;
	scanf("%d%d", &begin, &endin);

	int cur = 1;
	for (num[cur] = 1; num[cur] < 10; num[cur]++) {
	    dfs(cur + 1);
	    if (flag)
		break;
	}

	printf("Case %d: ", t++);
	if (flag) {
	    for (int i = 1; i <= endin; i++)
		printf("%d", num[i]);
	    printf("\n");
	}
	else
	    printf("-1\n");
    }
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值