PAT104910481047

Posted ton2018

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PAT104910481047相关的知识,希望对你有一定的参考价值。

1049

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 
 4 long long howmanyones1(int N)
 5 {
 6     int temp=N;
 7     int i=0;
 8     int dec[20];
 9     int o[10]={1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000};
10     while(temp>0)
11     {
12         dec[i++]=temp%10;
13         temp/=10;
14     }
15     long long ones=0;
16     i--;
17     while(i>0)
18     {
19         int d=dec[i];
20         if(d==1)
21             ones+=o[i-1]*i+N-o[i]+1;
22         else if(d!=0)
23             ones+=d*o[i-1]*i+o[i];
24         N-=d*o[i];
25         i--;
26     }
27     if(dec[0]>0)
28         ones++;
29     return ones;
30 }
31 
32 int main(void)
33 {
34     int N;
35     scanf("%d",&N);
36 
37     printf("%ld",howmanyones1(N));
38 
39     return 0;
40 }

1048

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 using namespace std;
 4 
 5 int compare(const void *data1,const void *data2)
 6 {
 7     int *ptr1=(int*)data1;
 8     int *ptr2=(int*)data2;
 9     return ptr1[0]>=ptr2[0];
10 }
11 int search(int key,int *v, int n)
12 {
13     int left=0,right=n;
14     while(right>left)
15     {
16         int mid=(right+left)/2;
17         int key1=v[mid];
18         if(key1<key)
19             left=mid+1;
20         else if(key1>key)
21             right=mid;
22         else
23             return key1;
24     }
25     return v[left];
26 }
27 int main(void)
28 {
29     int N,M;
30     scanf("%d %d",&N,&M);
31     int *v=(int*)malloc(sizeof(int)*N);
32     int i;
33     for(i=0;i<N;i++)
34         scanf("%d",v+i);
35     qsort(v,N,sizeof(int),compare);
36 
37     for(i=0;i<N-1;i++)
38     {
39         int t=M-v[i];
40         int t1=search(t,v+i+1,N-i-1);
41         if(t1==t)
42         {
43             printf("%d %d",v[i],t1);
44             return 0;
45         }
46     }
47     printf("No Solution");
48     return 0;
49 }

1047

技术分享图片
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <vector>
 5 using namespace std;
 6 
 7 int N, K;
 8 struct people{
 9     char name[5];
10     int id;    
11 }str[40001];
12 
13 int cnt[2501];
14 vector<int> buf[2501];
15 char bucket[40001];
16 
17 bool mycmp(const struct people &a, const struct people &b) {
18     return strcmp(b.name, a.name)>0;
19 }
20 
21 int main(int argc, char * argv[]) {
22     scanf("%d %d", &N, &K);
23     int tmp;
24     int k;
25     for (int i=1; i<=N; ++i) {
26         str[i].id = i;
27         scanf("%s %d", str[i].name, &tmp);
28         for (int j=0; j<tmp; ++j) {
29             scanf("%d", &k);    
30             ++cnt[k];
31             buf[k].push_back(i);
32         }
33     }
34     sort(str+1, str+1+N, mycmp);
35     vector<int>::iterator itr;
36     for (int i=1; i<=K; ++i) {
37         printf("%d %d\n", i, cnt[i]);
38         
39         for (itr = buf[i].begin(); itr != buf[i].end(); ++itr) {
40             bucket[*itr] = 1;
41         }
42         for (int j=1; j<=N; ++j) {
43             if (bucket[str[j].id]) {
44                 printf("%s\n", str[j].name);
45             }    
46         }
47         for (itr = buf[i].begin(); itr != buf[i].end(); ++itr) {
48             bucket[*itr] = 0;
49         }
50     }
51     return 0;
52 }
pat1047

 

以上是关于PAT104910481047的主要内容,如果未能解决你的问题,请参考以下文章

PAT-乙级-1049 数列的片段和

PAT Basic 1049

PAT 数列的片段和简单数论

PAT乙级1049 数列的片段和 (20 分)

PAT 乙级 1049 数列的片段和

PAT1049-----枚举法,找规律题,注意降低时间复杂度