# NENUOJ 之 C语言入门

# 前言

这一部分为NENUOJ的“C语言入门”部分,大部分题目不需要多余的讲解,所以会直接提供代码参考,除了少部分题目会写一点思路。

注意!一定一定一定一定一定一定不要抄袭和直接复制代码!

# 1001 输入输出训练之一A+B (opens new window)

# 题目描述

这是为编程新手设计的系列输入输出题之二,通过这个训练你会掌握OJ中最为常用的输入输出控制方式。 这里你只需要计算a+b,非常容易哟。不过请注意输入输出格式。

# 输入

输入文件中的第一行是一个整数N,然后是N行数据,每行有a和b两个整数,中间空格隔开。

# 输出

对于输入文件每一行,输出一行,为两个整数的和(不会超过整数的范围)。

# 样例输入 复制

2
1 2
-3 30

# 样例输出 复制

3
27

# 提示

需要使用循环

# 代码C语言:

#include<stdio.h>
int main(){
    int n,a,b;
    scanf("%d",&n);
    while(n--){
        scanf("%d %d",&a,&b);
        printf("%d\n",a+b);
    }
    return 0;
}

# 1002 输入输出训练之二A+B (opens new window)

# 题目描述

这是为编程新手设计的系列输入输出题之一,通过这个训练你会掌握OJ中最为常用的输入输出控制方式。 这里你只需要计算a+b,非常容易。不过请注意输入输出格式。

# 输入

输入文件包含很多数据,每行有a和b两个整数,中间空格隔开。

# 输出

对于输入文件每一行,输出一行,为两个整数的和(不超过整数范围)。

# 样例输入 复制

1 2
-3 30

# 样例输出 复制

3
27

# 提示

while(scanf("%d",&a)!=EOF) 或者 while(~scanf("%d",&a) 可以一直读a,直到文件尾。

# 代码C语言:

#include<stdio.h>
int main(){
    int a,b;
    while(scanf("%d %d",&a,&b)!=EOF){
        printf("%d\n",a+b);
    }
    return 0;
}

# 1003 输入输出训练之三A+B (opens new window)

# 题目描述

这是为编程新手设计的系列输入输出题之二,通过这个训练你会掌握OJ中最为常用的输入输出控制方式。 这里你只需要计算a+b,非常容易哟。不过请注意输入输出格式。

# 输入

输入文件包含很多行数据,每行有a和b两个整数,中间空格隔开。最后一行为两个0,表示输入结束,这两个0不做处理。

# 输出

对于输入文件每一行,输出一行,为两个整数的和(不超过整数的范围)。

# 样例输入 复制

1 2
-3 30
0 0

# 样例输出 复制

3
27

# 代码C语言:

#include<stdio.h>
int main(){
    int a,b;
    while( ~ scanf("%d %d",&a,&b)){
        if(a != 0 && b != 0){
            printf("%d\n",a+b);
        }
    }
    return 0;
}

# 1004 输入输出训练之四A+B (opens new window)

# 题目描述

这是为编程新手设计的系列输入输出题之四,通过这个训练你会掌握OJ中最为常用的输入输出控制方式。 这里你只需要计算a+b,非常容易哟。不过请注意输入输出格式。

# 输入

输入文件包含很多行数据,每行开始是一个整数N,然后紧跟着有N个整数。每个整数中间空格隔开。最后一行为0,表示输入结束,这个0不做处理。

# 输出

对于输入文件每一行,输出一行,为N个整数的和(不超过整数范围)。

# 样例输入 复制

3 7 5 3
5 1 6 3 2 8
0

# 样例输出 复制

15
20

# 代码C语言:

#include<stdio.h>
int main(){
    int n,m;
    int sum = 0;
    while(~scanf("%d",&n)){
        if(n == 0) break;
        for(int i = 1;i <= n;i++){
            scanf("%d",&m);
            sum += m;
        }
        printf("%d\n",sum);
        sum = 0;
    }
    return 0;
}

# 1005 输入输出训练之五A+B (opens new window)

# 题目描述

这是为编程新手设计的系列输入输出题之五,通过这个训练你会掌握OJ中最为常用的输入输出控制方式。 这里你只需要计算a+b,非常容易哟。不过请注意输入输出格式。

# 输入

输入文件包含很多行数据,第一行是一个整数N,然后紧跟着有N行数据。每行开始是一个整数M,然后是M个整数,每个整数中间空格隔开。

# 输出

对于输入文件每一行,输出一行,为M个整数的和。

# 样例输入 复制

2
3 7 5 3
5 1 6 3 2 8

# 样例输出 复制

15
20

# 代码C语言:

#include<stdio.h>
int main(){
    
    int n,m;
    int t;
    int sum = 0;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        for(int i = 1;i <= n;i++){
            scanf("%d",&m);
            sum += m;
        }
        printf("%d\n",sum);
        sum = 0;
    }
    return 0;
}

