编程练习 15.11.9~15.11.14

来源:互联网 发布:校园网络广播系统 编辑:程序博客网 时间:2024/06/05 04:35

    • Factorization
      • 读题
      • 我的答案
      • 标准答案
      • 反馈
    • Simple Matrix
      • 读题
      • 我的答案
      • 标准答案
      • 反馈
    • 1009grade 自己加的题
      • 读题
      • 我的答案
      • 标准答案
      • 反馈
    • 神奇的幻方
      • 读题
      • 我的答案
      • 标准答案
      • 反馈
    • 蛇形方阵for hw
      • 读题
      • 我的答案
      • 标准答案
      • 反馈
    • Couplesfor hw
      • 读题
      • 我的答案
      • 标准答案
      • 反馈
    • Find Actual Number of Miles
      • 读题
      • 我的答案
      • 标准答案
      • 反馈
    • qsortfor hw
      • 读题
      • 我的答案
      • 标准答案
      • 反馈

Factorization

Please Input a number n(2 <= n <= 10000), then find it’s all factor(s) and output them(it).
output format:
1.behind every factor, there will be a ’ ‘(space).
2.no ‘\n’ this time.
For example:
[Input]
10
[Output]
2 5
it means that 2+’ ‘5+’ ’
Hint:
涉及到知识点:循环、if、break。
如果n的因数分解为n,就输出n+’ ‘

读题

这个题目是因式分解!!!不是写出它的所有因子.一开始就是这样错了!

我的答案

#include<stdio.h>int main() {    int n, i;    scanf("%d", &n);    for (i = 2 ; i <= n; i++) {        if (n % i == 0) {            printf("%d ", i);            n /= i;            i = 1;        }    }    return 0;}

标准答案

#include <stdio.h>int main() {    int number;    int factor;    scanf("%d", &number);    while (number != 1) {  // 对number分解直到number为1        for (factor = 2; factor <= number; ++factor) {  // 从2开始找能整除的数            if (number % factor == 0) {                printf("%d ", factor);                number /= factor;                break;            }        }    }    return 0;}

反馈

挺简单的.不过当时有一个错误就是:在循环的后面我令i = 2;但由于循环里有i++,所以新一轮循环时i = 3;所以导致错误;后来自己手算了一遍才发现这个问题并及时纠正

Simple Matrix

Given an n*n matrix, try to calculate the sum of the digits on its diagonal line.
给定一个n*n矩阵,计算对角线上数字的和。
n <= 10
Sample Input(样例输入):
4
1 2 3 4
5 6 7 8
9 8 7 6
4 2 8 9
Output(输出):
23

读题

这个题乍一看需要二维数组..其实不用也可以..蛮简单

我的答案

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

标准答案

#include <stdio.h>int main() {    int i, j, n, sum, a[200][200];    sum = 0;    scanf("%d", &n);    for (i = 0; i < n; i++) {        for (j = 0; j < n; j++)            scanf("%d", &a[i][j]);    }    for (i = 0; i < n; i++) {        sum += a[i][i];    }    printf("%d\n", sum);    return 0;}

反馈

嗯.没学到啥,练练手罢了.

[1009]grade (自己加的题)

我们这个教室里面有两个班,假设两个班里分别有n名,m名同学。现在给出每个同学的成绩,请问哪个班的平均成绩分别是多少,哪个班的平均成绩更高。
因为软件学院里面有很多的教务班,所以我们会进行多次询问。
【输入格式】
第一行仅一个整数t(t<10),表示询问的次数。
对于每一次询问,第一行有两个整数n,m,(n,m<100);第二行有n个整数,表示第一个班级n个同学的成绩;第三行有m个整数,表示第二个班级m个同学的成绩。(0<=成绩<=100)
【输出格式】
共2t行。对于每一次询问,输出两行,第一行有两个小数,分别表示两个班的平均成绩(保留两位小数),中间用一个空格隔开。第二行输出一个单词,如果第一个班级的平均成绩高于第二个班级,则输出“More”,如果两个班级平均成绩相等,则输出“Equal”,如果第一个班级的平均成绩小于第二个,则输出“Less”。

注意,比较两个班成绩大小的时候,我们比较的是它的真实值而不是保留两位小数以后的值,比如说1/3和0.33虽然保留两位小数以后都是0.33,但是我们判断1/3>0.33。
【输入样例】
3
3 5
100 100 100
1 2 3 4 5
2 3
100 96
99 98 97
3 3
1 1 2
1 1 3
【输出样例】
100.00 3.00
More
98.00 98.00
Equal
1.33 1.67
Less

