关于一些初级ACM竞赛题目的分析和题解(八)

关于一些初级ACM竞赛题目的分析和题解(八) 
下面两道题只是表面上复杂些而已,其实 比较简单,上题:
A. Beautiful Year
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

It seems like the year of 2013 came only yesterday. Do you know a curious fact? The year of 2013 is the first year after the old 1987 with only distinct digits.

Now you are suggested to solve the following problem: given a year number, find the minimum year number which is strictly larger than the given one and has only distinct digits.

Input

The single line contains integer y (1000 ≤ y ≤ 9000) — the year number.

Output

Print a single integer — the minimum year number that is strictly larger than y and all it's digits are distinct. It is guaranteed that the answer exists.

Examples
input
1987
output
2013
input
2013
output
2014

输入n(n大于等于1000,小于等于9000,且n的个十百千位各不相同),找出比n大的并个十百千位也不同的数;
下面是代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
    int a,b,c,d,e,n;
    scanf("%d",&n);
    while (n<=10000)
    {
n++;
        a=n%10;b=n%100/10;c=n/1000;d=n%1000/100;  // 确定个十百千位
        if (a!=b&&a!=c&&a!=d&&b!=c&&b!=d&&c!=d)  //  判断各位不同
        {  
        return 0*printf("%d",n);}

    }
}
重点是怎么找出个十百千的数,找到就好了,

A. Nearly Lucky Number
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal representations contain only the lucky digits 4 and 7. For example, numbers 477444 are lucky and 517467 are not.

Unfortunately, not all numbers are lucky. Petya calls a number nearly lucky if the number of lucky digits in it is a lucky number. He wonders whether number n is a nearly lucky number.

Input

The only line contains an integer n (1 ≤ n ≤ 1018).

Please do not use the %lld specificator to read or write 64-bit numbers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.

Output

Print on the single line "YES" if n is a nearly lucky number. Otherwise, print "NO" (without the quotes).

Examples
input
40047
output
NO
input
7747774
output
YES
input
1000000000000000000
output
NO
Note

In the first sample there are 3 lucky digits (first one and last two), so the answer is "NO".

In the second sample there are 7 lucky digits, 7 is lucky number, so the answer is "YES".

In the third sample there are no lucky digits, so the answer is "NO".


关于幸运数,输入一行数,若他的各位数含4,7的数为4和7则为几乎幸运的数,判断是否为几乎幸运的数,是的话输出YES,否的话输出NO,下面是代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
    int l,b=0;
    char a[19999];
    gets(a);
    l=strlen(a);
    for (int i=0;i<l;i++)
        if(a[i]=='4'||a[i]=='7')
            b++;   // 计数
    printf((b==4||b==7)?"YES":"NO");

}
这两道题都比较简单,多练A题也能学到c++语言的一些语法。希望自己能越学越好 奋斗


















猜你喜欢

转载自blog.csdn.net/monster_ayb/article/details/79229944