# 1006 输入输出训练之六A+B (opens new window)

# 题目描述

这是为编程新手设计的系列输入输出题之六,通过这个训练你会掌握OJ中最为常用的输入输出控制方式。 这里你只需要计算a+b,非常容易哟。不过请注意输入输出格式。

# 输入

输入文件包含很多行数据,每行包括两个整数a和b,中间空格隔开。

# 输出

对于输入文件每一行,输出一行,为a和b的和(不超过整数范围)。每行数据之间用一个空行隔开。

# 样例输入 复制

1 2
-3 30

# 样例输出 复制

3

27

# 代码C语言:

#include<stdio.h>
int main(){
    
    int a,b;
    int flag = 0;
    while(~scanf("%d %d",&a,&b)){
        
        if(flag == 1) printf("\n");
        
        printf("%d\n",a+b);
        
        flag=1;
    }
    return 0;
}

# 1007 输入输出训练之七A+B (opens new window)

# 题目描述

这是为编程新手设计的系列输入输出题之七,通过这个训练你会掌握OJ中最为常用的输入输出控制方式。 这里你只需要计算a+b,非常容易哟。不过请注意输入输出格式。

# 输入

输入文件中的第一行是一个整数N,然后是N行数据,每行有a和b两个整数,中间空格隔开。

# 输出

对于输入文件每一行,输出一行,为两个整数的和(不超过整数范围),每行数据用空行隔开,最后一行不用空行。

# 样例输入 复制

2
1 2
-3 30

# 样例输出 复制

3

27

# 代码C语言:

#include<stdio.h>
int main(){
    
    int a,b;
    int flag = 0;
    int n;
    scanf("%d",&n);
    while(n--){
        scanf("%d %d",&a,&b);
        if(flag == 0) printf("%d\n",a + b);
        else printf("\n%d\n",a + b);
        flag = 1;
    }
    return 0;
}

# 1008 循环训练之一 (opens new window)

# 题目描述

给定整数n,请按照要求输出指定的图形。

# 输入

每行一个正整数n。

# 输出

对于每行的n在屏幕上输出指定的图形,每个图形之间有一个空行隔开。

# 样例输入 复制

3
6

# 样例输出 复制

*
**
***

*
**
***
****
*****
******

# 代码C语言:

#include<stdio.h>
int main(){
    
    int n;
    int i,j;
    int flag=0;
    while(~scanf("%d",&n)){
        if(flag==1) printf("\n");
        for(i = 1; i <= n; i++){
            for(j = 1; j <= i; j++){
                printf("*");
            }
            printf("\n");
        }
        flag=1;
    }
    return 0;
}

# 1009 循环训练之二 (opens new window)

# 题目描述

给定整数n,请按照要求输出指定的图形。

# 输入

每行一个正整数n。

# 输出

对于每行的n在屏幕上输出指定的图形,每个图形之间有一个空行隔开。

# 样例输入 复制

3
6

# 样例输出 复制

***
**
*

******
*****
****
***
**
*

# 代码C语言:

#include<stdio.h>
int main(){
    int n;
    int i,j;
    int flag=0;
    while(~scanf("%d",&n)){
        if(flag==1) printf("\n");
        for(i = 1; i <= n; i++){
            for(j = n; j >= i; j--){
                printf("*");
            }
            printf("\n"); 
        } 
        flag=1;  
    }
    return 0;
}

# 1010 循环训练之三 (opens new window)

# 题目描述

给定整数n,请按照要求输出指定的图形。

# 输入

每行一个正整数n。

# 输出

对于每行的n在屏幕上输出指定的图形,每个图形之间有一个空行隔开。

# 样例输入 复制

3
6

# 样例输出 复制

  *
 **
***

     *
    **
   ***
  ****
 *****
******

# 代码C语言:

#include<stdio.h>
int main(){
    
    int n;
    int i,j;
    int flag=0;
    while(~scanf("%d",&n)){
        
        if(flag==1) printf("\n");
        
        for(i=1;i<=n;i++){
            
            for(j=n-1;j>=i;j--){
                printf(" ");
            }
            
            for(j=1;j<=i;j++){
                printf("*");
            }
            
            printf("\n");
        }
        flag=1;
    }
    
    return 0;
}

# 1011 循环训练之四 (opens new window)

# 题目描述

给定整数n,请按照要求输出指定的图形。

# 输入

每行一个正整数n。

# 输出

对于每行的n在屏幕上输出指定的图形,每个图形之间有一个空行隔开。

# 样例输入 复制

3
6

# 样例输出 复制

***
 **
  *

******
 *****
  ****
   ***
    **
     *

# 代码C语言:

