2106:decimal system

本文介绍了一个简单的程序,用于将不同进制的数值转换为十进制,并计算这些数值的总和。该程序能够处理从二进制到十进制之间的各种数制,适用于计算机科学学习者理解数制转换原理。
Problem Description
As we know , we always use the decimal system in our common life, even using the computer. If we want to calculate the value that 3 plus 9, we just import 3 and 9.after calculation of computer, we will get the result of 12.
But after learning <<The Principle Of Computer>>,we know that the computer will do the calculation as the following steps:
1 computer change the 3 into binary formality like 11;
2 computer change the 9 into binary formality like 1001;
3 computer plus the two number and get the result 1100;
4 computer change the result into decimal formality like 12;
5 computer export the result;

In the computer system there are other formalities to deal with the number such as hexadecimal. Now I will give several number with a kind of change method, for example, if I give you 1011(2), it means 1011 is a number in the binary system, and 123(10) means 123 if a number in the decimal system. Now I will give you some numbers with any kind of system, you guys should tell me the sum of the number in the decimal system.
 
Input
There will be several cases. The first line of each case contains one integers N, and N means there will be N numbers to import, then there will be N numbers at the next N lines, each line contains a number with such form : X1….Xn.(Y), and 0<=Xi<Y, 1<Y<=10. I promise you that the sum will not exceed the 100000000, and there will be at most 100 cases and the 0<N<=1000.
 
Output
There is only one line output case for each input case, which is the sum of all the number. The sum must be expressed using the decimal system.
 
Sample Input
3
1(2)
2(3)
3(4)

4
11(10)
11(2)
11(3)
11(4)
 
Sample Output
6
23
 

分析:

实际上就是任意进制向十进制转化的问题。

代码如下:

#include <iostream>
using namespace std;

int pow(int m,int n)//求m的n次幂
{
	long int pro = 1;
	while(n--)
		pro *= m;
	return pro;
}

int main()
{
	int T,p,q;//k为指数
	
	char left_arc,right_arc;
	while (cin >> T)
	{	
		int sum = 0;
		while(T--)
		{
			int k = 0;
			cin >> p >> left_arc >> q >> right_arc;

			while (p != 0)
			{
				sum += p % 10 * pow(q,k++);
				p /= 10;
			}		
		}
		cout << sum << endl;
	}

	return 0;
}