读题

我的答案

#include<stdio.h>int main() {    int t, i, j, k, n, m, x;    float sum1[103] = {0};    float sum2[103] = {0};    float average1[11], average2[11];    scanf("%d", &t);    for (i = 0; i < t; i++) {        scanf("%d%d", &n, &m);        for (j = 0; j < n; j++) {            scanf("%d", &x);            sum1[i] += x;        }        for (k = 0; k < m; k++) {            scanf("%d", &x);            sum2[i] += x;        }        average1[i] = sum1[i] / n;        average2[i] = sum2[i] / m;    }    for (i = 0; i < t; i++) {        printf("%.2f %.2f\n", average1[i], average2[i]);        if (average1[i] == average2[i]) {            printf("Equal\n");        } else if (average1[i] > average2[i]) {            printf("More\n");        } else if (average1[i] < average2[i]) {            printf("Less\n");        }    }    return 0;}

标准答案

98分..精度问题..没有得到标准答案

反馈

挺简单的.

神奇的幻方

【问题描述】
幻方是一种很神奇的N*N矩阵:它由数字 1,2,3,……,N*N构成,且每行、每列及两条对角线上的数字之和都相同。
当N为奇数时,我们可以通过以下方法构建一个幻方:
首先将1写在第一行的中间。
之后,按如下方式从小到大依次填写每个数K(K=2,3,…,N*N) :
1. 若 (K−1) 在第一行但不在最后一列,则将K填在最后一行,(K−1)所在列的右一列;
2. 若 (K−1) 在最后一列但不在第一行,则将K填在第一列,(K−1)所在行的上一行;
3. 若 (K−1) 在第一行最后一列,则将K 填在(K−1) 的正下方;
4. 若 (K−1) 既不在第一行,也不在最后一列,如果(K−1) 的右上方还未填数,则将K填在(K−1)的右上方,否则将K填在 (K−1) 的正下方。
现给定N,请按上述方法构造N*N的幻方。
【输入格式】
输入只有一行,包含一个整数N N不超过500 且为奇数
【输出格式】
输出文件包含N行,每行N个整数,即按上述方法构造出的N*N的幻方。
左上角为(1,1) 右下角为(N,N)
相邻两个整数之间用单个空格隔开。
行末没有空格
输入样例
3
输出样例
8 1 6
3 5 7
4 9 2
Hint:
题目来源:noip2015 day1 T1
稍有修改
因之前数据过大,评测系统无法正常出解结果
现在把数据改小,不超过100.为了能够有100组随机数据,增加一个标识。即:随机数据中会出现第二个数但是你们不需要理会,只需要读入两个数,然后使用第一个数N,第二个数忽略
例如随机数据中出现
5
3
那么你读入5 3后 此时,N=5 3不需要理会。

读题

一开始还是有点懵,但是后来呢,发现,判断条件其实挺单一:是否为第一行与是否为最后一列,只要对此作出判断,再对新的数的xy作出调整,然后对其赋值就好了

我的答案

#include<stdio.h>int main() {    int a[110][110] = {0};    int n, i, j;    scanf("%d", &n);    int x = 1;    int y = (n + 1) / 2;    a[x][y] = 1;    for (i = 2; i <= n * n; i++) {        if (x ==1 && y != n) {            x = n; y++;        } else if (x != 1 && y == n) {            x--; y = 1;        } else if (x == 1 && y == n) {            x++;        } else if (x != 1 && y != n) {            if (a[x - 1][y + 1] == 0) {                x--; y++;            } else {                x++;            }        }        a[x][y] = i;    }    for (i = 1; i <= n; i++) {        for (j = 1; j < n; j++) {            printf("%d ", a[i][j]);        }        printf("%d\n", a[i][j]);    }    return 0;}

标准答案

#include<stdio.h>.int a[510][510] = {};int main() {    int x, y;    int n;    int dx, dy;    int i, j;    scanf("%d", &n);    x = 1;    y = n/2+1;    a[x][y] = 1;    for (i = 2; i <= n * n; i++) {        if (x == 1 && y != n) {           dx = n;            dy = y + 1;        }        if (y == n && x != 1) {            dx = x - 1;            dy = 1;    }        if (x == 1 && y == n) {            dx = x + 1;            dy = n;        }        if (x != 1 && y != n) {            if (a[x - 1][y +1] == 0) {                dx = x - 1;                dy = y + 1;            } else {        dx = x + 1;                dy = y;            }        }        x = dx;        y = dy;        a[x][y] = i;    }    for (i = 1; i <= n; i++) {        for (j = 1; j < n; j++) {            printf("%d ", a[i][j]);        }        printf("%d\n", a[i][n]);    }    return 0;}

