简单模拟答案

发布于 2020-02-24  268 次阅读


害死人不偿命的(3n+1)猜想(卡拉兹幻想)

#include<cstdio>
int divide(int n){
    //while循环不能定义在divide
    if(n % 2 == 0){
        return  n / 2;
    }else{
        return  ((3 * n) + 1)/2;
    }
}
int main(){
    int a,i;
    i = 0;
    scanf("%d",&a);
    while(a != 1){
        a = divide(a);
        i++;
    }
    printf("%d\n",i);
    return 0;
}

挖掘机技术哪家强

#include<cstdio>
int N = 0;
//这里的N会不会在main函数里面再创建一个副本,如果其他函数要用是否需要传出来?
int num;
int scr;
int sum[10];
int MAX_num = -1;
int MAX_sum = -1;
void plus(int N){
    for(int i = 0; i < N; i++){
        scanf("%d %d",&num,&scr);
        sum[num] += scr;
    }
    for(int i = 0;i < N; i++){
        if(sum[i]>MAX_sum){
            MAX_sum = sum[i];
            MAX_num = i;
        }
    }
    printf("%d %d",MAX_num,MAX_sum);
}
int main(){
    scanf("%d",&N);
    plus(N);
    return 0;
}

A+B和C

#include<cstdio>
typedef long long LL;
LL a = 0,b = 0,c = 0;
int T;
void func(){
    scanf("%d",&T);
    while(T>0&&T<=10){
        for(int i = 0; i < T; i++){
            scanf("%d%d%d",&a,&b,&c);
            if(a+b>c) printf("Case #%d:true",T);
            else printf("Case #%d:false",T);
        }
    }
}
int main(){
    func();
    return 0;
}

A+B and C(64Bit)

#include<cstdio>
typedef long long int LL;
void func(){
    int T,tcase=1;
    scanf("%d",&T);
    while(T--){
        LL a ,b ,c;
        scanf("%lld%lld%lld",&a,&b,&c);
        //注意数据类型对应,这里的%lld对应long long int,而不应该是%d
        LL sum = a+b;
        //这里要设置一个sum来记录a+b的值,直接放在if会产生错误(为何?)
        bool flag=0;
        if(a<0&&b<0&&sum>=0) flag = false;
        else if(a>0&&b>0&&sum<=0) flag = true;
        else if(sum>c) flag=1;
        else flag =0;
        if(flag == 1){
            printf("Case #%d: true\n",tcase++);
        }else{
            printf("Case #%d: false\n",tcase++);
        } 
    }
}
int main(){
    func();
    return 0;
} 

部分A+B

#include<cstdio>
// 获取正整数n的(x)部分
int cal(int n, int x)
{
    int digit;
    int ret = 0;
    while (n > 0){
        digit = n % 10;
        if (digit == x){
            ret = ret*10 + digit;
        }
        n /= 10;
    }
    return ret;
}
int main(){
    int a,pa,b,pb;
    scanf("%d%d%d%d",&a,&pa,&b,&pb);
    printf("%d\n",cal(a,pa)+cal(b,pb));
    return 0;
}
#include <stdio.h>
void func(){
    int C1=0,C2=0;
    scanf("%d%d",&a,&b);
    //注意加上& 
    int sub = ((C2 - C1) + 50) / 100;
    //先给(b-a)加上50,这样如果(b-a)/100的小数位大于等于0.5则会进位,小于等于0.5则会舍去
    int hh = sub / 3600;
    sub = sub % 3600;//此时将n % 3600即为剩下的分钟和秒数
    int mm = sub / 60, ss = sub % 60;
    printf("%02d:%02d:%02d", hh, mm, ss);
    //用printf的%02d格式自动为不足2位的整数在前面补上0
}
int main() {
    func();
    return 0;
}

划拳

#include<cstdio>
void func(){
    int lose_account_jia = 0, lose_account_yi = 0;
    int head_jia = 0, head_yi = 0;
    int speak_jia = 0, speak_yi = 0;
    int n;
    scanf("%d",&n);
    while(n--){
        scanf("%d%d%d%d", &speak_jia, &head_jia, &speak_yi, &head_yi);
        if(head_jia == speak_jia+speak_yi&&head_yi!= speak_jia+speak_yi){
            lose_account_yi++;
        }else if(head_jia != speak_jia+speak_yi&&head_yi== speak_jia+speak_yi){
            lose_account_jia++;
        }
    }
    printf("%d %d\n",lose_account_jia,lose_account_yi);
}
int main(){
    func();
    return 0;
} 