#include<stdio.h>
int main(){
    
    int n;
    int i,j;
    int flag=0;
    while(~scanf("%d",&n)){
        if(flag==1) printf("\n");
        for(i=1;i<=n;i++){
            for(j=1;j<i;j++){
                printf(" ");
            }
            for(j=n;j>=i;j--){
                printf("*");
                
            }
            printf("\n");
        }
        flag=1;
    }
    
    return 0;
}

# 1012 循环训练之五 (opens new window)

# 题目描述

给定正整数n,请按照要求输出指定的图形。

# 输入

每行一个正整数n。

# 输出

对于每行的n在屏幕上输出指定的图形,每个图形之间有一个空行隔开。

# 样例输入 复制

2
4

# 样例输出 复制

 *
***

   *
  ***
 *****
*******

# 代码C语言:

#include<stdio.h>
int main(){
    
    int n;
    int i,j;
    int flag=0;
    while(~scanf("%d",&n)){
        if(flag==1) printf("\n");
        for(i=0;i<n;i++){
            for(j=n-1;j>i;j--){
                printf(" ");
            }
            for(j=1;j<=2*i+1;j++){
                printf("*");
            }
            printf("\n");
        }
        flag=1;
    }
    
    return 0;
}

# 1013 循环训练之六 (opens new window)

# 题目描述

给定正整数n,请按照要求输出指定的图形。

# 输入

每行一个正整数n。

# 输出

对于每行的n在屏幕上输出指定的图形,每个图形之间有一个空行隔开。

# 样例输入 复制

2
4

# 样例输出 复制

***
 *

*******
 *****
  ***
   *

# 代码C语言:

#include<stdio.h>
int main(){
    
    int n;
    int i,j;
    int flag=0;
    while(~scanf("%d",&n)){
        if(flag==1) printf("\n");
        for(i=1;i<=n;i++){

            for(j=1;j<i;j++){
                printf(" ");
            }

            for(j=1;j<=2*n-2*i+1;j++){
                printf("*");
            }

            printf("\n");
        }
        flag=1;
    }
    
    return 0;
}

# 1014 循环训练之七 (opens new window)

# 题目描述

给定正整数n,请按照要求输出指定的图形。

# 输入

每行一个正整数n。

# 输出

对于每行的n在屏幕上输出指定的图形,每个图形之间有一个空行隔开。

# 样例输入 复制

2
4

# 样例输出 复制

***
 *
***

*******
 *****
  ***
   *
  ***
 *****
*******

# 代码C语言:

#include<stdio.h>
int main(){
    
    int n;
    int flag=0;
    int i,j;
    while(~scanf("%d",&n)){
        if(flag==1) printf("\n");
        for(i=1;i<=n;i++){
            for(j=1;j<i;j++){
            printf(" ");
        }
            for(j=1;j<=2*n-2*i+1;j++){
            printf("*");
        }
            printf("\n");
        }
        for(i=1;i<=n-1;i++){
            for(j=1;j<n-i;j++){
            printf(" ");
        }
            for(j=1;j<=2*i+1;j++){
            printf("*");
        }
            printf("\n");
        }
        flag=1;
    }
    return 0;
}

# 1015 华氏温度转换为摄氏温度 (opens new window)

# 题目描述

编程实现华氏温度转换为摄氏温度,计算公式为:c=5 * (f-32) / 9,其中,c表示摄氏温度,f表示华氏温度。

# 输入

输入中每行一个实数,表示华氏温度。

# 输出

要求输出对应的摄氏温度,保留3位小数。

# 样例输入 复制

150
30
0
-34.5

# 样例输出 复制

65.556
-1.111
-17.778
-36.944

# 代码C语言:

#include<stdio.h>
int main(){
    
    double f;
    double c;
    
    while(~scanf("%lf",&f)){
        
        c=5.0*(f-32)/9.0;
        
        printf("%.3lf\n",c);
    }
    return 0;
}

# 1016 摄氏温度转换为华氏温度 (opens new window)

# 题目描述

编程实现摄氏温度转换为华氏温度,计算公式为:f=9 * c / 5 + 32;其中,c表示摄氏温度,f表示华氏温度。

# 输入

输入中每行一个整数,表示摄氏温度。

# 输出

要求按输出样例格式进行输出,保留1位小数。

# 样例输入 复制

26
-10

# 样例输出 复制

celsius=26.0,fahr=78.8
celsius=-10.0,fahr=14.0

# 代码C语言:

#include<stdio.h>
int main(){
    
    double f;
    double c;
    
    while(~scanf("%lf",&c)){
        
        f=9.0*c/5.0 + 32;
        
        printf("celsius=%.1lf,fahr=%.1lf\n",c,f);
    }
    return 0;
}

# 1017 平均成绩 (opens new window)

# 题目描述

