首页 > 其他分享 >一个相同长度的数组,对该数组进行求和

一个相同长度的数组,对该数组进行求和

时间:2023-01-10 15:58:53浏览次数:33  
标签:count 10 求和 len int result 数组 长度 nums1

#include <stdio.h>
#include <math.h>

int length(int arr[], int len) {
	int i;
	for(i = len-1; i >= 0; i--) {
		if(arr[i]) {
			return i + 1;
		}
	}
	return len;
}


void main() {
	/*
		加法运算
	*/
	int nums1[10] = {2, 2, 2};
	int nums2[10] = {9, 8, 7};
	int carry = 0;
	int i, n, count = 1, result = 0;
	int len = sizeof nums1 / sizeof nums1[0];

 	for(i = length(nums1, len) - 1; i >= 0; i--) {
		n = nums1[i] + nums2[i] + carry;
		carry = floor( n/10 );
		
		if(count == 1) {
			result = result + n % 10;
			count *= 10;
			continue;
		}

		result = result + n % 10 * count;
		count *= 10;
	}

	if( carry ) result += count;

	printf("%d\n", result);
}

标签:count,10,求和,len,int,result,数组,长度,nums1
From: https://www.cnblogs.com/anas-kai/p/17040517.html

相关文章

  • LeetCode.209 长度最小的子数组
    1.题目给定一个含有 n 个正整数的数组和一个正整数target。找出该数组中满足其和≥target的长度最小的连续子数组 [numsl,numsl+1,...,numsr-1,numsr],并返回......
  • 数组中有某值就删除没有就加入
    数组中有某值就删除没有就加入代码livedemo点击预览 constarr=[1,2,3,4]constx=5//用indexOf()方法判断constindex=arr.indexOf(x);//index<0:......
  • 561. 数组拆分
    问题描述https://leetcode.cn/problems/array-partition/description/解题思路这个题目很有意思。其中的思想依然是贪心。我们要去想,在一个小的数对中,我们应该怎么样才......
  • PowerShell 读取 Goldengate 进程转为数组插入到sql server中
    在使用oracle Goldengate同步时,有时会忽略了ogg进程的启动。若安装Goldengatemonitor只监控着一两个同步又没必要,所以使用脚本来进行监控查看。为便于可视化,先将ogg......
  • 微信小程序this.setData修改对象、数组中的值
    在微信小程序的[前端开发]中,使用this.setData方法修改data中的值,其格式为:this.setData({'参数名1':值1,'参数名2':值2)}需要注意的是,如果是简单变量,这里的......
  • jQuery核心对象(伪数组,什么时候可以不写绑定文档加载完成的监听$(function(){},each中又
    伪数组相关文档主要是讲了给1.$()【函数】和$.xxx【方法】2.$xxx.yyy()【$xxx是一种常见的给jQuery对象的命名方式】【给对象用的方法】用的函数和方法。绝大部分都......
  • js提取元素中的指定成员组成数组
    js提取元素中的指定成员组成数组一、概念map()方法定义在JavaScript的Array中,它返回一个新的数组,数组中的元素为原始数组调用函数处理后的值。二、语法array.map(fu......
  • golang数组
    目录目录数组特性语法数组内存结构数组声明数组赋值数组指针数组方法数组遍历数组类型扩展1.字符串数组2.结构体数组3.接口数组4.管道数组5.图像解码器数......
  • P2261 [CQOI2007]余数求和
    (昨天发病写题解,结果一看早就截止了)前置知识:数列分块我们先看一个式子\[\sum_{k=1}^{n}\lfloor\sqrtk\rfloor\]化简我们稍微枚举一下,就会发现这样一个性质这......
  • Vue判断数组元素是否为undefined
    问题:现在有这样一个数组,没有第一个元素,如何判断该位置为空控制台输出为undefinde首先尝试array[0]===undefined,可以需要修改为array[0]==="undefinde"这样不行......