编程练习 15.10.18~15.10.25


15331281 本人学院


新的一周,新的开始,大家好,欢迎收看校园内外。^.^(什么鬼)
好啦新的一周的编程练习又来了,这周又有什么题目1

简单数论题

给出两个正整数m和n,请求出这个两个数的最大公约数(greatest common divisor,后面简写为gcd)和最小公倍数( least common multiple,后面简称lcm)。
测试样例:
input:25 45
output: the gcd about these two numbers is: 5
the lcm about these two numbers is: 225

         2.建议学习自定义函数来结构化代码。```

### my answer
```c




<div class="se-preview-section-delimiter"></div>

#include<stdio.h>
int main() {
    int x, y, a, b, c, gcd, lcm;
    scanf("%d%d", &x, &y);
    a = x, b = y;
    if (a < b) {
        c = a, a = b, b = c;
    }
    c = a % b;
    while (c != 0) {
        a = b;
        b = c;
        c = a % b;
    }
    gcd = b;
    lcm = x * y / gcd;
    printf("the gcd about these two numbers is: %d\n", gcd);
    printf("the lcm about these two numbers is: %d\n", lcm);
    return 0;
}




<div class="se-preview-section-delimiter"></div>

the standard answer





<div class="se-preview-section-delimiter"></div>

#include<stdio.h>
int gcd(int m, int n);
int lcm(int m, int n);

int main(void) {
    int m, n;
    scanf("%d%d", &m, &n);

    if (m >= 1 && n >= 1) {
    printf("the gcd about these two numbers is: %d\n", gcd(m, n));
    printf("the lcm about these two numbers is: %d\n", lcm(m, n));
    } else {
        printf("the input is invalid!\n");
    }
    return 0;
}

int gcd(int m, int n) {
    if (n == 0) return m;
    return gcd(n, m%n);
}

int lcm(int m, int n) {
    return ((m*n)/gcd(m, n));
}




<div class="se-preview-section-delimiter"></div>

反馈

当然,当时并不知道什么是自定义函数,所以只能先用老办法,下面提供自定义函数的参考资料~

自定义函数

参考资料1
参考资料2

定义函数分为两步:
I.声明:在main函数开头定义变量的位置,声明函数:定义上述函数声明是:
int add(int a,int b);
注意分号,声明前部不能有执行语句;
II.把下面的函数定义部分放在main函数外部
int add(int a,int b)
{
return a+b;
}
推荐放在main的后花括号后面,注意此处的int add(int a,int b)后面并没有分号,()内要注明每个形参的数据类型

Investigating the land(for lab)

Description
Fred Mapper is considering purchasing some land in Louisiana to build his house on. In the process of investigating the land, he learned that the state of Louisiana is actually shrinking by 50 square miles each year, due to erosion caused by the Mississippi River. Since Fred is hoping to live in this house the rest of his life, he needs to know if his land is going to be lost to erosion.

After doing more research, Fred has learned that the land that is being lost forms a semicircle. This semicircle is part of a circle centered at (0,0), with the line that bisects the circle being the X axis. Locations below the X axis are in the water. The semicircle has an area of 0 at the beginning of year 1. (Semicircle illustrated in the Figure.)
Input
Each test will be one line contain the X and Y Cartesian coordinates of the land Fred is considering. These will be floating point numbers(variable type of X and Y is double) measured in miles. The Y coordinate will be non-negative. (0,0) will not be given.
Notes: pi = 3.141592654
Output
A single line of output should appear. This line should take the form of: “This property will begin eroding in year Z.” Where Z is the first year (start from 1) this property will be within the semicircle AT THE END OF YEAR Z. Z must be an integer.
Sample Input
1.0 1.0
25.0 0.0
Sample Output
This property will begin eroding in year 1.
This property will begin eroding in year 20.

my answer





<div class="se-preview-section-delimiter"></div>

#include<stdio.h>




<div class="se-preview-section-delimiter"></div>

#include<math.h>
int main() {
    float x, y, r, s, time1;
    int year;
    double pi = 3.141592654;
    scanf("%f%f", &x, &y);
    r = sqrt(x * x + y * y);
    s = pi * r * r / 2;
    time1 = s / 50;
    int time = time1;
    float jud = time1 - time;
    if (jud == 0) {
    year = time;
    } else {
       year =  time + 1;
    }
    printf("This property will begin eroding in year %d.\n", year);
    return 0;
}





<div class="se-preview-section-delimiter"></div>

standard answer





<div class="se-preview-section-delimiter"></div>

#include <stdio.h>
const double pi = 3.141592654;

int main() {
    double x, y;
    scanf("%lf %lf", &x, &y);
    double area = pi * (x * x + y * y);
    int year = (area / 100.0 + 1.0);
    printf("This property will begin eroding in year %d.\n", year);
    return 0;
}




<div class="se-preview-section-delimiter"></div>

反馈