输入每行为3个整数(0~100),代表某位学生的数学、英语和计算机课程的成绩,求该生的平均成绩,以整数表示(四舍五入)。输出格式见输出样例。

# 输入

输入有很多行,每行为3个整数,分别表示数学、英语和计算机课的成绩。

# 输出

输出该生各科成绩和平均成绩(四舍五入)。每个学生一行。

# 样例输入 复制

87 72 93
42 60 29
0 100 50

# 样例输出 复制

math=87,eng=72,comp=93,average=84
math=42,eng=60,comp=29,average=44
math=0,eng=100,comp=50,average=50

# 代码C语言:

#include<stdio.h>
int main(){
    int math,english,computer;
    int average;
    while(~scanf("%d %d %d",&math,&english,&computer)){
        average=(int)((math+english+computer)/3.0+0.5);
        printf("math=%d,eng=%d,comp=%d,average=%d\n",math,english,computer,average);
    }
    return 0;
}

# 1018 成绩排序 (opens new window)

# 题目描述

已知某个班有n(1 <= n <= 100)个学生,输入每行为学生姓名(最多20个字符)和其c语言成绩(0~100),请按照成绩从高到低排序后输出。若有相同的,不能改变其顺序。

# 输入

输入有多行,每行包括学生姓名和成绩,空格隔开。

# 输出

按照成绩从高到低排序输出,成绩相同的按照原始顺序。

# 样例输入 复制

Zhangsan 80
Lisi 95
Zhouyi 69
Wangwu 73
Zhaoliu 69

# 样例输出 复制

Lisi 95
Zhangsan 80
Wangwu 73
Zhouyi 69
Zhaoliu 69

# 思路:

这道题其实方法很多,存储的话用数组,因为有名字和成绩要同时存储,所以我们用一个结构体来做,所以就是结构体数组。然后既然要成绩从高到低,直接写个冒泡排序什么的就可以,就是下方的代码一,是笔者大一上写的代码了。然后笔者写题解的时候,写了下方的代码二,思路上就是开个双重循环,外循环输出,内循环找最大的成绩做标记,用来在外层循环输出。vis数组是记录哪些人被我们输出了,如果已经被输出了就让vis[ i ] = 1,下次看到vis数组的值是1的话就会跳过了。下方代码三是C++代码,用sort函数真的很简单,不过虽然sort也能过,但是还是推荐用stable_sort,毕竟这道题要求输出稳定排序的结果(即不更改顺序)。

后面几道题和这道题也差不多,笔者推荐读者可以自行学习更好的代码,笔者这里不再重复写多种方法了。

# 代码C语言 方法一:

#include<stdio.h>
#include<string.h>
int main(){
    
    int i=0;
    int j=0;
    int k=0;
    int temp;//作为过渡
    char str[25];//作为过渡
    
    struct student{
        
        char name[25];
        
        int score;
        
    }stu[105];
    
    
    while(~scanf("%s%d",stu[i].name,&stu[i].score)){
        i++;
    }
    
    for(j=0;j<i-1;j++){
        
        for(k=0;k<i-1-j;k++){
            if(stu[k].score<stu[k+1].score){
                
                strcpy(str,stu[k+1].name);
                strcpy(stu[k+1].name,stu[k].name);
                strcpy(stu[k].name,str);

                temp = stu[k+1].score;
                stu[k+1].score=stu[k].score;
                stu[k].score=temp;

        }
    }
    }
    
    for(j=0;j<i;j++){
        if(j!=i-1) printf("%s %d\n",stu[j].name,stu[j].score);
        
        else if(j==i-1) printf("%s %d",stu[j].name,stu[j].score);
    }
    
    return 0;
}

# 代码C语言 方法二:

#include<stdio.h>
#include<string.h>
struct stu{
    char name[100];
    int score;
}stu[105];
int vis[105];
int max(int a,int b){
    return a > b ? a : b;
}
int main(){
    int i = 0;
    while(~scanf("%s %d",stu[i].name,&stu[i].score)){
        i++;
    }
    int maxx = -1;
    char temp[100];
    int index = 0;
    for(int j = 0;j < i;j++){
        for(int k = 0;k < i;k++){
            if(vis[k] == 1) continue;
            int changed = maxx;
            maxx = max(maxx,stu[k].score);
            if(maxx != changed){//表示出现了更新
                index = k;
            }
        }
        if(j != i - 1) printf("%s %d\n",stu[index].name,stu[index].score);
        else printf("%s %d",stu[index].name,stu[index].score);
        vis[index] = 1;
        maxx = -1;
        index = 0;
    }
    return 0;
}

# 代码C++ 方法三:

#include<bits/stdc++.h>
using namespace std;
struct stu{
    string name;
    int score;
}stu[105];

int i = 0;