反馈

蛇形方阵(for hw)

Erin最近学习了数组,她想通过数组实现一个蛇形方阵的打印,你可以帮她实现这个程序吗?
input:整数n(2

读题

这个题挺好理解的.
把矩阵排好,然后按行输出即可

我的答案

#include<stdio.h>int main() {    int a[110][110] = {0};    int n, b, k, i, j, r, x, y;    scanf("%d", &n);    r = n / 2 + n % 2;  // r为方针的圈数    i = 1;    for (k = 1; k <= r; k++) {        b = n - k + 1;  // b为转折点之一        x = k; y = b;            for (; x != b; x++, i++)                a[x][y] = i;            for (; y != k; y--, i++)                a[x][y] = i;            for (; x != k; x--, i++)                a[x][y] = i;            for (; y != b; y++, i++)                a[x][y] = i;        }    if (n % 2)    a[r][r] = n * n;    for (i = 1; i < n; i++) {        for (j = 1; j < n; j++) {            printf("%d ", a[i][j]);        }        printf("%d\n", a[i][j]);    }    for (j = 1; j < n; j++)    printf("%d ", a[i][j]);    printf("%d", a[i][j]);    return 0;}

标准答案

#include<stdio.h>int main() {    int n;    scanf("%d", &n);    int i, j, k;    int arr[10005];    for (i = 0; i < 10005; i++) {        arr[i] = i;    }    int result[105][105];    for (i = 0; i < 105; i++) {        for (j = 0; j < 105; j++)            result[i][j] = 0;    }    j = 1;    k = n;    int flag = 1;    for (i = 1; i <= n*n; i++) {        result[j][k] = i;        if (flag == 1) {            if (j == n) {                flag = 2;                k--;            }            else if (result[j+1][k] == 0)                j++;            else if (result[j+1][k] != 0) {                flag = 2;                k--;            }        }        else if (flag == 2) {            if (k == 1) {                flag = 3;                j--;            }            else if (result[j][k-1] == 0)                k--;            else if (result[j][k-1] != 0) {                flag = 3;                j--; }        }        else if (flag == 3) {            if (j == 1) {                flag = 4;                k++;            }        else if (result[j-1][k] == 0)                j--;        else if (result[j-1][k] != 0) {                flag = 4;                k++;}        }        else if (flag == 4) {            if (k == n) {                flag = 1;                j++;            }            else if (result[j][k+1] == 0)                k++;            else if (result[j][k+1] != 0) {                flag = 1;                j++;            }        }    }    for (j = 1; j <= n-1; j++) {        for (k = 1; k <= n-1; k++) {            printf("%d ", result[j][k]);        }        printf("%d\n", result[j][k]);    }    for (k = 1; k <= n-1; k++) {        printf("%d ", result[j][k]);    }    printf("%d", result[j][k]);    return 0;}

反馈

Couples(for hw)

Description
N couples are standing in a circle, numbered consecutively clockwise from 1 to 2N. Husband and wife do not always stand together. We remove the couples who stand together until the circle is empty or we can’t remove a couple any more.
Can we remove all the couples out of the circle?
Input
There may be several test cases in the input file. In each case, the first line is an integer N(1 <= N <= 100000)—-the number of couples. In the following N lines, each line contains two integers —- the numbers of each couple.
N = 0 indicates the end of the input.
Output
Output “Yes” if we can remove all the couples out of the circle. Otherwise, output “No”.
Sample Input
4
1 4
2 3
5 6
7 8
2
1 3
2 4
0
Sample Output
Yes
No
Hint:
题目来源:http://soj.sysu.edu.cn/show_problem.php?pid=1021

读题

要用到栈/

我的答案

标准答案

反馈

Find Actual Number of Miles

You are given a car odometer which displays the miles traveled as an integer. The odometer has a defect, however: it proceeds from the digit 2 to the digit 4 and from the digit 7 to the digit 9, always skipping over the digit 3 and 8. This defect shows up in all positions (the one’s, the ten’s, the hundred’s, etc.). For example, if the odometer displays 15229 and the car travels one mile, odometer reading changes to 15240 (instead of 15230).
Input
Each line of input contains a positive integer in the range 1..999999999 which represents an odometer reading. (Leading zeros will not appear in the input.) The end of input is indicated by a line containing a single 0. You may assume that no odometer reading will contain the digit 3 and 8.
Output
Each line of input will produce exactly one line of output, which will contain: the odometer reading from the input, a colon, one blank space, and the actual number of miles traveled by the car.
Sample Input
15
250
0
Sample Output
15: 12
250: 160

读题

题目意思是:给你一个里程表读数;这个里程表有个缺点,会跳过3和8两个数字,在任何数位都会;我们的任务是输出它的实际公里数
开始我是从1到这个数,计算它有多少个含3和8的数;再减去
后来发现这个方法太麻烦;超时

实际上这是一个伪八进制转化为10进制的算法
因为:10进制是10个数字进一,而现在因为跳过了3和8,所以是8个数字进一,也即里程表的显示是八进制.
另外,由于跳过3,所以4实际上是3,5实际上是4,类推,到9,9实际上是7.

我的答案

#include<stdio.h>#include<math.h>int main() {    int sum, a, temp;    sum = 0;    for (scanf("%d", &a); a != 0; scanf("%d", &a)) {        int mod = a, temp = a;        for (int i = 0; temp != 0; i++) {            mod = temp % 10;            if (mod == 0 || mod == 1 || mod == 2)            sum += mod * pow(8, i);            else if (mod == 9)            sum += (mod - 2) * pow(8, i);            else            sum += (mod - 1) * pow(8, i);            temp /= 10;        }        printf("%d: %d\n", a, sum);        sum = 0;    }    return 0;}

标准答案

/* *author: younglee. */#include<stdio.h>int dealWith(int n) {    int arr[15], count = 0, i, res = 0;    while (n > 0) {        arr[count++] = n%10;        n /= 10;    }    for (i = 0; i < count; i++) {        if (arr[i] >= 9) {            arr[i] -= 2;        } else {            if (arr[i] >= 4) arr[i]--;        }    }    for (i = count - 1; i >= 0; i--) {        res *= 8;        res += arr[i];    }    return res;}int main(void) {    int num;    while (scanf("%d", &num) && num != 0) {        int realNum = dealWith(num);        printf("%d: %d\n", num, realNum);    }    return 0;}

反馈

用数组做的时候我老是做错;后来直接用一个数取模取余来做才行;还要加强对数组的学习!!!

qsort(for hw)

使用C语言自带的排序算法qsort()进行整数排序。

qsort的原型是void qsort(void*base,size_t num,size_t width,int(__cdecl*compare)(const void*,const void*))。
qsort函数所接收的参数列表:

1、base代表的是要被排序的数组的首地址,如无特殊要求,在使用的时候直接将数组名代入即可。

2、num代表的是数组中要被排序的元素的个数

3、width代表的是被排序元素类型的长度,通常情况下使用sizeof()函数,如sizeof(base[0]);

4、比较函数,如何比较两个元素的大小,因为还没有学指针等内容,这道题目中已经准备好了compare函数在头文件中,不用同学们自己定义。

输入:两行数字;第一行一个数字N(0

读题

如题.
compare函数的文件

#ifndef __COMPAREFORINT__#define __COMPAREFORINT__#include<stdio.h>#include<stdlib.h>#include<string.h>#include<iostream>int compare(const void* a, const void* b) {    const int* c = reinterpret_cast<const int*>(a);    const int* d = reinterpret_cast<const int*>(b);    return *c - *d;}#endif

我的答案

#include<stdio.h>#include<stdlib.h>#include<string.h>#include"compare.h"int main() {    int n, i;    int a[30];    scanf("%d", &n);    for (i = 0; i < n; i++)    scanf("%d", &a[i]);    int width = sizeof(a[0]);    qsort(a, n, width, compare);    for (i = 0; i < n; i++)    printf("%d ", a[i]);    return 0;}

标准答案

#include<stdio.h>#include<stdlib.h>#include<string.h>#include"compare.h"#define MAX 20int main() {    int length = 0;    int array[MAX];    int i;    scanf("%d", &length);    for (i = 0; i < length; i++) {        scanf("%d", &array[i]);    }    qsort(array, length, sizeof(array[0]), compare);    for (i = 0; i < length; i++) {        printf("%d ", array[i]);    }    return 0;}

反馈

说实话,在这个题我并没有学到什么东西.    
0 0
原创粉丝点击