哈工大 sse C语言 困难

Q1892.(10分数, 语言: C)Two Bags of Potatoes
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Valera had two bags of potatoes, the first of these bags contains x (x≥1)
potatoes, and the second — y (y≥1) potatoes. Valera — very scattered
boy, so the first bag of potatoes (it contains x potatoes) Valera lost.
Valera remembers that the total amount of potatoes (x+y) in the two bags,
firstly, was not gerater than n, and, secondly, was divisible by k.
Help Valera to determine how many potatoes could be in the first bag.
Print all such possible numbers in ascending order.
Input
The first line of input contains three integers y, k, n (1≤y,k,n≤10^9;
n/k≤10^5).
Output
Print the list of whitespace-separated integers — all possible values
of x in ascending order. You should print each possible value of x exactly
once. If there are no such values of x print a single integer -1.

中文翻译:

题目描述

瓦莱拉有两个装满土豆的袋子。第一个袋子里有x(x≥1)个土豆,第二个袋子里有y(y≥1)个土豆。瓦莱拉是一个非常粗心大意的男孩,所以他丢失了第一个袋子(里面装有x个土豆)。

瓦莱拉记得,这两个袋子里的土豆总数(x+y)首先不会超过n,其次这个总数能被k整除。

请帮助瓦莱拉确定第一个袋子里可能有多少个土豆。按照升序打印出所有可能的数字。

输入

输入的第一行包含三个整数y, k, n(1≤y,k,n≤10^9;n/k≤10^5)。

输出

打印一个由空格分隔的整数列表——所有可能的x值,按照升序排列。每个可能的x值应该只打印一次。如果没有这样的x值,打印单个整数-1。

题目分析

这道题目要求我们找到所有可能的x值,满足条件:x + y ≤ n 且 (x + y) % k == 0。

我们可以遍历所有可能的x值,从1开始,直到n - y(因为x + y ≤ n),检查每个x值是否满足第二个条件 (x + y) % k == 0。如果满足,则打印该x值。

如果遍历完所有可能的x值后都没有找到满足条件的x,则打印-1。

#include <stdio.h>  
  
int main() {  
    long long y, k, n;  
    scanf("%lld %lld %lld", &y, &k, &n);  
    int found = 0; // 标记是否找到满足条件的x  
    for (long long x = 1; x <= n - y; ++x) {  
        if ((x + y) % k == 0) {  
            printf("%lld ", x);  
            found = 1;  
        }  
    }  
    if (!found) {  
        printf("-1");  
    }  
    return 0;  
}
Q1331.(10分数, 语言: C)将一个链表按逆序排列,即将链头当链尾,链尾当链头。
程序的运行示例如下:
请输入链表(非数表示结束)
结点值:3
结点值:4
结点值:5
结点值:6
结点值:7
结点值:end
原来表:
   3   4   5   6   7

反转表:
   7   6   5   4   3

 链表用数组完成

#include<stdio.h>
#define N 100
void main(){
    int a[N];
    int i=-1;
    int x;
    printf("\n请输入链表(非数表示结束)\n");
    do{
        i++;
        printf("结点值:");
        x = scanf("%d",&a[i]);
    }while(x==1);
    i--;
    printf("\n原来表:\n");
    for(int j=0;j<=i;j++){
        printf("%4d",a[j]);
    }
    for(int j=0;j<=i/2;j++){
        int term = a[j];
        a[j] = a[i-j];
        a[i-j] = term;
    }
    
    printf("\n\n反转表:\n");
    for(int j=0;j<=i;j++){
        printf("%4d",a[j]);
    }

}
Q378.(10分数, 语言: C)

*汉诺塔问题是一个著名的问题,初始模型如图所示。其来源据说是在约19世纪末欧洲的商店中出售一种智力玩具,在一块铜板上有三根杆,最左边的杆自上而下、由小到大顺序串着64个圆盘构成的塔,游戏的目的是将最左边A杆上的圆盘,借助最右边的C杆,全部移动到中间的B杆上,条件是一次仅能移动一个盘,且不允许大盘放在小盘的上面。

**输入格式要求:"%d" 提示信息:"Please enter the number of discs:"

**输出格式要求:"\tTotal:%d\n"    "%2d-(%2d):%c==>%c\n" 

if (n == 1) {  
        (*count)++;
        printf("%2d-(%2d):%c==>%c\n" , *count, n, from, to); 
        return;  
    }  
    hanoi(n - 1, from, aux, to,count);  
    (*count)++;
    printf("%2d-(%2d):%c==>%c\n", *count, n, from, to);  
    hanoi(n - 1, aux, to, from,count);  

#include <stdio.h>  
 