bool cmp(struct stu a,struct stu b){
    return a.score > b.score;
}

int main(){
    while(cin >> stu[i].name >> stu[i].score){
        i++;
    }
    stable_sort(stu,stu + i,cmp);
    for(int j = 0;j < i;j++){
        cout << stu[j].name << " " << stu[j].score << "\n";
    }
}

# 1019 成绩排序之二 (opens new window)

# 题目描述

已知某个班有n(1 <= n <= 100)个学生,输入每行为学生姓名(最多20个字符)和其c语言成绩(0~100),请按照成绩从高到低排序后输出。

# 输入

输入有多行,每行为一个学生,包括学生姓名和成绩,空格隔开。

# 输出

按成绩从高到低排序后输出,每个学生一行,姓名占20列,左对齐,成绩占3列,右对齐,中间用一个空格隔开。若有相同的,不能改变其原始顺序。

# 样例输入 复制

Zhangsan 80
Lisi 100
Zhouyi 69
Wangwu 73
Zhaoliu 69

# 样例输出 复制

Lisi                 100
Zhangsan              80
Wangwu                73
Zhouyi                69
Zhaoliu               69

# 代码C语言:

#include<stdio.h>
#include<string.h>
int main(){
    
    int i=0;
    int j=0;
    int k=0;
    int temp;//作为过渡
    char str[25];//作为过渡
    
    struct student{
        
        char name[25];
        
        int score;
        
    }stu[105];
    
    
    while(~scanf("%s%d",stu[i].name,&stu[i].score)){
        i++;
    }
    
    for(j=0;j<i-1;j++){
        
        for(k=0;k<i-1-j;k++){
            if(stu[k].score<stu[k+1].score){
                
                strcpy(str,stu[k+1].name);
                strcpy(stu[k+1].name,stu[k].name);
                strcpy(stu[k].name,str);

                temp = stu[k+1].score;
                stu[k+1].score=stu[k].score;
                stu[k].score=temp;

        }
    }
    }
    
    for(j=0;j<i;j++){
        if(j!=i-1) printf("%-20s %3d\n",stu[j].name,stu[j].score);
        
        else if(j==i-1) printf("%-20s %3d",stu[j].name,stu[j].score);
    }
    
    return 0;
}

# 1020 最高分和最低分 (opens new window)

# 题目描述

已知某个班有n(1 <= n <= 100)个学生,输入每行为学生姓名(最多20个字符)和其c语言成绩(0~100),请找出最高分和最低分输出,若最高分或最低分多于1个的,按先后顺序输出。

# 输入

输入有多行,每行表示一个学生的信息,包括姓名和成绩,空格隔开。

# 输出

输出最高分和最低分,多于1个的按出现的顺序依次输出,格式详见输出样例。

# 样例输入 复制

Zhangsan 80
Lisi 95
Zhouyi 69
Wangwu 73
Zhaoliu 69

# 样例输出 复制

Max is 95,name has Lisi
Min is 69,name have Zhouyi,Zhaoliu

# 代码C语言:

#include<stdio.h>
#include<string.h>
int main(){
    
    int i=0;
    int j=0;
    int k=0;
    int temp;//作为过渡
    char str[25];//作为过渡
    int min,max;
    int count1=1,count2=1;
    
    struct student{
        
        char name[25];
        
        int score;
        
    }stu[105];
    
    
    while(~scanf("%s%d",stu[i].name,&stu[i].score)){
        i++;
    }
    
    for(j=0;j<i-1;j++){
        
        for(k=0;k<i-1-j;k++){
            if(stu[k].score<stu[k+1].score){
                
                strcpy(str,stu[k+1].name);
                strcpy(stu[k+1].name,stu[k].name);
                strcpy(stu[k].name,str);

                temp = stu[k+1].score;
                stu[k+1].score=stu[k].score;
                stu[k].score=temp;

        	}
    	}
    }
    max=stu[0].score;
    min=stu[i-1].score;
    for(j=1;j<i-1;j++){
        if(max==stu[j].score) count1++;
        if(min==stu[j].score) count2++;
    }
    
    if(count1==1) printf("Max is %d,name has %s",stu[0].score,stu[0].name);
    else {
        printf("Max is %d,name have %s",stu[0].score,stu[0].name);
            for(j=1;j<count1;j++)
        printf(",%s",stu[j].name);
    }
    printf("\n");
    if(count2==1) printf("Min is %d,name has %s",stu[i-1].score,stu[i-1].name);
    else {
        printf("Min is %d,name have %s",stu[i-1].score,stu[i-count2].name);
            for(j=i-count2+1;j<=i-1;j++)
        printf(",%s",stu[j].name);
    }
    
    return 0;
}

# 1021 按位输出 (opens new window)

# 题目描述

把正整数按位进行输出。

# 输入