数组元素循环右移问题

#include <stdio.h>
#define MAXN 100

int main() {
    int n, m, num[MAXN * 2], i, temp;
    scanf("%d %d", &n, &m);
    for (i = 0; i < n; i++) {
        scanf("%d", &num[i]);
    }
    m %= n;
    for (i = n + m - 1; i > m - 1; i--) {
        num[i] = num[i - m];
    }
    for (i = 0; i <= m - 1; i++) {
        num[i] = num[n + i];
    }
    for (i = 0; i < n; i++) {
        if (i == 0) {
            printf("%d", num[i]);
        }
        else {
            printf(" %d", num[i]);
        }
    }
    printf("\n");
    return 0;
}

本题还可以使用STL解决,即使用reverse()函数

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
    int n,m;
    cin >> n >> m;
    if(m > n)
        m -=n;
    vector<int> arr(n);
    for(int i=0; i<n; i++){
        int temp;
        cin >> temp;
        arr[i] = temp;
    }
    reverse(arr.begin(),arr.end());
    reverse(arr.begin(),arr.begin()+m);//从数组开头查m个元素转换顺序
    reverse(arr.begin()+m,arr.end());//从数组第m+1个元素到数组末尾转换顺序,arr.end()可以写成arr.begin()+n
    for(int j=0; j<n; j++){
        if(j != 0)
            cout << " ";
        cout << arr[j];
    }
    return 0;
}

数字分类

#include <stdio.h>
int main()
{
    int num;
    int A1 = 0, A2 = 0, A3 = 0, A4 = 0, A5 = 0;
    int A2flag = 0, A4count = 0;
    scanf("%d", &num);
    for(int i = 0, n; i < num; i++)
    {
        scanf("%d", &n);
        switch(n % 5)
        {
            case 0: A1 += n % 2 ? 0 : n;                                break;
            case 1: A2flag = A2flag == 1 ? -1 : 1; A2 += A2flag * n;    break;
            case 2: A3 ++;                                              break;
            case 3: A4 += n; A4count ++;                                break;
            case 4: A5 = n > A5? n : A5;                                break;
        }
    }

    if(A1 == 0)     printf("N ");   else printf("%d ", A1);
    if(A2flag == 0) printf("N ");   else printf("%d ", A2);
    if(A3 == 0)     printf("N ");   else printf("%d ", A3);
    if(A4 == 0)     printf("N ");   else printf("%.1f ", A4 * 1.0 / A4count);
    if(A5 == 0)     printf("N");    else printf("%d", A5);

    return 0;
}

这题也可以用STL解决部分问题。

#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>

using namespace std;
#define MAX_STR_LENS  4000

int main()
{
    int N = 0,i = 0, data = 0;
    int A1 = 0, A2 = 0, A3 = 0, A4outs = 0,  A5 = 0;
    double A4 = 0;
    vector<int> vecA2; //属于A2的所有数据

    cin >> N;
    for(i = 0; i < N; i++)
    {
        cin >> data;

        /* 判断是否是属于A1 */
        if (data % 5 == 0 && data % 2 == 0) A1 += data;

        /* 判断是否是属于A2 */
        if (data % 5 == 1) vecA2.push_back(data);

        /* 判断是否是属于A3 */
        if (data % 5 == 2) A3++;

        /* 判断是否是属于A4 */
        if (data % 5 == 3){
            A4 += data;
            A4outs++;
        }

        /* 判断是否是属于A5 */
        if (data % 5 == 4) A5 = max(A5,data);

    }

    /*输出 */
    if (A1 == 0) cout << "N";
    else cout << A1;
    if(vecA2.empty()) cout << " N";
    else
    {
        for(i = 0; i < vecA2.size(); i++)
        {
            A2 = A2 + pow(-1,i) * vecA2[i];
            //pow():计算-1的i次方
        }
        cout << " " << A2;
    }

    if (A3 == 0) cout << " N";
    else cout << " " << A3;

    if (A4outs == 0) cout << " N";
    else printf(" %.1f",A4/A4outs);

    if (A5 == 0) cout << " N" << endl;
    else cout << " " << A5 << endl;
    return 0;
}

