Archive for the ‘Code forces’ Category

Codeforces Beta Round #84 (Div. 2 Only)   Leave a comment


A – Nearly Lucky Number

import java.util.Scanner;

public class NearlyLucky {
	public static boolean isLucky(int n) {
		String s = n + "";
		for (int i = 0; i < s.length(); i++)
			if (s.charAt(i) != '4' && s.charAt(i) != '7')
				return false;
		return true;
	}

	public static boolean isNearlyLucky(long n) {
		String s = n + "";
		int count = 0;
		for (int i = 0; i < s.length(); i++)
			if (s.charAt(i) == '4' || s.charAt(i) == '7')
				count++;
		return isLucky(count);
	}

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		long n = in.nextLong();
		if (isNearlyLucky(n))
			System.out.println("YES");
		else
			System.out.println("NO");
	}
}
B. Lucky String
#include <stdio.h>
#include <stdlib.h>
char* s;
int n;
int last[26];
int main()
{
	int i,j;
	scanf("%d",&n);
	s = malloc(n+1);
	for(i=0;i<=n;i++)
		s[i]=0;
	for(i = 0;i<26;i++)
		last[i]=-1;
	for(i=0;i<n;i++)
	{
		for(j=0;j<26;j++)
			if(last[j]==-1)
			{
				s[i]=j+'a';
				last[j]=i;
				break;
			}
			else if(last[j]+4==i||last[j]+7==i)
			{
				s[i]=j+'a';
				last[j]=i;
				break;
			}
	}
	s[n]=0;
	puts(s);
	return 0;
}
C. Lucky Sum of Digits
#include <stdio.h>
int main()
{
	int n,i,j;
	char found = 0;
	scanf("%d",&n);
	for(i=0;i<=n/4;i++)
	{
		if((n-i*4)%7==0)
		{
			found = 1;
			for(j=0;j<i;j++)
				putchar('4');
			for(j=0;j<(n-i*4)/7;j++)
				putchar('7');
			break;
		}
	}
	if(!found)
		puts("-1");
	return 0;
}
Advertisements

Posted August 29, 2011 by epicrado in Code forces