L1-1 birth

L1-1. Birth

time limit
400 ms
Memory Limit
65536 kB
Code size limit
8000 B
Sentenced program title
Standard
Author
Chen Yue

The above is a wonderful Sina microblogging posted: "I was born in 1988, until the age of 25 did not encounter the same four digits of the year." That is, until 2013 to reach "4 digits are not the same." requirements. This question you upon request, automatically populate "I was born in y years, until the age of x n numbers do not encounter the same year" this sentence.

Input formats:

The number n of different digital input is given in the year of birth year in the target row and y, wherein y is between [1, 3000], n may be 2, or 3, or 4. Note to less than 4 years of zero padding in front, for example, the year 1 is considered to be 0001, there were two different digits 0 and 1.

Output formats:

Input, output x and year can be achieved in accordance with the requirements. Between numbers separated by a space, the line from beginning to end may not have the extra space. Year Yaoan four outputs. Note: The so-called "n numbers are not the same" means different number happens to be the n. The "2013" is considered to meet the "four-digit number is different" conditions, and are not considered two or three digits different conditions.

Sample Input 1:
1988 4
Output Sample 1:
25 2013
Sample Input 2:
1 2
Output Sample 2:
0 0001
 
  
 
  
#include<bits/stdc++.h>
using namespace std;
int main() {
	int year,n,first,sum,num[4]={0},all[10]={0};

	cin>>year>>n;
    first =year;
while(1){
			
    num[0] =year/1000; //分别将个十百千位上的数保存在num[]数组中
    num[1] =year/100%10;
    num[2] =year/10%10;
    num[3] =year%10;

    for(int i=0;i<4;i++){    //给all赋值,例如 0001 的数字,all只有 all[0] 和 all[1] 是++,结果为1.
        all[num[i]]++;
    }
    for(int i=0;i<10;i++){
        if(all[i]>=1)        //例如 0001 数字,因为两个不一样的数,所以两个为 1 的all值,所以sum为2;同理 0012 则 sum 为 3;
            sum++;
    }
    if(sum==n)              //比较,如果 n==sum ,即满足题目中给的,有 n 个不同的数字时,结束循环。
        break;
    for(int i=0;i<10;i++){    //如果不满足要求,则将all 数组的值滞空。 重新计算。
        all[i] =0;
    }
    year++;               //将 year 累加
    sum=0;                //将 sum 置 0;
}
	printf("%d,%04d",year-first,year);   输出结果, %04d 表示,一共有四位,不足时补 0 ;
	return 0;
}


Guess you like

Origin blog.csdn.net/qq_34851243/article/details/73123999