一只小白的代码之路 15.9.1~15.10.18


本人学院

目录

说在前头的废话

可爱的导论老师给咱们开启了markdown的大门,建议咱们在博客记录自己的学习,以便日后在介绍自己时可以任性地甩给对方一个地址,于是就有了这个博客啦 (鼓掌撒花)既然如此,我就只好一脸严肃地写写这段时间的程设学习了~

1. 入门

程序员们的第一个程序 Hello World!

#include<iostream>
using namespace std;

int main(){
    cout<<"hello world!"<<endl;
    return 0;
}

运行后的结果是这样的:
helloworld

网络图片1
运行成功的时候好激动啊(≧▽≦)/然而其实这只是照着别人给出的代码打的,
自己都不明白这几行字神马意思啊啊啊O(≧口≦)O

后来去听了几节hellofreshman新生第一课,还是听不懂啊啊啊

课文习题

输入三个数,分别输出它们的和,平均数,乘积,最大值和最小值

#include <stdio.h>
int main(void)
{
    int x,y,z,sum,average,product;

    printf("input three different integers:" );
    scanf("%d%d%d" , &x,&y,&z);

    sum=x+y+z ;
    average=sum/3 ;
    product=x*y*z ;

    printf("sum is %d \n" ,sum );
    printf("average is %d \n" , average );
    printf("product is %d \n" , product );

    if(x > y && x > z ){
        printf("biggest is %d \n", x);
    }

    if(y > x && y > z ){
        printf("biggest is %d \n", y);
    }

    if(z > x && z > y ){
        printf("biggest is %d \n", z);
    }

    if(x < y && x < z ){
        printf("smallest is %d \n", x);
    }

    if(y < x && y < z ){
        printf("smallest is %d \n", y);
    }

    if(z < y && z < x ){
        printf("smallest is %d \n", z);
    }

return 0;

} 

当时只看了一点课文内容,只会用简单的语句来陈述,所以写得超长.

2.程设实验课(programming)2

简单版hello world!

#include <stdio.h>
    int main() {    
    int number;
    printf("\nPlease input a number: ");
    scanf("%d", &number);
    printf("Hello World: %d", number);
    return 0;
} 