输入文件中每行为一个正整数n。

# 输出

把整数n按位输出,从低到高。

# 样例输入 复制

0
999
123

# 样例输出 复制

0 has one number,is 0
999 have three numbers,are 9,9,9
123 have three numbers,are 3,2,1

# 小提示:

这道题如果只枚举到nine是不够的,要枚举到ten哦!

# 代码C语言:

#include<stdio.h>
int main(){
    
    int n;
    int count=0;
    int num[10000];
    int i;
    int temp;
    char numm[21][20]={"zero","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","ninetten","twenty"};
    
    while(~scanf("%d",&n)){
        
        count=0;
        temp=n;
        
        if(n==0){
            printf("0 has one number,is 0\n");
            continue;
        }
        
        while(n!=0){
            num[count++]=n%10;
            n = n / 10;
        }
        
        if(count==1) printf("%d has one number,is %d\n",temp,num[0]);
        
        else{
            printf("%d have %s numbers,are %d",temp,numm[count],num[0]);
            for(i=1;i<count;i++){
                printf(",%d",num[i]);
            }
            printf("\n");
        }
    }
    
    return 0;
}

# 1022 利息 (opens new window)

# 题目描述

输入存款金额money、存期year和年利率rate,根据下列公式计算存款到期时的利息interest(税前)。

# 输入

输入可能有多行,每行有3个整数,分别表示金额,存期(年),年利率(百分比数)。

# 输出

根据money * (1+rate)year-money 来计算利息,每个输入对应一行输出,保留小数点两位。

# 样例输入 复制

1000 3 2

# 样例输出 复制

interest=61.21

# 代码C语言:

#include<stdio.h>
int main(){
    
    int money;
    int year;
    int rate;
    double temp = 1.0;
    double interest = 0;
    
    while(~scanf("%d %d %d",&money,&year,&rate)){
        
        temp = 1.0;
        
        while(year--){
            temp = temp * ( 1 + rate/100.0 );
        }
        
        interest = money * temp - money; 
        
        printf("interest=%.2f\n",interest);
    }
    
    return 0;
}

# 1023 分段函数 (opens new window)

# 题目描述

数学中经常使用分段函数来计算函数值,请编程实现。

# 输入

输入文件有多行,每行包括一个x,要求计算f(x)的值。

# 输出

当x<0时,f(x)=(x+1) * (x+1)+2x+1/x,当x>=0时,f(x)=x的平方根,保留两位小数,每行一个结果。

# 样例输入 复制

10
-0.5
0

# 样例输出 复制

f(10.00)=3.16
f(-0.50)=-2.75
f(0.00)=0.00

# 代码C语言:

#include<stdio.h>
#include<math.h>
int main(){
    
    double x;
    double fx;
    
    while(~scanf("%lf",&x)){
        if( x >= 0 ) fx = sqrt ( x );
        if( x < 0 ){
            fx = ( x + 1 ) * ( x + 1 ) + 2 * x + 1 / x ;
        }
        printf("f(%.2f)=%.2f\n",x,fx); 
    }
    
    return 0;
    
}

# 1024 四则运算 (opens new window)

# 题目描述

我们经常进行四则运算(+、-、、/、%),请编程实现。

# 输入

输入文件中每行包括两个整数a和b和一个运算符(+、-、*、/、%),要求根据运算符来输出运算结果,其中b不为0。

# 输出

按格式输出结果,每行一个,b为负数时需要加上()进行运算。

# 样例输入 复制

5 3 +
-5 3 *
5 -3 -

# 样例输出 复制

5 + 3 = 8
-5 * 3 = -15
5 - (-3) = 8

# 小提示:

题目叫四则运算,但是有五个符号哦~

这道题还可以用switch做,代码会更好看一点~

# 代码C语言:

#include<stdio.h>
int main(){
    
    int a,b;
    char sign;
    
    while(~scanf("%d %d %c",&a,&b,&sign)){
        
        if( sign == '+' ){
            if( b >= 0 )
                printf("%d + %d = %d\n",a,b,a+b);
            else printf("%d + (%d) = %d\n",a,b,a+b);
        }
        
        if( sign == '-' ){
            if( b >= 0 )
                printf("%d - %d = %d\n",a,b,a-b);
            else printf("%d - (%d) = %d\n",a,b,a-b);
        }
        
        if( sign == '*' ){
            if( b >= 0 )
                printf("%d * %d = %d\n",a,b,a*b);
            else printf("%d * (%d) = %d\n",a,b,a*b);
        }
        
        if( sign == '/' ){
            if( b >= 0 )
                printf("%d / %d = %d\n",a,b,a/b);
            else printf("%d / (%d) = %d\n",a,b,a/b);
        }
        
        if( sign == '%' ){
            if( b >= 0 )
                printf("%d %% %d = %d\n",a,b,a%b);
            else printf("%d %% (%d) = %d\n",a,b,a%b);
        }
        
    }
    
    return 0;
    
}

