【HDOJ】4956 Poor Hanamichi

基本数学题一道,看错位数,当成大数减做了,而且还把方向看反了。所求为最接近l的值。

 1 #include <cstdio>
 2
 3 int f(__int64 x) {
 4     int i, sum;
 5
 6     i = sum = 0;
 7     while (x) {
 8         if (i & 1)
 9             sum -= x%10;
10         else
11             sum += x%10;
12         ++i;
13         x/=10;
14     }
15     return sum;
16 }
17
18 int main() {
19     __int64 l, r, x;
20     bool flg;
21     int t;
22
23     scanf("%d", &t);
24     while (t--) {
25         scanf("%I64d%I64d", &l, &r);
26         for (x=l;;++x)
27             if (x%11 == 3)
28                 break;
29         flg = false;
30         while (x <= r) {
31             if (f(x) != 3) {
32                 flg = true;
33                 break;
34             }
35             x += 11;
36         }
37         if (flg)
38             printf("%I64d\n", x);
39         else
40             printf("-1\n");
41     }
42
43     return 0;
44 }

好歹写出来大数减了,贴上吧。

  1 #include <cstdio>
  2 #include <cstring>
  3 #include <iostream>
  4 #include <string>
  5 using namespace std;
  6
  7 string SRCB="11";
  8 bool stop;
  9
 10 int check(string s) {
 11     int sum=0;
 12     int len = s.length();
 13     int i = 0, j;
 14
 15     while (s[i]) {
 16         --len;
 17         j = s[i] - ‘0‘;
 18         if (len & 1)
 19             sum -= j;
 20         else
 21             sum += j;
 22         ++i;
 23     }
 24     return sum;
 25 }
 26
 27 string Minus(string a,string b) {
 28     string c="",ans="",t;
 29     int  flag=0, k=0;
 30
 31     if(a.length()<b.length()||(a.length()==b.length()&& a.compare(b)<0)) {
 32         t=a;
 33         a=b;
 34         b=t;
 35         stop=true;
 36         return "";
 37     }
 38     int i=a.length()-1,j=b.length()-1;
 39     while(i>=0&&j>=0) {
 40         if(a[i]+flag>b[j]) {
 41
 42           c+=a[i]+flag-b[j]+‘0‘;
 43           flag=0;
 44         } else if (a[i]+flag==b[j]) {
 45             c+=‘0‘;
 46             flag=0;
 47         } else {
 48             c+=(a[i]-‘0‘)+flag+10-(b[j]-‘0‘)+‘0‘;
 49             flag=-1;
 50         }
 51         i--;
 52         j--;
 53         k++;
 54     }
 55     while(i>=0) {
 56         if(a[i]+flag<‘0‘) {
 57             c+=a[i]+flag+10;
 58             flag=-1;
 59         } else {
 60             c+=a[i]+flag;
 61             flag=0;
 62         }
 63         i--,k++;
 64     }
 65     int len=k-1;
 66     while(c[len]==‘0‘ && len>0)
 67         len--;
 68     for(j=0;j<=len;j++)
 69         ans+=c[j];
 70     char tt;
 71     for(i=0,j=ans.length()-1;i<j;i++,j--) {
 72         tt = ans[i];
 73         ans[i] = ans[j];
 74         ans[j]=tt;
 75     }
 76     return ans;
 77 }
 78
 79 int main() {
 80     int t, tmp, ttmp;
 81     string l, r, ans;
 82     bool flag;
 83     char x[3];
 84     //FILE *fout = fopen("data2", "w");
 85
 86     scanf("%d", &t);
 87     while (t--) {
 88         cin >>l>>r;
 89         flag = stop = false;
 90         tmp = check(r);
 91         ttmp = tmp%11;
 92         if (ttmp < 0)
 93             ttmp += 11;
 94         if (ttmp > 3) {
 95             x[0] = ttmp-3+‘0‘;
 96             x[1] = ‘\0‘;
 97             ans = Minus(r, string(x));
 98         } else if (ttmp < 3) {
 99             x[0] = 8+ttmp+‘0‘;
100             x[1] = ‘\0‘;
101             ans = Minus(r, string(x));
102         } else {
103             ans = r;
104         }
105         if (stop) {
106             printf("-1\n");
107             continue;
108         }
109         while (l.length()<ans.length() || (l.length()==ans.length()&&l.compare(ans)<=0)) {
110             if (check(ans) != 3) {
111                 flag = true;
112                 break;
113             }
114             //cout <<ans<<endl;
115             ans = Minus(ans, SRCB);
116             if (stop)
117                 break;
118         }
119         if (flag) {
120             cout <<ans<<endl;
121             //fprintf(fout, "%s\n", ans.data());
122         } else {
123             printf("-1\n");
124             //fprintf(fout, "-1\n");
125         }
126         //fprintf(fout, "%d\n", ans);
127     }
128     //fclose(fout);
129     return 0;
130 }