锤子剪子布

C++的写法

#include <iostream>
using namespace std; 
int main() {
    int n;
    cin >> n;
    int jia_win = 0, yi_win = 0;
    int jia[3] = {0}, yi[3] = {0};
    for (int i = 1; i <= n; i++) {
        char j,y;
        cin >> j >> y; 
        if (j == 'C' && y == 'J') {
            jia_win++;
            jia[0]++;
        } else if (j == 'J' && y == 'C') {
            yi_win++;
            yi[0]++;
        } else if (j == 'J' && y == 'B') {
            jia_win++;
            jia[1]++;
        } else if (j == 'B' && y == 'J') {
            yi_win++;
            yi[1]++;
        } else if (j == 'B' && y == 'C') {
            jia_win++;
            jia[2]++;
        } else if (j == 'C' && y == 'B') {
            yi_win++;
            yi[2]++;
        }
    }
    cout << jia_win << " " << n - jia_win - yi_win << " " << yi_win << endl << yi_win << " " << n - jia_win - yi_win << " " << jia_win << endl;
    int max_jia = -1;
    for(int i = 0; i < 3; i++){
        if(jia[i] > max_jia){
            max_jia = i;
        }

    }
    if(max_jia == 0) cout<<"C";
    else if(max_jia == 1) cout<<"J";
    else if(max_jia == 2) cout<<"B";
    int max_yi = -1;
    for(int i = 0; i < 3; i++){
        if(yi[i] > max_yi){
            max_yi = i;
        }
    }
    if(max_jia == 0) cout<<" "<<"C";
    else if(max_jia == 1) cout<<" "<<"J";
    else if(max_jia == 2) cout<<" "<<"B";
    return 0;
}

C语言的写法(不知字符那里该怎么改,总是才输入5行左右就异常,下次再看)(挖坑,待解决)

#include <stdio.h>
int main() {
    int n;
    scanf("%d",&n);
    int jia_win = 0, yi_win = 0;
    int jia[3] = {0}, yi[3] = {0};
    for (int i = 0; i < n; i++) {
        char j, y;
        scanf("%c%c",&j,&y);
        if (j == 'C' && y == 'J') {
            jia_win++;
            jia[1]++;
        } else if (j == 'J' && y == 'C') {
            yi_win++;
            yi[1]++;
        } else if (j == 'J' && y == 'B') {
            jia_win++;
            jia[2]++;
        } else if (j == 'B' && y == 'J') {
            yi_win++;
            yi[2]++;
        } else if (j == 'B' && y == 'C') {
            jia_win++;
            jia[0]++;
        } else if (j == 'C' && y == 'B') {
            yi_win++;
            yi[0]++;
        }
    }
    printf("%d %d %d\n%d %d %d",jia_win, n - jia_win - yi_win, yi_win, yi_win, n - jia_win - yi_win, jia_win);
    /*
    int max_jia = -1;
    for(int i = 0; i < 3; i++){
        if(jia[i] > max_jia){
            max_jia = i;
        }

    }
    if(max_jia == 0) cout<<"C";
    else if(max_jia == 1) cout<<"J";
    else if(max_jia == 2) cout<<"B";
    int max_yi = -1;
    for(int i = 0; i < 3; i++){
        if(yi[i] > max_yi){
            max_yi = i;
        }
    }
    if(max_jia == 0) cout<<" "<<"C";
    else if(max_jia == 1) cout<<" "<<"J";
    else if(max_jia == 2) cout<<" "<<"B";
    return 0;
    */
    return 0;
}

Shuffling Machine