# 1025 旅途时间 (opens new window)

# 题目描述

我们经常需要计算旅途时间,请编程实现。

# 输入

输入文件中有很多行,每行包括2个整数time1和time2,分别表示火车的出发时间和到达时间。有效时间的范围为0000-2359(前两位表示小时,后两位表示分钟,不足3位的均为分钟,3位数的话,后两位为分钟),不需要考虑出发时间晚于到达时间的情况。

# 输出

根据输入计算并输出旅途时间。

# 样例输入 复制

800 900
711 1431
800 836

# 样例输出 复制

The train journey time is 1 hours.
The train journey time is 7 hours 20 minutes.
The train journey time is 36 minutes.

# 小提示:

笔者把这道题认为是入门篇最难的题目,因为真的很容易写错,也很难debug,笔者在大一的时候被困住了非常非常久。

方法一为笔者大一上的代码,非常冗余,并且要做很多的分类讨论。

方法二为笔者撰写题解时的新写法,非常值得学习,我们的思路就是统一标准,把time1和time2都统一为分钟数,然后求差值再去算差了几个小时和差了多少分钟,这种算法不容易出错,也避免了冗余的分类讨论。

# 代码C语言 方法一:

#include<stdio.h>
int main(){
    
    int time1;
    int time2;
    int hour1,hour2,min1,min2;
    while (~scanf("%d %d",&time1,&time2)){
        hour1 = time1 / 100;
        hour2 = time2 / 100;
        min1 = time1 % 100;
        min2 = time2 % 100;
        if ( hour1 == hour2 ) {
            if ( min2 < min1 ) {
                printf("The train journey time is %d hours %d minutes.\n",24+hour2-hour1-1,min2-min1+60);
            }
            else if( min2 >= min1 ){
                printf("The train journey time is %d minutes.\n",min2-min1);
            }
        }
        else if ( hour2 > hour1 ) {
            if ( min1 == min2 ) {
                printf("The train journey time is %d hours.\n",hour2-hour1);
            }
            else if( min2 > min1 ){
                printf("The train journey time is %d hours %d minutes.\n",hour2-hour1,min2-min1);
            }
            else if( min2 < min1 ){
                if ( hour2-hour1-1 != 0 )
                    printf("The train journey time is %d hours %d minutes.\n",hour2-hour1-1,60+min2-min1);
                else printf("The train journey time is %d minutes.\n",60+min2-min1);
            }
        }
        else if ( hour2 < hour1 ) {
            if ( min1 == min2 ) {
                printf("The train journey time is %d hours.\n",24+hour2-hour1);
            }
            else if( min2 > min1 ){
                printf("The train journey time is %d hours %d minutes.\n",24+hour2-hour1,min2-min1);
            }
            else if( min2 < min1 ){
                printf("The train journey time is %d hours %d minutes.\n",24+hour2-hour1-1,60+min2-min1);
            }
        }
    }
    return 0;
    
}

# 代码C语言 方法二:

#include<stdio.h>
int main(){
    int t1,t2;
    while(~scanf("%d %d",&t1,&t2)){
        int min1 = (t1/100)*60 + t1%100;
        int min2 = (t2/100)*60 + t2%100;
        int min = min2 - min1;
        if(min % 60 == 0){
            printf("The train journey time is %d hours.\n",min / 60);
        }else{
            if(min / 60 == 0){
                printf("The train journey time is %d minutes.\n",min % 60);
            }else{
                printf("The train journey time is %d hours %d minutes.\n",min / 60,min % 60);
            }
        }
    }
    return 0;   
}

# 1026 数字加密 (opens new window)

# 题目描述

输入文件中包含很多行,每行是一个四位正整数,需要将其加密后输出。加密的方法是将该数的每一位上的数字加9,然后除以10取余,作为该位上的新数字,最后将千位和十位上的数字互换,百位和个位上的数字互换,组成加密后的新四位数。

# 输入

输入有多行,每行一个四位正整数n。

# 输出

加密后输出,每行一个。

# 样例输入 复制

1257
6711

# 样例输出 复制

4601
56

# 代码C语言:

