当前位置:网站首页>1168: Bill (pointer topic)
1168: Bill (pointer topic)
2022-04-21 09:10:00 【Qian Qian】
1168: bill ( Pointer theme )
Title Description
At the end of every month , Xiao Ming will sort out and count the expenditure bill of this month . Nowadays, computers have become popular on university campuses , So Xiao Ming wants the computer to help with this . Smart, just make a program for Xiao Ming to complete this thing .
Input
Multi instance testing . First, enter an integer ncase, Indicates the number of test instances . The input for each test case is as follows :
The first line is an integer n (n<100). And then there was n Line billing information , Each line consists of the name of the thing name And the corresponding cost c form , Length not exceeding 200. There will be one or more spaces in the middle , And there are no spaces at the beginning and end of each line . 0.0 < c < 1000.0.
Output
Each test instance corresponds to one line of output , Total cost of output , Keep one digit after the decimal point .
The sample input Copy
2
1
Buy books 62.28
3
Apple 2.3
Buy clothes for girl friend 260.5
Go to cinema 30
Sample output Copy
62.3
292.8
source / classification
Method 1
#include<stdio.h>
#include<string.h>
#define N 200
/* Accurately find the cost of each item Find the subscript of the first space in reverse order , Again +1, Is where spending begins for example ,56.3 It's a string , First calculate 563, Because there is only one decimal place ,563 Except in 10 that will do */
int main(){
char *str,*c;
int ncase,n,f=0;
double sum=0,money=0,count=1;
str=(char *)malloc(sizeof(char)*N);
scanf("%d",&ncase);
while(ncase--){
scanf("%d ",&n);
sum=0;// Set the total amount to 0
while(n--){
gets(str);
count=1;// Record the number of digits after the decimal point
money=0;
f=0;
// The pointer c Point to price
for(int i=strlen(str)-1;i>=0;i--){
if(str[i]==' ') {
c=&str[i+1];
break;
}
}
// take char conversion double
for(int i=0;c[i]!='\0';i++){
if(c[i]!='.') money=money*10+(c[i]-'0');
if(c[i]=='.') f=1;// Marked with a decimal point
}
// If there is a decimal point, divide it by the corresponding number of digits
if(f==1){
for(int i=strlen(c)-1;i>=0;i--){
if(c[i]=='.') break;
count*=10;
}
}
sum+=money/count;
}
printf("%.1lf\n",sum);
}
return 0;
}
Method 2
#include<stdio.h>
#include<string.h>
#define N 200
/* Skillfully using function strrchr() and sscanf() */
int main(){
char *str,*c;
int ncase,n;
double sum=0,money=0;
str=(char *)malloc(sizeof(char)*N);
scanf("%d",&ncase);
while(ncase--){
scanf("%d ",&n);
sum=0;// Set the total amount to 0
while(n--){
gets(str);
c=strrchr(str,' ');// Go back to the last ' ' stay str The subscript , No return NULL
sscanf(c,"%lf",&money);// If successful, return the number of parameters , Failure returns -1.
sum+=money;
}
printf("%.1lf\n",sum);
}
return 0;
}
版权声明
本文为[Qian Qian]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210906131715.html
边栏推荐
- CC00026. CloudJenkins—————————————
- Uniapp hot update and full package update
- Integrating sdl2 with ffmpeg to realize texture rendering of random blocks
- 深蓝-视觉slam-第六节习题
- Yapi basic use (2022-04-15)
- BUUCTF[HCTF 2018]WarmUp
- About shardingsphere
- Based on ASP Net online flower shopping management
- Note 0104 MySQL advanced - index - Overview
- BUUCTF[ACTF2020 新生赛]Include
猜你喜欢

JS prototype and prototype chain

【ACM】131. Split palindrome string
![[(strongly pushed) Li Hongyi 2021 / 2022 spring machine learning course] unsupervised learning - linear methods](/img/2a/de77caa5516ed34af52c14bbe8dbc7.png)
[(strongly pushed) Li Hongyi 2021 / 2022 spring machine learning course] unsupervised learning - linear methods

Dark blue - Visual slam - Section 6 exercise

CC00026.CloudJenkins—————————————

CC00019. CloudJenkins—————————————

2022 chemical automation control instrument examination exercises and simulation examination

CC10000. CloudJenkins—————————————

Selection of WiFi module for data transmission through industrial control serial port of intelligent gateway of Internet of things

Binary tree knowledge
随机推荐
L2-031 深入虎穴 (25 分)
模拟实现vector
CC00000.CloudJenkins—————————————
Uniapp hot update and full package update
Intranet penetration - proxy penetration - rights lifting - injection - MSF Middleware - domain penetration - log clearing - learning resources
[CTF. Show. Reverse] reverse AK race easydse
1149: combine two of three digits
1149: 组合三位数之二
[ctf.show.reverse] 月饼杯 re1_西北望乡、re2_归心、re3_若无月
1146: eat candy
postman测试Excel文件导入导出功能
CC00026. CloudJenkins—————————————
Actual combat penetration - fofa dirbrute - code audit - construction POC ueditor - decryption - WAF Godzilla
How to improve the ability of programming? What is the most important thing about learning programming? Please see how mortals learn C
kotlin 协程 lanch 详解
1155: 字符串比较 多实例
Redisson简介和整合
ARPU, the user of China Mobile, has re entered the rising channel, and the salary of employees has also increased steadily
Implementation of pure student information linked list system in C Have you learned
1146: 吃糖果