【HDOJ】4956 Poor Hanamichi,布布扣,bubuko.com

时间: 2024-12-09 19:23:06

【HDOJ】4956 Poor Hanamichi的相关文章

【HDOJ】1099 Lottery

题意超难懂,实则一道概率论的题目.求P(n).P(n) = n*(1+1/2+1/3+1/4+...+1/n).结果如果可以除尽则表示为整数,否则表示为假分数. 1 #include <cstdio> 2 #include <cstring> 3 4 #define MAXN 25 5 6 __int64 buf[MAXN]; 7 8 __int64 gcd(__int64 a, __int64 b) { 9 if (b == 0) return a; 10 else return

【HDOJ】2844 Coins

完全背包. 1 #include <stdio.h> 2 #include <string.h> 3 4 int a[105], c[105]; 5 int n, m; 6 int dp[100005]; 7 8 int mymax(int a, int b) { 9 return a>b ? a:b; 10 } 11 12 void CompletePack(int c) { 13 int i; 14 15 for (i=c; i<=m; ++i) 16 dp[i]

【HDOJ】3509 Buge&#39;s Fibonacci Number Problem

快速矩阵幂,系数矩阵由多个二项分布组成.第1列是(0,(a+b)^k)第2列是(0,(a+b)^(k-1),0)第3列是(0,(a+b)^(k-2),0,0)以此类推. 1 /* 3509 */ 2 #include <iostream> 3 #include <string> 4 #include <map> 5 #include <queue> 6 #include <set> 7 #include <stack> 8 #incl

【HDOJ】1818 It&#39;s not a Bug, It&#39;s a Feature!

状态压缩+优先级bfs. 1 /* 1818 */ 2 #include <iostream> 3 #include <queue> 4 #include <cstdio> 5 #include <cstring> 6 #include <cstdlib> 7 #include <algorithm> 8 using namespace std; 9 10 #define MAXM 105 11 12 typedef struct {

【HDOJ】2424 Gary&#39;s Calculator

大数乘法加法,直接java A了. 1 import java.util.Scanner; 2 import java.math.BigInteger; 3 4 public class Main { 5 public static void main(String[] args) { 6 Scanner cin = new Scanner(System.in); 7 int n; 8 int i, j, k, tmp; 9 int top; 10 boolean flag; 11 int t

hdu 4956 Poor Hanamichi BestCoder Round #5(数学题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4956 Poor Hanamichi Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7    Accepted Submission(s): 4 Problem Description Hanamichi is taking part in

HDU 4956 Poor Hanamichi

HDU 4956 Poor Hanamichi 题目链接 思路:直接从l往上找判断即可 代码: #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; typedef long long ll; int t; ll l, r; bool judge(ll num) { ll flag = 1; ll ans = 0; whi

hdu 4956 Poor Hanamichi 解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4956(它放在题库后面的格式有一点点问题啦,所以就把它粘下来,方便读者观看) 题目意思:给出一个范围 [l, r] 你, 问是否能从中找到一个数证明 Hanamichi’s solution 的解法是错的. Hanamichi’s solution 是这样的: 对于某个数 X,从右往左数它的每一位数字(假设第一位是从0开始数).它 偶数位的数字之和 -  奇数位的数字之和  = 3  而且 这个 X

【HDOJ】2425 Hiking Trip

优先级队列+BFS. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <queue> 5 using namespace std; 6 7 #define MAXN 25 8 9 typedef struct node_st { 10 int x, y, t; 11 node_st() {} 12 node_st(int xx, int yy, int tt)