#include<stdio.h>
void swap(int *a,int *b){
    
    int temp;
    temp = *a;
    *a = *b;
    *b = temp;
    
}
int main(){
    
    int n;
    int ge,shi,bai,qian;
    while(~scanf("%d",&n)){
        
        ge = n % 10;
        shi = (n % 100 - ge) / 10;
        bai = n / 100 % 10;
        qian = n / 1000;
        
        ge = ( ge + 9 ) % 10;
        shi = ( shi + 9 ) % 10;
        bai = ( bai + 9 ) % 10;
        qian = ( qian + 9 ) % 10;
        
        swap (&shi,&qian);
        swap (&bai,&ge);
        
        if( qian != 0 )
            printf("%d%d%d%d\n",qian,bai,shi,ge);
        
        else if( qian == 0 && bai != 0 )
            printf("%d%d%d\n",bai,shi,ge);
        
        else if( qian == 0 && bai == 0 && shi != 0 )
            printf("%d%d\n",shi,ge);
        
        else if( qian == 0 && bai == 0 && shi == 0 )
            printf("%d\n",ge);
        
    }
    
    return 0;
    
}

# 1027 累加和 (opens new window)

# 题目描述

求累加和。

# 输入

输入文件中包含很多行,每行有一个正整数n,要求输出1+1/3+1/5+……+1/(2 * n-1)的前n项之和。

# 输出

每个输出占一行,输出时保留6位小数。

# 样例输入 复制

1
5

# 样例输出 复制

1.000000
1.787302

# 代码C语言:

#include<stdio.h>
int main(){
    
    int n;
    int i;
    double sum = 0;
    
    while(~scanf("%d",&n)){
        
        sum = 0;
        
        for( i = 1 ; i <= n ; i++){
            sum = sum + 1.0 / ( 2 * i - 1 );
        }
        
        printf("%.6lf\n",sum);
        
    }
    
    return 0;
    
}

# 1028 最小公倍数和最大公约数 (opens new window)

# 题目描述

编程求最小公倍数和最大公约数。

# 输入

输入文件有多行,每行有两个正整数m和n。

# 输出

要求输出它们的最小公倍数和最大公约数。中间用空行隔开。

# 样例输入 复制

3 7
6 9

# 样例输出 复制

The lowest common multiple of 3 and 7 is 21.
The greatest common divisor of 3 and 7 is 1.

The lowest common multiple of 6 and 9 is 18.
The greatest common divisor of 6 and 9 is 3.

# 代码C语言:

#include<stdio.h>
int gcd(int a,int b){
    if( b==0 ) return a;
    return gcd ( b , a % b );
}
int main(){
    
    int flag = 0;
    int m,n;
    while(~scanf("%d %d",&m,&n)){
        if( flag == 1 ) printf("\n");
        printf("The lowest common multiple of %d and %d is %d.\n",m,n,m*n/gcd(m,n));
        printf("The greatest common divisor of %d and %d is %d.\n",m,n,gcd(m,n));
        flag = 1;
    }
    
    return 0;
    
}

# 1029 素数的和 (opens new window)

# 题目描述

求素数的和。

# 输入

输入文件有很多行,每行包含一些正整数(零或负数表示结束),要求统计其中的素数的和。

# 输出

每行输出一个和。

# 样例输入 复制

2 3 4 5 10 0
4 2 5 14 6 7 -3

# 样例输出 复制

10
14

# 代码C语言:

#include<stdio.h>
#include<math.h>
int judge(int n){
    if(n == 1) return 0;
    int i = 2;
    for(i = 2;i <= sqrt(n);i++){
        if(n % i == 0) break;
    }
    if(i > sqrt(n)) return 1;
    else return 0;
}
int main(){
    int n;
    int sum = 0;
    while(~scanf("%d",&n)){
        if(n <= 0) {
            printf("%d\n",sum);
            sum = 0;
            continue;
        }
        if(judge(n))
            sum += n;
    }
}

# 1030 另类累加和 (opens new window)

# 题目描述

求累加和。

# 输入

输入文件中有很多行,每行包括2个正整数a和n(1 <= a,n <= 9)。

# 输出

求a+aa+aaa+aa…a(n个a)之和。

# 样例输入 复制

1 2
8 5

# 样例输出 复制

12
98760

# 代码C语言:

#include<stdio.h>
int solve(int a,int n){
    int i;
    int temp = a;
    for( i = 1 ; i < n ;i++){
        temp = 10 * temp + a;
    }
    return temp;
}
int main(){
    int sum;
    int a,n;
    int i;
    while(~scanf("%d %d",&a,&n)){
        
        sum = 0;
        for(i=1;i<=n;i++){
            sum += solve(a,i);
        }
        printf("%d\n",sum);
    }
    return 0;
}

# 总结

  1. 这些题基本上就是练一练写代码的基本功,都没有什么很大的难度。
  2. 最推荐做的题目是前面的 输入输出训练 + 循环训练 + 成绩排序 + 四则运算 + *旅途时间 + 素数的和 ,前两者是基础,后四者做了也都会有所收获。
  3. 笔者的大部分代码出自大一上时期,所以很多代码的代码规范不太好,解法也不是最优解,只有部分代码因为太丑了所以重构过,谨慎参考,一定不要直接抄代码!!!