输入在1行中分别给出P和A,格式为“Galleon.Sickle.Knut”,其间用1个空格分隔。这里Galleon是[0, 107]]区间内的整数,Sickle是[0,
17)区间内的整数,Knut是[0, 29)区间内的整数。
在一行中用与输入同样的格式输出哈利应该被找的零钱。如果他没带够钱,那么输出的应该是负数。
10.16.27 14.1.28
3.2.1
import java.util.Scanner; public class Main { private static final int[] W = {17*29,29,1}; public static void main(String[] args) { Scanner in = new Scanner(System.in); String[] p = in.next().split("\\."); String[] a = in.next().split("\\."); int[] P = {Integer.parseInt(p[0]),Integer.parseInt(p[1]),Integer.parseInt(p[2])}; int[] A = {Integer.parseInt(a[0]),Integer.parseInt(a[1]),Integer.parseInt(a[2])}; int ta = A[0]*W[0]+A[1]*W[1]+A[2]*W[2]; int tp = P[0]*W[0]+P[1]*W[1]+P[2]*W[2]; int t = ta-tp; if(ta<tp){ System.out.print("-"); t = -t; } System.out.println(t/W[0]+"."+t%W[0]/W[1]+"."+t%W[0]%W[1]/W[2]); } }
#include<stdio.h> int main (){//the shorter,the better. int Pg,Ps,Pk,Ag,As,Ak; for(;~scanf("%d.%d.%d %d.%d.%d",&Pg,&Ps,&Pk,&Ag,&As,&Ak);Ak = Ag*493+As*29+Ak-Pg*493-Ps*29-Pk,As=Ak>=0?1:(Ak=-Ak,-1),printf("%d.%d.%d\n",As*Ak/493,Ak%493/29,Ak%493%29)); }
#include<iostream> #include<string> using namespace std; int main(){ string s1,s2; cin>>s1>>s2; int pos1,pos2,a1,a2,a3,b1,b2,b3; int sum1,sum2,result,c1,c2,c3; pos1=s1.find('.'); a1=stoi(s1.substr(0,pos1)); pos2=s1.find('.',pos1+1); a2=stoi(s1.substr(pos1+1,pos2)); a3=stoi(s1.substr(pos2+1)); pos1=s2.find('.'); b1=stoi(s2.substr(0,pos1)); pos2=s2.find('.',pos1+1); b2=stoi(s2.substr(pos1+1,pos2)); b3=stoi(s2.substr(pos2+1)); sum1=a1*17*29+a2*29+a3; sum2=b1*17*29+b2*29+b3; result=sum2-sum1; c1=result/17/29; c2=result/29%17;; c3=result%29; if(c2<0) c2*=-1; if(c3<0) c3*=-1; cout<<c1<<"."<<c2<<"."<<c3<<endl; return 0; }
import java.util.Scanner; public class Main{ public static void main(String[] args){ int priceTotal,paidTotal; Scanner sc=new Scanner(System.in); String temp1=sc.next(); String temp2=sc.next(); String[] realPrice=temp1.split("\\."); int Galleon=Integer.parseInt(realPrice[0]); int Sickle=Integer.parseInt(realPrice[1]); int Knut=Integer.parseInt(realPrice[2]); priceTotal=17*29*Galleon+29*Sickle+Knut; String[] realPaid=temp2.split("\\."); Galleon=Integer.parseInt(realPaid[0]); Sickle=Integer.parseInt(realPaid[1]); Knut=Integer.parseInt(realPaid[2]); paidTotal=17*29*Galleon+29*Sickle+Knut; int charge=paidTotal-priceTotal; Galleon=charge/(17*29); Sickle=(charge%(17*29))/29; Knut=(charge%(17*29))%29; if(paidTotal<priceTotal){ System.out.print("-"); System.out.print(-Galleon+"."+-Sickle+"."+-Knut); }else{ System.out.println(Galleon+"."+Sickle+"."+Knut); } } }
#include<iostream> #include<string> using namespace std; int main(){ string str_bill, str_pay; int gal_bill, sic_bill, kun_bill; int gal_pay, sic_pay, kun_pay; int sum_bill, sum_pay, sum_deal; cin>>str_bill >> str_pay; sscanf(str_bill.c_str(), "%d.%d.%d", &gal_bill, &sic_bill, &kun_bill); sscanf(str_pay.c_str(), "%d.%d.%d", &gal_pay, &sic_pay, &kun_pay); sum_bill = gal_bill * 17 * 29 + sic_bill * 29 + kun_bill; sum_pay = gal_pay * 17 * 29 + sic_pay * 29 + kun_pay; sum_deal = abs(sum_bill - sum_pay); cout<<(sum_bill >= sum_pay ? "-":"")<<sum_deal/(29*17)<<"."\ <<sum_deal/29%17<<"."<<sum_deal%(29); return 0; }
#include<iostream> using namespace std; int main() { string t1,t2; long long g1,s1,k1,g2,s2,k2; bool neg=false; scanf("%lld.%lld.%lld %lld.%lld.%lld",&g1,&s1,&k1,&g2,&s2,&k2); if(g1>g2||(g1==g2&&s1>s2)||(g1==g2&&s1==s2&&k1>k2)){ swap(g1,g2); swap(s1,s2); swap(k1,k2); neg=true; } long long cnt=0; while(g1<g2||s1<s2||k1<k2) { if(g1<g2-1){ g1++; cnt+=17*29; continue; } if(g1!=g2){ s1++; cnt+=29; if(s1==17){ s1=0; g1++; } continue; } k1++; cnt+=1; if(k1==29){ k1=0; s1++; } } if(neg)cout<<"-"; cout<<cnt/17/29<<"."<<cnt/29%17<<"."<<cnt%29; return 0; }
边界检查,,超过int
类型范围,需要使用long long
类型(注意和long
有区别)。
和1016 程序运行时间
问题的一个类似处理,进制化简。
/* * app=PAT-Basic lang=c++ * https://pintia.cn/problem-sets/994805260223102976/problems/994805284923359232 */ #include <cstdio> using namespace std; int main() { int G[2] = {}, S[2] = {}, K[2] = {}; long long sum1, sum2, sub; scanf("%d.%d.%d %d.%d.%d", &G[0], &S[0], &K[0], &G[1], &S[1], &K[1]); sum1 = G[0] * 17 * 29 + S[0] * 29 + K[0]; sum2 = G[1] * 17 * 29 + S[1] * 29 + K[1]; sub = sum2 - sum1; if (sub < 0){ printf("-"); sub = -sub; } printf("%lld.%lld.%lld", sub / (17 * 29), sub % (17 * 29)/29,sub % 29); return 0; }
#include<iostream>
#include<cstring> #include<string> #include<cstdio> #include<cmath> #include<algorithm> #include<sstream> using namespace std; map<int,int> a; long long d=1000000007; map<int,long long> key; long long digui(int x) { if(key[x]==0) { long long ans1=digui(x-1)%d; long long ans2=digui(x-2)%d; long long ans3=(long long)(double(log10(x))/double(log10(2))); key[x]=(ans1+ans2+ans3)%d; } return key[x]; } int a1,a2,a3,b1,b2,b3; int c1,c2,c3; int f1=0,f2=0; void check() { c3=b3-a3; if(c3<0) { c3+=29; f2=1; } c2=b2-a2-f2; if(c2<0) { c2+=17; f1=1; } c1=b1-a1-f1; } int main(){ char c; cin>>a1>>c>>a2>>c>>a3; cin>>b1>>c>>b2>>c>>b3; //cout<<b3; check(); if(c1<0) { f1=0,f2=0; swap(a1,b1); swap(a2,b2); swap(a3,b3); check(); cout<<'-'<<c1<<'.'<<c2<<'.'<<c3<<endl; } else cout<<c1<<'.'<<c2<<'.'<<c3<<endl; return 0; }
#include<map>
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc=new Scanner(System.in); String p=sc.next(); String a=sc.next(); String[] pMoney=p.split("\\."); String[] aMoney=a.split("\\."); int pTotal=Integer.valueOf(pMoney[0])*17*29+Integer.valueOf(pMoney[1])*29+Integer.valueOf(pMoney[2]); int aTotal=Integer.valueOf(aMoney[0])*17*29+Integer.valueOf(aMoney[1])*29+Integer.valueOf(aMoney[2]); int sight=1; int different=aTotal-pTotal; if(different<0){ different=-different; sight=-sight; } int p1=different/(17*29); int p2=(different-p1*17*29)/29; int p3=(different-p1*17*29-p2*29); p1*=sight; System.out.println(p1+"."+p2+"."+p3); } }
try: while True: string = input().split() pay = list(map(int,string[0].split('.'))) cost = list(map(int,string[1].split('.'))) payNum = (pay[0]*17+pay[1])*29+pay[2] costNum = (cost[0]*17+cost[1])*29+cost[2] remaining = costNum-payNum if remaining<0: sign = "-" remaining = -remaining else: sign = "" result = [0,0,0] result[2] = remaining%29 result[0],result[1] = divmod((remaining//29),17) print(sign+".".join(map(str,result))) except Exception: pass
#include <stdio.h> int main(){ int C, m[6]; scanf("%ld.%ld.%ld %ld.%ld.%ld", &m[0], &m[1], &m[2], &m[3], &m[4], &m[5]); C = m[3]*29*17+m[4]*29+m[5] > (m[0]*29*17+m[1]*29+m[2]) ? m[3]*29*17+m[4]*29+m[5] - (m[0]*29*17+m[1]*29+m[2]) : m[0]*29*17+m[1]*29+m[2] - (m[3]*29*17+m[4]*29+m[5]); printf("%s%d.%d.%d", m[3]*29*17+m[4]*29+m[5] > (m[0]*29*17+m[1]*29+m[2]) ? "" : "-",C / (29 * 17), C / 29 % 17, C % 29); return 0; }
import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO 自动生成的方法存根 Scanner sc=new Scanner(System.in); String str1=sc.next(); String str2=sc.next(); String[] arr1=str1.split("\\."); int a1=Integer.parseInt(arr1[0]); int b1=Integer.parseInt(arr1[1]); int c1=Integer.parseInt(arr1[2]); //System.out.println(a1+" "+b1+" "+c1); String[] arr2=str2.split("\\."); int a2=Integer.parseInt(arr2[0]); int b2=Integer.parseInt(arr2[1]); int c2=Integer.parseInt(arr2[2]); //System.out.println(a2+" "+b2+" "+c2); int num2=(a2*17+b2)*29+c2; int num1=(a1*17+b1)*29+c1; String res=null; if(num2<num1){ res="-"+myFunc(a1,b1,c1,a2,b2,c2); }else{ res=myFunc(a2,b2,c2,a1,b1,c1); } System.out.print(res); } public static String myFunc(int a2,int b2,int c2,int a1,int b1,int c1){ if(c2<c1){ c2+=29; b2-=1; } int c=c2-c1; if(b2<b1){ b2+=17; a2-=1; } int b=b2-b1; int a=a2-a1; return a+"."+b+"."+c; } }
#include <stdio.h> #include <stdlib.h> void swap(int *,int *); int main() { int borrow = 0; int flag = 0; int Gallon1 ,Sike1 ,Kult1 ; int Gallon2 ,Sike2 ,Kult2 ; char ch1,ch2; scanf("%d%c%d%c%d",&Gallon1,&ch1,&Sike1,&ch2,&Kult1);//应付的钱 scanf("%d%c%d%c%d",&Gallon2,&ch1,&Sike2,&ch2,&Kult2);//实际带的钱 if((Gallon1 > Gallon2)||((Gallon1 == Gallon2)&&(Sike1 > Sike2))||((Gallon1 == Gallon2)&&(Sike1 == Sike2)&&(Kult1 > Kult2))) { flag = 1; swap(&Gallon1,&Gallon2); swap(&Sike1,&Sike2); swap(&Kult1,&Kult2); } Kult2 = Kult2 - Kult1; if(Kult2 < 0) { Kult2 += 29; borrow = 1; } Sike2 = Sike2 - Sike1; if(borrow) { borrow = 0; Sike2--; } if(Sike2 < 0) { borrow = 1; Sike2 += 17; } Gallon2 = Gallon2 - Gallon1; if(borrow) { borrow = 0; Gallon2--; } if(flag) printf("-"); printf("%d.%d.%d\n",Gallon2,Sike2,Kult2); return 0; } void swap(int *a,int *b) { int tmp = *a; *a = *b; *b = tmp; }
思路://换算为最低单位数字进行比较,然后再转换位最高单位数组,[注意]scanf的使用算是比较好的了。 #include <iostream> using namespace std; //“十七个银西可(Sickle)兑一个加隆(Galleon), //二十九个纳特(Knut)兑一个西可,很容易。 //Galleon.Sickle.Knut int main() { //Galleon.Sickle.Knut long galleon1,sickle1,knut1,galleon2,sickle2,knut2 = 0; scanf("%ld.%ld.%ld %ld.%ld.%ld",&galleon1,&sickle1,&knut1,&galleon2,&sickle2,&knut2); long sub = galleon2*17*29 + sickle2*29 + knut2 - galleon1*17*29 - sickle1*29 - knut1; sub>0?(1):(cout<<"-",sub=-1*sub); cout << sub/17/29 << "." << sub/29%17 << "." << sub%29; }
#include<bits/stdc++.h> using namespace std; int main() { int a,b,c; //g=s*17,s=k*29; scanf("%d.%d.%d",&a,&b,&c); b+=a*17; c+=b*29; int q,w,e; scanf("%d.%d.%d",&q,&w,&e); w+=q*17; e+=w*29; int temp = e-c; if(temp<0){ cout <<"-"; temp = 0-temp; } //cout <<temp<<endl; b=temp/29; c=temp%29; a=b/17; b=b%17; printf("%d.%d.%d",a,b,c); return 0; }