const:const是一个C语言的关键字,它限定一个变量不允许被改变,产生静态作用。使用const在一定程度上可以提高程序的安全性和可靠性。[^footnote2]

char2Int(for hw)

Input three integer numbers within [0, 9] in the type of char , output their ascii codes;
Convert these three characters into int type, add them and output the summation.
Input: three characters should be inputed in one line without any blank space. It is recommended to use the getchar() function.
Output: one ascii code occupies one single line. Every output ends with a ‘\n’.
Sample:
Input:234
Output:
50
51
52
9
hint:没有思路的同学可以参考课件2里面的大小写转换程序

my answer





<div class="se-preview-section-delimiter"></div>

#include<stdio.h>




<div class="se-preview-section-delimiter"></div>

#include<stdlib.h>
int main() {
    char a = getchar();
    char b = getchar();
    char c = getchar();
    printf("%d\n", a);
    printf("%d\n", b);
    printf("%d\n", c);
    int sum = a -48 + b - 48 + c - 48;
    printf("%d\n", sum);
    return 0;
}





<div class="se-preview-section-delimiter"></div>

the standard answer





<div class="se-preview-section-delimiter"></div>

#include<stdio.h>




<div class="se-preview-section-delimiter"></div>

#include<stdlib.h>

int main() {
    char charA, charB, charC;
    int intA, intB, intC;
    int sum = 0;
    charA = getchar();
    charB = getchar();
    charC = getchar();
    printf("%d\n", charA);
    printf("%d\n", charB);
    printf("%d\n", charC);
    intA = charA - '0';
    intB = charB - '0';
    intC = charC - '0';
    sum = intA + intB + intC;
    printf("%d\n", sum);
    return 0;
}





<div class="se-preview-section-delimiter"></div>
收获:
将字符0~9从字符转化为int类型,可用原字符减去'0',也可写作原字符减去48

simplify the fraction

Description:
Input the two integer a, b(0 <= a, b <= 10000).which is mean the fraction a/b.
Please output the fraction which is simplified.
output format:
1.there is an “\n” behind the manhatton distance.
2.if input is “6 8” then output “6/8==>3/4” + “\n”
attention:
1. if b is equal to 0, output “Error” + “\n”.
2. if a is equal to 0, output “Zero” + “\n”.
3. if b is equal to 0, no matter what a it is, just output “Error” + “\n”.
4. if input is “10 5” then output “10/5==>2/1” + “\n”
For example[Input]5 10
[Output]5/10==>1/2
Hint:
You can calculate the greatest common divisor(GCD) to simplify the fraction

my answer





<div class="se-preview-section-delimiter"></div>

#include<stdio.h>
int main() {
    int x, y, a, b, c, gcd;
    scanf("%d%d", &x, &y);
    if (y == 0)
    printf("Error\n");
    if (y != 0 && x ==0)
    printf("Zero\n");
    if (y !=0 && x != 0) {
    a = x, b = y;
    if (a < b) {
    c = a, a = b, b = c;
    }
    c = a % b;
    while (c != 0) {
      a = b;
      b = c;
      c = a % b;
    }
    a = x / b; b = y / b;
    printf("%d/%d==>%d/%d\n", x, y, a, b);
    }
    return 0;
}





<div class="se-preview-section-delimiter"></div>

the standard answer





<div class="se-preview-section-delimiter"></div>

#include<stdio.h>
int main() {
    int numerator, denominator, temp, remain;
    int a, b;

    scanf("%d%d", &numerator, &denominator);
    if (denominator == 0) {  //  分母为0
        printf("Error\n");
        return 0;
    }

    if (numerator == 0) {  //  分子为0
        printf("Zero\n");
        return 0;
    }

    //  求最大公约数
    a = numerator;
    b = denominator;

    if (a < b) {
        temp = a;
        a = b;
        b = temp;
    }

    while (a % b != 0) {
        remain = a % b;
        a = b;
        b = remain;
    }
    printf("%d/%d==>", numerator, denominator);
    printf("%d/%d\n", numerator / b, denominator / b);

    return 0;
}





<div class="se-preview-section-delimiter"></div>

反馈

问:"这样写的两个if当分子分母都为零的时候,为什么不会同时输出zero和error"
答:"return 0;表示程序结束"

将要远行

Description:
以撒将要远行。当以撒的妈妈想要杀死以撒时,以撒决定逃跑。以撒的武器,是他的眼泪。好运的是,以撒获得了一个道具,这个道具
使得以撒可以发射出三滴眼泪。只要以撒拥有至少三滴眼泪时,他就会一次性发射三滴眼泪来战斗(如果不够三滴就不发射)。而如果
他把所有的眼泪都发射完毕(也就是剩余0滴眼泪),以撒就会伤心的死去。假设现在以撒拥有N滴眼泪,(以撒的人生非常悲剧,他常
常哭泣,积攒了许多眼泪,所以N非常大,总之longlong肯定是存不下,但不会超过100位数)如果以撒会伤心的死去,输出God,否则,
输出Issac
样例输入1:
5
样例输出1:
Issac
样例输入2:
1234567890987654321
样例输出2:
God
Hint:
这题并不是考高精度
没有思路的同学可以搜索 整除3的特征

