听峰问雨 听峰问雨
首页
导航站
  • 编程语言

    • Python
  • 数据结构与算法
  • 设计模式
  • UVA
  • LeetCode
  • 《Go语言实战》
  • 《Go Web编程》
  • 《算法精粹 经典计算机科学问题的Python实现》
  • 学习
  • 博客搭建
  • 本站

    • 分类
    • 标签
    • 归档
  • 我的

    • 收藏
    • 关于
GitHub (opens new window)

zfprotectors

默默学习er
首页
导航站
  • 编程语言

    • Python
  • 数据结构与算法
  • 设计模式
  • UVA
  • LeetCode
  • 《Go语言实战》
  • 《Go Web编程》
  • 《算法精粹 经典计算机科学问题的Python实现》
  • 学习
  • 博客搭建
  • 本站

    • 分类
    • 标签
    • 归档
  • 我的

    • 收藏
    • 关于
GitHub (opens new window)
  • UVA

    • UVA340 - Master-Mind Hints
    • UVA401 - Palindromes
    • UVA409 - Excuses, Excuses!
    • UVA414 - Machined Surfaces
    • UVA424 - Integer Inquiry
    • UVA445 - Marvelous Mazes
    • UVA457 - Linear Cellular Automata
    • UVA458 - The Decoder
    • UVA465 - Overflow
    • UVA488 - Triangle Wave
    • UVA489 - Hangman Judge
    • UVA490 - Rotating Sentences
    • UVA494 - Kindergarten Counting Game
    • UVA537 - Artificial Intelligence?
      • 问题描述
      • 思路
      • 代码
    • UVA644 - Immediate Decodability
    • UVA694 - The Collatz Sequence
    • UVA748 - Exponentiation
    • UVA10010 - Where's Waldorf?
    • UVA10055 - Hashmat the Brave Warrior
    • UVA10071 - Back to High School Physics
    • UVA10106 - Product
    • UVA10115 - Automatic Editing
    • UVA10250 - The Other Two Trees
    • UVA10300 - Ecological Premium
    • UVA10361 - Automatic Poetry
    • UVA10420 - List of Conquests
    • UVA10494 - If We Were a Child Again
    • UVA10815 - Andy's First Dictionary
    • UVA10878 - Decode the tape
  • LeetCode

  • ACM
  • UVA
zfprotectors
2022-05-18
目录

UVA537 - Artificial Intelligence?

# 问题描述

 输入一系列字符串,从中找到物理的相关的两个变量的值,根据公式求另外一个变量的值。

# 思路

看似简单,实际上在字符串中获取变量的相关信息有点麻烦,巧用常量数组节省多余计算,学习运用sscanf()函数能够很快将字符转换成数值类型进行存储。

# 代码

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
char ch[]="PUI";
char pref[]="mkM";
char unit[]="WVA";
double rate[3]={0.001,1000.00,1000000.00};
double sum[3];
char s[1000];
char pre;
int main(){
    int t,i,j,flag,k;
    int c=1;
    double res;
    scanf("%d",&t);
    getchar();
    while(t--){
        memset(sum,0,sizeof(sum));
        gets(s);
        for(i =0; i< strlen(s); ++i){
            if(s[i+1]=='='){
                for(j=0;ch[j]!=s[i];j++);
                sscanf(&s[i+2],"%lf%c",&sum[j],&pre);
                for(k=0;k<3;k++){
                    if(pre==pref[k]){
                        sum[j]*=rate[k];
                    }
                }
            }
        }
        for(i=0;i<3;i++){
            if(sum[i]==0)
                flag=i;
        }
        //注意关系式之间的转换
        if(flag==0)
            res=sum[2]*sum[1];
        else if(flag==1)
            res=sum[0]/sum[2];
        else
            res=sum[0]/sum[1];
        printf("Problem #%d\n%c=%.2lf%c\n\n",c++,ch[flag],res,unit[flag]);

    }
    return 0;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
编辑 (opens new window)
#ACM#C++#UVA
上次更新: 2022/05/18, 20:28:19
UVA494 - Kindergarten Counting Game
UVA644 - Immediate Decodability

← UVA494 - Kindergarten Counting Game UVA644 - Immediate Decodability→

最近更新
01
LeetCode88 - 合并两个有序数组
06-22
02
LeetCode1 - 两数之和
06-22
03
LeetCode1603 - 设计停车系统
06-21
更多文章>
Theme by Vdoing | Copyright © 2021-2022 zfprotectors | 闽ICP备2021014222号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式