type MonthlyProductPerformance struct { gorm.Model CustomerPerformanceID uint `gorm:"index:idx_customer_performance_id;column:customer_performance_id;type:bigint(20) unsigned;not null;comment:客户业绩汇总ID(关联月度客户业绩汇总表)"` StatMonth string `gorm:"column:stat_month;type:varchar(7);not null;comment:统计月份(YYYY-MM)"` CustomerID uint64 `gorm:"column:customer_id;type:bigint(20) unsigned;not null;comment:客户ID"` SystemID uint `gorm:"index:idx_system_id;column:system_id;type:int(11) unsigned;not null;comment:系统ID"` SystemName string `gorm:"column:system_name;type:varchar(128);not null;comment:系统名称(智慧门店系统/订货系统/分账系统/全家桶)"` ProductCurrentARR float64 `gorm:"column:product_current_arr;type:decimal(15,2);not null;default:0.00;comment:本月产品最新ARR(月初ARR)"` ProductHistoricalARR float64 `gorm:"column:product_historical_arr;type:decimal(15,2);not null;default:0.00;comment:历史产品最高ARR(快照)"` ProductMonthEndHistoricalARR float64 `gorm:"column:product_month_end_historical_arr;type:decimal(15,2);not null;default:0.00;comment:历史产品最高ARR(月末)"` ProductMonthEndHistoricalARRUpdated float64 `gorm:"column:product_month_end_historical_arr_updated;type:decimal(15,2);not null;default:0.00;comment:历史产品最高ARR(月末)(修改后)"` ProductNewSignLine float64 `gorm:"column:product_new_sign_line;type:decimal(15,2);not null;default:0.00;comment:产品新签红线"` ProductNewSignLineUpdated float64 `gorm:"column:product_new_sign_line_updated;type:decimal(15,2);not null;default:0.00;comment:产品新签红线(修改后)"` ProductOrderAmount float64 `gorm:"column:product_order_amount;type:decimal(15,2);not null;default:0.00;comment:产品下单金额"` ProductNewSignAmount float64 `gorm:"column:product_new_sign_amount;type:decimal(15,2);not null;default:0.00;comment:产品新签金额"` ProductRenewalAmount float64 `gorm:"column:product_renewal_amount;type:decimal(15,2);not null;default:0.00;comment:产品续期金额"` } type MonthlyCustomerPerformance struct { gorm.Model CustomerID uint64 `gorm:"uniqueIndex:uk_customer_month;column:customer_id;type:bigint(20) unsigned;not null;comment:客户ID"` CustomerName string `gorm:"column:customer_name;type:varchar(128);not null;comment:客户名称"` StatMonth string `gorm:"uniqueIndex:uk_customer_month;index:idx_stat_month;column:stat_month;type:varchar(7);not null;comment:统计月份(YYYY-MM)"` OrderTotalAmount float64 `gorm:"column:order_total_amount;type:decimal(15,2);not null;default:0.00;comment:下单金额"` CustomerNewSignLine float64 `gorm:"column:customer_new_sign_line;type:decimal(15,2);not null;default:0.00;comment:客户新签红线"` CustomerNewSignAmount float64 `gorm:"column:customer_new_sign_amount;type:decimal(15,2);not null;default:0.00;comment:客户新签金额"` CustomerRenewalAmount float64 `gorm:"column:customer_renewal_amount;type:decimal(15,2);not null;default:0.00;comment:客户续期金额"` RefundAmount float64 `gorm:"column:refund_amount;type:decimal(15,2);not null;default:0.00;comment:退款金额"` RefundNewSignAmount float64 `gorm:"column:refund_new_sign_amount;type:decimal(15,2);not null;default:0.00;comment:退款新签金额"` RefundRenewalAmount float64 `gorm:"column:refund_renewal_amount;type:decimal(15,2);not null;default:0.00;comment:退款续期金额"` CustomerCurrentARR float64 `gorm:"column:customer_current_arr;type:decimal(15,2);not null;default:0.00;comment:本月客户最新ARR(月初arr)"` CustomerHistoricalARR float64 `gorm:"column:customer_historical_arr;type:decimal(15,2);not null;default:0.00;comment:历史最高客户ARR(本月快照)"` CustomerMonthEndHistoricalARR float64 `gorm:"column:customer_month_end_historical_arr;type:decimal(15,2);not null;default:0.00;comment:历史最高客户ARR(月末)"` } func (c *Client) UpdateFieldsAndCreatePerformanceAdjustmentHistory(ctx context.Context, customerPerformanceId uint, productPerformanceId uint, adjustmentHistory *PerformanceAdjustmentHistory) error { return c.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error { switch adjustmentHistory.FieldName { case "product_month_end_historical_arr_updated": // 更新月末最高arr // 更新产品的对应值(产品月末最高arr) if err := tx.Model(&MonthlyProductPerformance{}).Where("id = ?", productPerformanceId).Updates(map[string]interface{}{ adjustmentHistory.FieldName: adjustmentHistory.NewValue, "updated_at": time.Now(), }).Error; err != nil { return err } // 更新客户的对应值(客户月末最高arr) if err := tx.Model(&MonthlyCustomerPerformance{}).Where("id = ?", customerPerformanceId). Updates(map[string]interface{}{ "customer_month_end_historical_arr": gorm.Expr("customer_month_end_historical_arr - ?", adjustmentHistory.OldValue-adjustmentHistory.NewValue), "updated_at": time.Now(), }).Error; err != nil { return err } case "product_new_sign_line_updated": // 更新产品红线 // 更新产品的对应值(产品的新签红线、产品新签金额、产品续期金额) if err := tx.Model(&MonthlyProductPerformance{}).Where("id = ?", productPerformanceId).Updates(map[string]interface{}{ adjustmentHistory.FieldName: adjustmentHistory.NewValue, "product_new_sign_amount": clause.Expr{SQL: "product_order_amount - ?", Vars: []interface{}{adjustmentHistory.NewValue}}, "product_renewal_amount": clause.Expr{SQL: "CASE WHEN product_order_amount - (product_order_amount - ?) > 0 THEN product_order_amount - (product_order_amount - ?) ELSE 0 END", Vars: []interface{}{adjustmentHistory.NewValue, adjustmentHistory.NewValue}}, "updated_at": time.Now(), }).Error; err != nil { return err } // 更新客户的对应值(客户新签红线、新签金额、续期金额) } // 创建变更记录 if err := tx.Model(&PerformanceAdjustmentHistory{}).Create(&adjustmentHistory).Error; err != nil { return err } return nil }) } 补全// 更新客户的对应值(客户新签红线、新签金额、续期金额)的逻辑
最新发布
11-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值