#include<cstdio>
const int N = 54;
char mp[5]={'S','H','C','D','J'};
int start[N+1], end[N+1], next[N+1];
int main(){
    int K;
    scanf("%d",&K);
    for(int i = 1; i <= N; i++){
        start[i] = i;
    }
    for(int i = 1; i <= N; i++){
        scanf("%d",&next[i]);
    }
    for(int step = 0; step < k; step++){
        for(int i = 1; i <= N; i++){
            end[next[i]]=start[i]; 
        }
        for(int i = 1; i <= N; i++){
            start[i]=end[i];
        }
    }
    for(int i = 1; i <= N; i++){
        if(i != 1) printf(" ");
        printf("%c%d", mp[start[i]]/13, start[i]%13+1);
    }
}

Shortest Distance

803A389BACDDA25A3840F61D22DBFCCB.png
AEF892C1681413BEB58CD7BC663A2A28.png

一元多项式求导

自己写的代码:

#include<cstdio>
int main(){
    int k,e,count=0,account=0;
    while(scanf("%d%d",&k,&e)!=EOF){
        account++;
        if(e>=1){
            k*=e;
            e--;
            count++;
        }else if(e=0){
            k=0;
            e--;
            count++;
            //注意:这里对函数的求导也不要写在while...EOF里面,因为题目给的输入样例是一行直接输入完毕的,没办法判断结束的标志(输入样例没给n)
            //注意:正是因为题目给的输入样例是一行直接输入完毕的,所以最好开一个数组来存储系数,其下标为指数,同时注意指数和数组下标的对应关系。
        }
        if(account=1){printf("%d %d",k,e);}
        else if(account>1){printf(" %d %d",k,e);}
    }
    if(count=0){printf("0 0");}
    return 0;
}

这是算法书给的代码:

#include<cstdio>
int main(){
    int a[1010]={0};
    int k,e,count=0;
    while(scanf("%d,%d",&k,&e)!=EOF){
        a[e]=k;
    }
    a[0]=0;
    for(int i=1;i<=1000;i++){
        a[i-1]=a[i]*i;
        a[i]=0;//这一步归零,因为原来a[i]已经*i并赋值给a[i-1]了
        if(a[i-1]!=0) count++;
    }
    if(count==0) printf("0 0");
    else{
        for(int i=1000;i>=0;i--){
            if(a[i]!=0){
                printf("%d %d",a[i],i);
                count--;
                if(count!=0) printf(" ");
            }
        }
    }
    return 0;
}

A+B for Polynomials

#include<cstdio>
int main(){
    float c[1001] = {0};
    int m, n, t;
    float num;
    scanf("%d", &m);
    for (int i = 0; i < m; i++) {
        scanf("%d%f", &t, &num);//对于double类型,scanf是%f,printf是&lf
        c[t] += num;
    }
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%d%f", &t, &num);
        c[t] += num;
    }
    int cnt = 0;
    for (int i = 0; i < 1001; i++) {
        if (c[i] != 0) cnt++;
    }
    printf("%d", cnt);
    for (int i = 1000; i >= 0; i--) {
        if (c[i] != 0.0)
            printf(" %d %.1f", i, c[i]);
    }
    return 0;
}

Product of Polynomials

(挖坑,待解决)

#include<cstdio>
int main(){
    int n1, n2, a, cnt = 0;
    scanf("%d", &n1);
    double b, arr[1001] = {0.0}, ans[2001] = {0.0};//arr保存第一行数据,ans保存结果
    for(int i = 0; i < n1; i++) {
        scanf("%d %lf", &a, &b);
        arr[a] = b;
    }
    scanf("%d", &n2);
    for(int i = 0; i < n2; i++) {
        scanf("%d %lf", &a, &b);
        for(int j = 0; j < 1001; j++)
                ans[j + a] += arr[j] * b;
    }
    for(int i = 2000; i >= 0; i--)
        if(ans[i] != 0.0) cnt++;
    printf("%d", cnt);
    for(int i = 2000; i >= 0; i--)
        if(ans[i] != 0.0)
            printf(" %d %.1f", i, ans[i]);
    return 0;
}

本文标题:《简单模拟答案》

本文链接:https://wnag.com.cn/934.html

特别声明:除特别标注,本站文章均为原创,本站文章原则上禁止转载,如确实要转载,请电联:wangyeuuu@qq.com,尊重他人劳动成果,谢过~


正因为有要好好实现的梦想,所以今天也要好好加油。