欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

《算法笔记》Day 1

程序员文章站 2022-07-08 16:43:39
...

1022 D进制的A+B (20分)

输入两个非负 10 进制整数 A 和 B (≤2​30​​−1),输出 A+B 的 D (1<D≤10)进制数。

输入格式:
输入在一行中依次给出 3 个整数 A、B 和 D。

输出格式:
输出 A+B 的 D 进制数。

输入样例:

123 456 8

输出样例:

1103

Code:

#include<stdio.h>
#include<string.h>
int main(void){
    long long A,B,D;
    scanf("%lld %lld %lld",&A,&B,&D);
    A = A + B;
    long long a[30];
    memset(a,0,sizeof(a));
    int n = 0;
    do{
        a[n++] = A%D;
        A /= D;
    }while(A!=0);
    int i;
    for(i=n-1;i>=0;i--){
        printf("%d",a[i]);
    }
    return 0;
}

《算法笔记》Day 1
1009 说反话 (20分)

给定一句英语,要求你编写程序,将句中所有单词的顺序颠倒输出。

输入格式:
测试输入包含一个测试用例,在一行内给出总长度不超过 80 的字符串。字符串由若干单词和若干空格组成,其中单词是由英文字母(大小写有区分)组成的字符串,单词之间用 1 个空格分开,输入保证句子末尾没有多余的空格。

输出格式:
每个测试用例的输出占一行,输出倒序后的句子。

输入样例:

Hello World Here I Come

输出样例:

Come I Here World Hello

Code:

#include<stdio.h>
#include<string.h>
int main(void){
    char str[81];
    char a[81][81];
    gets(str);
    int i,j = 0,k = 0;
    for(i=0;i<strlen(str);i++){
        if(str[i] != ' '){
            a[j][k++] = str[i];
        }
        else{
            k = 0;
            j++;
        }
    }
    for(i=j;i>0;i--){
        printf("%s ",a[i]);
    }
    printf("%s",a[0]);
    return 0;
}

《算法笔记》Day 1
1025 PAT Ranking (25分)

Programming Ability Test (PAT) is organized by the College of Computer Science and Technology of Zhejiang University. Each test is supposed to run simultaneously in several places, and the ranklists will be merged immediately after the test. Now it is your job to write a program to correctly merge all the ranklists and generate the final rank.

Input Specification:
Each input file contains one test case. For each case, the first line contains a positive number N (≤100), the number of test locations. Then N ranklists follow, each starts with a line containing a positive integer K (≤300), the number of testees, and then K lines containing the registration number (a 13-digit number) and the total score of each testee. All the numbers in a line are separated by a space.

Output Specification:
For each test case, first print in one line the total number of testees. Then print the final ranklist in the following format:

registration_number final_rank location_number local_rank

The locations are numbered from 1 to N. The output must be sorted in nondecreasing order of the final ranks. The testees with the same score must have the same rank, and the output must be sorted in nondecreasing order of their registration numbers.

Sample Input:

2
5
1234567890001 95
1234567890005 100
1234567890003 95
1234567890002 77
1234567890004 85
4
1234567890013 65
1234567890011 25
1234567890014 100
1234567890012 85

Sample Output:

9
1234567890005 1 1 1
1234567890014 1 2 1
1234567890001 3 1 2
1234567890003 3 1 2
1234567890004 5 1 4
1234567890012 5 2 2
1234567890002 7 1 5
1234567890013 8 2 3
1234567890011 9 2 4

Code:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct Student{
    char name[13];
    int score;
    int rank;
    int location;
    int local_rank;
}student[30200];

bool cmp(Student a,Student b){
    if(a.score != b.score){
        return a.score > b.score;
    }
    else return strcmp(a.name,b.name) < 0;
}
int main(void){
    int i,j;
    int kao;
    int sum = 0;
    scanf("%d",&kao);
    for(i=0;i<kao;i++){
        int num;
        scanf("%d",&num);
        for(j=0;j<num;j++){
            scanf("%s %d",&student[sum].name,&student[sum].score);
            student[sum].location = i+1;
            sum++;
        }
        sort(student + sum - num,student + sum,cmp);
        student[sum-num].local_rank = 1;
        for(j=sum - num + 1;j<sum;j++){
            if(student[j].score == student[j-1].score){
                student[j].local_rank = student[j-1].local_rank;
            }
            else{
                student[j].local_rank = j + 1 - (sum - num);
            }
        }
    }
    printf("%d\n",sum);
    sort(student,student + sum,cmp);
    int temp = 1;
    for(i=0;i<sum;i++){
        printf("%s ",student[i].name);
        if((i!=0) && (student[i].score != student[i-1].score)){
            temp = i+1;
        }
        printf("%d %d %d\n",temp,student[i].location,student[i].local_rank);
    }
    return 0;
}

《算法笔记》Day 1

相关标签: 算法笔记