程设实验课上,TA们给咱们布置了新的作业——简单的hello world,此时通过看书刚学会printf用法的我表示微微的窃喜,然而问题又来了——Google style ,这个坑爹的东西让我修改了好几次才提交到满分
目前已知:
1.每行前面空四格
2.最后留一个空白行
3.int main()后空一格再接{

Relationship between two numbers

#include <stdio.h>
int main() {
    int a, b;
    scanf("%d %d", &a, &b);
    if (a < b) {
    printf("%d is less than %d\n", a, b);
} 
    if (a > b) { 
    printf("%d is greater than %d\n", a, b);
}
    if (a == b) {
    printf("%d is equal to %d\n", a, b); 
}   
    return 0;
}

*Google style:
if()后空一格直接接上{*

Double it

输入一个数,输出这个数的两倍

#include <stdio.h> 
int main() {
    int m, n;
    scanf("%d", &n);
    m = 2 * n;
    printf("%d\n", m);
    return 0;
}

EnTaroTassadar

著名的暴雪电影制片厂不久前推出了他的新作:电影虚空之遗附带的同名游戏的前三关战役。游戏的第一关中,一些星灵战士被莫比斯俘虏了。而你(泽拉图)要去解救他们。解救的方法就是生产一些士兵,然后打败敌人。生产一个士兵,需要花费晶体矿,瓦斯气矿,并且占据一定的补给。
现在,给出你拥有的晶体矿、瓦斯气矿、补给,以及想要生产的士兵的花费。问最多可以生产出多少士兵。
输入
第一行给出三个整数,a, b, c (1 <= a , b , c <= 10^5)
第二行给出三个整数,d, e, f (1 <= d ,e , f <= 10^5)
输出
输出一行,包括一个整数,即可以生产的士兵总数

样例输入
5 8 3
2 3 3
样例输出
1

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

当时用else if时出了问题,只好乖乖的用回最最简单的if

Four value sorting

将输入的四个数从小到大输出

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

判断是不是素数

#include<stdio.h>
int main() {
    int a, x, i, y;

    y = 1;
    scanf("%d", &a);
    if (a == 0 || a == 1 ) {
        printf("Invalid Input Number!");
    } else {
        for (i = 2; i < a; i++ ){
        x = a % i;
        if (x == 0){
            y = 0;
} 
            break;  
        } 
    if (y == 0) {
        printf("It is not Prime Number!");
    }
    if (y == 1) {
        printf("It is  Prime Number!");
    }
}
    return 0;
}

这是修改版本,当时不会用 || 表示或。

简单几何题

输入三个数,判断以这三个数为边长能否组成三角形,若不能,输出“this is not a triangle!” 若可以,求出三角形的周长与面积
hint:面积可用海伦公式求:

S=(p(pa)(pb)(pc))

公式中a,b,c分别为三角形三边长,p为半周长,S为三角形的面积。

#include<stdio.h>
#include<math.h>
int main() {
    int a, b, c, x;
    scanf("%d%d%d", &a, &b, &c);
    if (a > c) {
        x = a, a = c, c = x;
    }
    if (b > c) {
        x = b, b = c, c = x;
    }
    if (a + b > c) {
       float perimeter = a + b + c;
       float p = perimeter/2;
       float area = sqrt(p * (p - a) * (p - b) * (p - c) );
        printf("the perimeter of this triangle is:%.6f!\n", perimeter);
        printf("the area of this triangle is:%.6f!\n", area);
    }
    if (a + b <= c) {
        printf("this is not a triangle!\n");
    }
    return 0;
}

*新技能:求算数平方根
sqrt()
(使用要添加math头文件 #include

Manhattan distance

Description:
Input four integer x1, y1, x2, y2, which is mean that the coordinates of two points A(x1, y1), B(x2, y2). [0 <= x1,y1,x2,y2 <= 10000]
Please output manhatton distance between the two points.
output format:there is an “\n” behind the manhatton distance.
For example
[Input]0 0 1 1
[Output]2
Hint:
Manhattan distance, considered by Hermann Minkowski in 19th century Germany, is a form of geometry in which the usual distance function of metric or Euclidean geometry is replaced by a new metric in which the distance between two points is the sum of the absolute differences of their Cartesian coordinates. ——Find in Wiki
d(i,j)=|X1-X2|+|Y1-Y2|.

if you want to use the abs() function to calculate the absolute value, you should include the stdlib.h head file.
You also can compare X1 and X2.And then use the big value to minus the small value.

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

PS:仍未掌握 abs() function 用法

ranges of some data types

#include<stdio.h>
#include<limits.h>
int main() {
    printf("size of char is %d\n", CHAR_BIT);
    printf("char max is %d\n", CHAR_MAX);
    printf("char min is %d\n", CHAR_MIN);
    printf("int min is %d\n", INT_MIN);
    printf("int max is %d\n", INT_MAX);
    printf("long min is %ld\n", LONG_MIN);
    printf("long max is %ld\n", LONG_MAX);
    printf("short min is %d\n", SHRT_MIN);
    printf("short max is %ld\n", SHRT_MAX);
    printf("unsigned char max is %d\n", UCHAR_MAX);
    printf("unsigned long max is %lu\n", ULONG_MAX);
    printf("unsigned int max is %ld\n", UINT_MAX);
    printf("unsigned short max is %d\n", USHRT_MAX);
    return 0;
}

*这个一开始完全不懂,后来去查库文件,才豁然开朗。
这时还有一个问题,有些变量数值超出范围,需要用到ld,lu (对于这个我仍处于半懂状态)*

Square or Circle

Now you have a rope with a length of C (may not be an integer).
You can form a circle or a square with the rope.
How much is the circle’s area larger than the square’s?
Sample Input 4
Sample Output 0.27
Hint: PI取3.1415927
结果保留小数点后2位

#include<stdio.h>
int main() {
    float c, r, s1, s2, x;
    float pi = 3.1415927;
    scanf("%f", &c);
    r = c / (2 * pi);
    s1 = pi * r * r;
    s2 = (c / 4) * (c / 4);
    x = s1 - s2;
    printf("%0.2f\n", x);
    return 0;
}

*新技能:保留两位小数:
对于浮点数,在输出时,“%.2f” 即可*

时光法

在联机的回合制游戏中,往往都会设置一个时限,超出时限则自动结束回合。而在当下流行的一款游戏中则有这一机制:你在回合中的每一个操作都会产生一个动画效果,后一个动画效果会在前一个结束后开始,例如你进行了4次操作,每次操作都会产生一个3秒的动画效果,那么总消耗时间就为12秒。如果在你的回合结束时,你完成了若干项操作,但动画效果并没有播放完毕,动画会继续播放,但同时会消耗你对手这回合的时间。例如,回合时限为15秒,你回合结束后又继续播放了8秒的动画,那么对手这一回合就只剩下了7秒。
现在问题来了:假如你当前生命值为N点,回合时间总长为M秒,你的对手小安每一秒可以完成一个造成a点伤害的操作,那么你回合结束时最少需要剩下一个多少秒的动画才能使得对手没办法一回合消灭你。(生命值变为小于等于0时,即被消灭)
保证给出的你的生命值大于0且小于200 时限M大于0且小于50 伤害a大于0 且 小于20
输入:三个数N M a
输出:一个数x,即需要剩下一个x秒的动画。
样例读入10 5 5
样例输出4
Hint:
如果不需要剩下没有播放完的动画则认为是剩下0秒的动画。不会剩下负数秒的动画

#include<stdio.h>
int main() {
    int N, M, a, t, x, t2, y;
    scanf("%d%d%d", &N, &M, &a);
    y = N % a;
    if (y == 0) {
    x = N / a;
}
    if (y != 0) {
    x = N / a + 1;
}
    t2 = x - 1;
    t = M - t2;
    if (t >= 0) {
    printf("%d\n", t);
}
    if (t < 0) {
    printf("0\n");
}
    return 0;
}

感受:先慢慢理解好题意,在草稿上想好一些变量的关系,想好大致程序,再打码,会简单许多。

triangle

Input three integer numbers in one line, separated by blank space;
If these three numbers cannot form a triangle, output “not”;
If the triangle is equilateral, output “equilateral”;
If the triangle is isosceles, output “isosceles”;
If the triangle is just a normal one, output “normal”.
Sample:
Input:2 3 4
Output:normal

#include<stdio.h>
int main() {
    int a, b, c, x;
    scanf("%d%d%d", &a, &b, &c);
    if (a > c) {
        x = a, a = c, c = x;
    }
    if (b > c) {
        x = b, b = c, c = x;
    }
    if (a + b > c) {
    if (a == b && b == c) {
    printf("equilateral");
    }
    else if (a == b || b == c || a == c) {
    printf("isosceles");
    } else {
    printf("normal");
    }
    }
    if (a + b <= c) {
    printf("not");
    }
    return 0;
}

3.程设实验课(explaining)

purple cows (for lab)

•Jeremy lives in the countryside, and there are lots of cows in the village.
•Most of the cows are white or black, but one day Jeremy notices that there is a purple cow, which astonished him immediately. So he plan to count numbers of all cows, to calculate how many cows there are.
•Input will contain n+1 numbers, the first one “n” means how many cows in total, and following n numbers represent different kinds of cows. 1 for while, 2 for black, and 3 for the purple one.
•Please write down the algorithm or code to show how you solve this question.

•The in put will be like this:
•5
•1
•2
•2
•3
•1

•Then the output should be:
•2 2 1

答案:
一:将输入的第一个变量定义为a
二:定义变量w,b,p,
三:将输入的下一个数定义为c
四: 若c=2,则b+1
若c=3,则p+1
五:a-1
六:若a>0,则重复三四五步
否则输出w,b,p

Remark from 何翔* (TA)
good.好像少了计算w吧,不过大致意思正确就行

修改:四:若c = 1, 则 w+1

Exchange the drink

一:定义可乐杯为a,橙汁杯为b,定义另一个杯子为c
二:将a杯中的可乐倒进c中 (c=a)
三:将b杯中的橙汁倒入a中 (a=b)
四:将c杯中的可乐倒进b中 (b=c)
五:结束

Remark from 何翔* (TA)
good

水仙花数

打印出100到1000(包括100、不包括1000)中的所有的水仙花数。

水仙花数是自幂数的一种,是指一个 n 位数 ( n≥3 ),它的每个位上的数字的 n 次幂之和等于它本身。(例如:1^3 + 5^3+ 3^3 = 153)。

一 令X=100
二 将X的个位数字赋给A,十位数字赋给B,百位数字赋给C
三:若X == A * A * A + B * B * B + C * C * C,则输出X
四,X = X + 1,若X小于1000,则重复二三步,否则,结束。

Top K

现有N个无序的数字,找出其中前K个最大的数,K<=N。
例如N=5{8,6,7,2,9},前2个最大的就是K=2{9,8}。
给出解题思路,最好举例说明你的解题思路。
(扩展)有解题思路的同学可以想想是否有更少的比较两数字大小次数的方法解决此问题。
一:第一个来的数字放在第一位
二:下一个来的数字若比前一个数字大,则与前一个数字交换位置,否则跟在数组的最后
三:对于前K个数字,重复第二步
四:对接下来的数字,重复第二步,然后删去最后一个数,直到N个数字全部来完。
例如题中例子N=5{8,6,7,2,9},K=2
{8}→{8,6}→{8,7,6}→{8,7}→{8,7,2}→{8,7}→{9,8,7}→{9,8}

BinaryNumber

给出一个十进制下的正实数N,将它转换成二进制形式。请描述做法。
假设在二进制形式下是有限小数
一:将N的整数部分赋给X,小数部分赋给Y
二:A = X % 2,
三:X = X / 2;
四:若X > 0,重复二三步,否则将第二步中得到的A从右到左输出即为二进制下的整数部分
五:Y = Y * 2
六:取Y的整数部分
七:去掉Y 的整数部分,若Y大于0,重复五六七步
八:否则,将第六步的Y从左到右依次写在小数点右边,即得二进制下的小数部分

如何判断一个数是不是素数

素数(prime number)又称质数,有无限个。一个大于1的自然数,除了1和它本身外,不能被其他自然数整除,换句话说就是该数除了1和它本身以外不再有其他的因数;否则称为合数。试给出一个算法,判断一个整数是否为素数?
Hint: 请根据素数的定义来设计算法。
一:将这个数赋给A
二:若A == 2,输出这个数是素数
三:取t = 2,n = A的平方根
四:若A不等于2,令x = A % t,若x == 0,输出这个数不是素数
五:若x不等于零,t = t +1,重复第四步,直到t>n,则输出这个数是素数

判断一个数是否为素数,只要除到该数的平方根就可以结束了

LCM

Given two integers A and B, describe how you will get their LCM(Least Common Multiple).
一:定义A,B,X,a,b且令a=A,b=B
二:若a>b,则a=a-b
若a<b,则b=b-a
三:若a=!b,重复第二步
四:若a==b,X=A*B/a
五:输出X,结束

以上。在这段时间的学习中整个人基本处于迷茫状态。一直在想“这是什么鬼”“不懂啊救命”“到底错哪儿了”,但愿今后,我能贯彻落实多读书多看报,少吃零食多睡觉(怎么可能)的方针,从一个小白,长成一个大白,哦不,(严肃!)每天都有所进步,直至最后……(此处留白)


  1. 不能上传自己的图片心好塞。
  2. 题目出自TA 张楚涵 何翔 彭勃 ,下同
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值