PTA:找密码(C语言)

在一个古堡的大门上有5行字符。其中隐藏着打开大门的密码。密码共有4位数字(0到9)。小明发现了一种找密码的方法:最后一行中的字符在第一行字符中出现的总次数是密码的第一个数字,依此类推。输入数据保证每行得到的数字在0到9之间。 请编写一个函数 decode帮助小明找出密码。

函数接口定义:
int decode(char**s);

通过参数s向函数传递5行字符串,函数返回密码值。

裁判测试程序样例:
#include <stdio.h>
#include <stdlib.h>
#define N 100
#define M 5
int decode(char**s);
int main()
{

char *s[M];
int i;
int key;
for(i=0; i<M; i++){
s[i] = (char *)malloc((N+1)*sizeof(char));
gets(s[i]);
}

key = decode(s);
printf("%04d",key);
for(i=0; i<M; i++){
free(s[i]);
}

return 0;
}
/* 您提交的代码将放置在这里 */

输入样例:
1111
222
33
41
2341

输出样例:
4322

int decode(char** s)
{
	int x1, x2, x3, x4;
	int count = 0;

	/*	i is the locate of s[]  (the line number)				*
	 *  j is the locate of s[4] (the char of dictionary line)	*
	 *  k is the locate of s[i] (the char of active line)		*/

	for (int i = 0; i < 4; i++)	
	{
		for (int j = 0; s[4][j] != '\0' && s[4][j] != '\n'; j++)
		{
			for (int k = 0; s[i][k] != '\0' && s[i][k] != '\n'; k++)
			{
				if (s[i][k] == s[4][j])
					count++;
			}
		}

		switch (i)
		{
			case 0:
            x1 = count * 1000;
            break;
			case 1:
            x2 = count * 100 ; 
            break;
			case 2: 
            x3 = count * 10  ; 
            break;
			case 3: 
            x4 = count; 
            break;
		}
		count = 0;
	}
	return x1 + x2 + x3 + x4;
}
  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值