my answer





<div class="se-preview-section-delimiter"></div>

#include<stdio.h>
int main() {
    int sum = 0; char x;
    while ((x = getchar()) != '\n') {
    x -= 48;
    sum += x;
    }
    int judge = sum % 3;
    if (judge == 0) {
    printf("God\n");
    } else {
    printf("Issac\n");
    }
    return 0;
}





<div class="se-preview-section-delimiter"></div>

the standard answer





<div class="se-preview-section-delimiter"></div>

#include<stdio.h>




<div class="se-preview-section-delimiter"></div>

#include<string.h>
char s[1100];
int main() {
    scanf("%s", s);
    int n = strlen(s);
    int sum = 0;
    int i;
    for (i = 0; i < n; i++) {
        int id = s[i] - '0';
        sum += id;
    }
    sum %= 3;
    if (sum == 0) {
        printf("God\n");
    } else {
        printf("Issac\n");
    }
    return 0;
}





<div class="se-preview-section-delimiter"></div>

反馈

1.题目中"比long long还大""三整除的特征"暗示要将每位上的数字相加,然后判断是否能被三整除,再联系之前有学过的getchar()的用法,知,只要用个循环,将所有字符转换为int相加,就可以得出各位数之和.
2.循环中如何判断是否结束?
当getchar读到' ' 或'\n'时,结束循环(貌似要用'\n')
3.scanf("%s", strlen(s) s);用%s可以读一串不为空格的字符串,并赋值给数组s[ ]
4.添加string.h头文件后,可以用strlen()计算给定字符串的(unsigned int型)长度,不包括'\0'在内

百钱百鸡(for hw)

Description:
1只公鸡值5文钱;1只母鸡值3文钱;3只小鸡值1文钱。请问用文钱买100只鸡,公鸡、母鸡和小鸡各有几只?实际题目中会按照M文钱买N只鸡的形式
按公鸡母鸡小鸡的顺序分别输出结果,一组解答占一行,解答按照公鸡数目从大到小排序(其次母鸡,再次小鸡)
无解时请输出 no answer
如输入为:
100 100
则输出:
12 4 84
8 11 81
4 18 78
0 25 75
如输入为:1 4
则输出为:no answer

my answer





<div class="se-preview-section-delimiter"></div>

#include <stdio.h>
    int main() {
    int x, y, z, m, n, i;
    int c = 0;
    scanf("%d%d", &m, &n);
    for (x = m/5; x >=0; x--)
      for (y = m/3; y >=0 ; y--)
        for (z = m; z >= 0; z--)
          if (5*x + 3*y + z ==m && x+y+3*z ==n) {
            printf("%d %d %d\n", x, y, 3*z);
            c++;
          }
    if (c == 0) {
      printf("no answer\n");
}
    return 0;
}





<div class="se-preview-section-delimiter"></div>

the standard answer






<div class="se-preview-section-delimiter"></div>

#include<stdio.h>
int main() {
    int a, b, c;
    int money, number, flag;
    scanf("%d %d", &money, &number);
    flag = 0;
    for (a = money/5; a >= 0; a--)
        for (b = money/3; b >= 0; b--)
            for (c = money; c >= 0; c--) {
                if (a + b + c*3 == number && a*5 + b*3 + c == money) {
                        flag++;
                        printf("%d %d %d\n", a, b, c*3);
                    }
            }
    if ( flag == 0 )
        printf("no answer\n");
    return 0;
}





<div class="se-preview-section-delimiter"></div>

反馈

1.这里,由钱的数量可以知道每种鸡最多买多少只,所以可以减少循环的次数
2.小鸡三只1块钱,可以三只看做一组,即将小鸡个数定为3*z,这会简化运算

The second largest number (for hw)

Description:
Write a program that read a number n and n integers (2 <= n <= 100), then print the second largest integers (in the n integers).
Sample Input
5
3 2 1 5 4
Sample Output
4

my answer





<div class="se-preview-section-delimiter"></div>

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





<div class="se-preview-section-delimiter"></div>

the standard answer





<div class="se-preview-section-delimiter"></div>

#include <stdio.h>




<div class="se-preview-section-delimiter"></div>

#include <limits.h>

int main() {
    int n, t, m1 = INT_MIN, m2 = INT_MIN;
    for (scanf("%d", &n); n > 0; --n) {
        scanf("%d", &t);
        if (t > m1) {
            m2 = m1;
            m1 = t;
        }
        else if (t > m2)
            m2 = t;
    }
    printf("%d\n", m2);
    return 0;
}

反馈

小结

本周的练习难度不算大,就是有两题一开始没出好就有点坑哈~
自己看书看了各种循环结构,这里不赘述
嗯嗯加油!



  1. 题目均出自TA
    [^footnote2]:来自百度百科
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值