void hanoi(int n, char from, char to, char aux,int *count) {  
    if (n == 1) {  
        (*count)++;
        printf("%2d-(%2d):%c==>%c\n" , *count, n, from, to); 
        return;  
    }  
    hanoi(n - 1, from, aux, to,count);  
    (*count)++;
    printf("%2d-(%2d):%c==>%c\n", *count, n, from, to);  
    hanoi(n - 1, aux, to, from,count);  
}  
  
int main() {  
    int n;
    int count=0; 
    printf("Please enter the number of discs:");  
    scanf("%d", &n);  
    // 从a杆移动到b杆,使用c杆作为辅助  
    hanoi(n, 'a', 'b', 'c',&count);  
    // 输出总步数  
    printf("\tTotal:%d\n", count); 
  
    return 0;  
}
Q3079.(10分数, 语言: C)猴子吃桃程序_扩展2
猴子第一天摘了若干个桃子,吃了一半,不过瘾,又多吃了1个。第二天早上将剩余的桃子又吃掉一半,并且又多吃了1个。此后每天都是吃掉前一天剩下的一半零一个。到第n天再想吃时,发现只剩下1个桃子,问第一天它摘了多少桃子?为了加强交互性,由用户输入不同的天数n进行递推,即假设第n天的桃子数为1。同时还要增加对用户输入数据的合法性验证(如:不允许输入的天数是0和负数)

程序运行结果示例:
Input days:
0↙
Input days:
-5↙
Input days:
a↙
Input days:
3↙
x=10

输入格式:"%d"
输出格式:
输入提示信息:"Input days:\n"
输出:"x=%d\n"

// 如果读取失败,则清除输入缓冲区  
            while (getchar() != '\n');  

#include <stdio.h>  
  
int main() { // main 函数应该有返回类型 int  
    int n;  
    do {  
        printf("Input days:\n");  
        int success = scanf("%d", &n); // 检查scanf的返回值  
        if (success != 1) {  
            // 如果读取失败,则清除输入缓冲区  
            while (getchar() != '\n');  
        }  
    } while (n <= 0);  
  
    int sum = 1;  
    for (int i = 0; i < n-1; i++) {  
        sum = (sum + 1) * 2;  
    }  
    printf("x=%d\n", sum); // 输出变量名应该和变量定义保持一致  
  
    return 0; // main 函数应返回0表示程序正常结束  
}
Q1313.(10分数, 语言: C)找出一个二维数组中的鞍点,即该位置上的元素在该行最大,在该列上最小。也可能没有鞍点。
**输入数据格式:
"\n输入行数:"
"%d"
"\n输入列数:"
"%d"
"第%d行?\n"
"%d"
**输出格式要求:
"%5d"
"\n第%d行,第%d列的%d是鞍点\n"
"\n矩阵中无鞍点!\n"
程序的运行示例1如下:
输入行数:3
输入列数:3
第0行?
1 2 3
第1行?
4 5 6
第2行?
7 8 9
    1    2    3
    4    5    6
    7    8    9

第0行,第2列的3是鞍点
程序的运行示例2如下:
输入行数:2
输入列数:2
第0行?
1 2
第1行?
4 1
    1    2
    4    1

矩阵中无鞍点!

#include <stdio.h>
#include <stdlib.h>

#define M_ROW 3
#define N_COL 3

int main(){
    int M,N;
	int matrix[M_ROW][N_COL];
	printf("\n输入行数:");
    scanf("%d",&M);
    printf("\n输入列数:");
    scanf("%d",&N);
	for(int i = 0; i < M; i++){
        printf("第%d行?\n",i);
		for(int j = 0; j < N; j++){
			scanf("%d", &matrix[i][j]);
		}
	}

    for(int i = 0; i < M; i++){
        for(int j = 0; j < N; j++){
            printf("%5d",matrix[i][j]);
        }
        printf("\n");
    }
	int sign = 0;
	int row, col, maxRow, colMin;
	for(int i = 0; i < M; i++){
		maxRow = matrix[i][0];
		row = i;
		col = 0;
		for(int j = 0; j < N; j++){
			if(matrix[i][j] > maxRow){
				maxRow = matrix[i][j];
				row = i;
				col = j;
			}
		}

		colMin = maxRow;
		for(int k = 0; k < M; k++){
			if(matrix[k][col] < colMin){
				colMin = matrix[k][col];
				break;
			}
		}
		if(colMin == maxRow){
			sign = 1;
			printf("\n第%d行,第%d列的%d是鞍点\n",row , col,maxRow);
		}
	}

	if(sign == 0){
		printf("\n矩阵中无鞍点!\n");
	}

	system("pause");
	return 0;
}

  • 12
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值