每日一练(day2)

这篇博客包含了三道编程练习题的解析,分别是关于小Q新式棋盘中行权值和列权值的比较,找到最长连续递增子序列的问题,以及计算贝博士的机械零件中可翻新零件的总数。每道题目都详细描述了问题背景、输入输出要求,并提供了示例测试用例。
摘要由CSDN通过智能技术生成

 1、题目名称:小Q新式棋盘

时间限制:1000ms内存限制:256M

题目描述

已知棋盘大小为n*n。 每个位置都有自己的权值q。 该棋盘中有多少对行权值和小于列权值和。

输入描述:

第一行输入整数n。(1<=n<=100)表示棋盘的大小 以下n行每行输入n个整数表示棋子的权值。(1<=a<=1000)

输出描述:

输出小Q的分值。

示例 

示例1

输入

3
1 2 3
1 2 3
1 2 3

输出

3


#include <stdlib.h>
#include <string.h>
#include<iostream>
#include<vector>
using namespace std;
int n;
int **arr;
void solution() {
	int *row=(int*)malloc(sizeof(int)*n);
	int *col=(int*)malloc(sizeof(int)*n);
	memset(row,0,sizeof(int)*n);
	memset(col,0,sizeof(int)*n);
	for (int i=0;i<n;i++) {
		for (int j=0;j<n;j++) {
			row[i]+=arr[i][j];
			col[j]+=arr[i][j];
		}
	}
	int s=0;
	for (int i=0;i<n;i++) {
		for (int j=0;j<n;j++) {
			//printf("row:%d col:%d row:%d col:%d\n",i,j,row[i],col[j]);
			if(row[i]<col[j]) {
				s++;
			}
		}
	}
	cout<<s<<endl;
}
int main() {
	cin>>n;
	arr = (int**)malloc(n * sizeof(int*));
	for (int i = 0; i < n; i++) {
		arr[i]=(int*)malloc(n * sizeof(int));
		for (int j = 0; j < n; j++) {
			cin>>arr[i][j];
		}
	}
	solution();
	return 0;
}

2、题目名称:Longest Continuous Increasing Subsequence

时间限制:1000ms内存限制:256M

题目描述

Given an unsorted array of integers, find the length of longest continuous increasing subsequence (subarray).(Test cases are for reference only, you will be scored by the quality of code.)

输入描述:

Enter the integer n in the first line. Enter n integers in the second line.

输出描述:

Outputs the length of the longest incremental sequence.

示例 

示例1

输入

5
1 3 5 4 7

输出

3

class Solution {
public:
    int findLengthOfLCIS(vector<int>& nums) {
        int len = nums.size(),ans = 1,cur = 1;
        if(!len)
            return 0;
        else{
            for(int i = 1;i < len;i ++){
                if(nums[i] > nums[i - 1]){
                    ans = max(ans,++ cur);
                }
                else{
                    cur = 1;
                }
            }
        }
        return ans;
    }
};

3、题目名称:贝博士的机械零件

时间限制:1000ms内存限制:256M

题目描述

贝博士是个大忙人,他在设计和制造一台非常复杂的机械式计算机。 最近贝博士有一点烦恼,因为机械零件的种类繁多,磨损又快,经费不太够用了。不过,他发现有一些机械零件在磨损以后,可以用若干同一型号的磨损旧零件重新回炉熔化以后再铸造出一个该型号的新零件,符合这样的特点的机械零件称为可翻新零件。 于是贝博士请来了他的助手艾小姐,请她统计一下有多少种型号的可翻新零件,每一种目前有多少存量又能以多少个旧零件重新回炉铸造出一个新零件,要求计算出对应于每一种机械零件他最终能使用的零件个数。

输入描述:

第一行输入两个数p和q,表示某型号的零件存量为p,且可以用q个旧零件重新回炉铸造出一个新零件。

输出描述:

每一种可翻新机械零件最终可以使用的个数。

示例 

示例1

输入6 6

输出7

#include<bits/stdc++.h>
using namespace std;
 
int main(){
    int p,q,sum=0,m;    //p为开始时手中新零件存量,q个旧零件就可以造出一个新零件
    cin>>p>>q;
    sum = p;            //p是要全部使用掉的,直接让sum=p
    while(p>=q){
        m = p/q;        //计算要造出多少个新零件
        sum += m;       //使用这些新零件
        p = p % q;     //看余下多少旧零件
        p += m;        //注意造出的新零件使用后也是旧零件了
    }
    cout<<sum;
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Code Slacker

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值