python40分求助!!

P1089 [NOIP2004 提高组] 津津的储蓄计划

作为某Py社长我来帮你看看
by Huo_guo_tu @ 2023-07-20 11:24:15


## 加油,希望你能读完 ```python mother = 0 now = 0 think = [] a = True for i in range(12): think.append(int(input())) for x in think: if(x <= 300): now += (300 - x + now)%100 mother += (300 - x + now) - (300 - x + now)%100 else: now -= x - 300 if(now<0): print(-(think.index(x) + 1)) a = False break if(now >= 100): now -= 100 if(a): now += mother + mother / 5 print(now) ``` 这是你原先的代码,测试出来30分 主要问题在: 第10行 now += (300 - x + now)%100 +=应改成等号; 第11行 mother += (300 - x + now) - (300 - x + now)%100 这里的now不是第10行的now,是已经改变过的now; 简单来说就是第10行(300-x+now)和第11行(300-x+now)不等价; 可以用temp变量,插在第10行前,先将原来的now保存下来,第11行的now改成temp; 即:mother += (300 - x + temp) - (300 - x + temp) % 100 最后一行 因为前一行使用了除法,自动转换为浮点数,最后应该要转整数int; 即:print(int(now)) 包括for循环中在else后面的if毫无意义 ##### 将上述错误点都排除,代码如下: ```python mother = 0 now = 0 think = [] a = True temp = 0 for i in range(12): think.append(int(input())) for x in think: if x <= 300: temp = now now = (300 - x + now) % 100 mother += (300 - x + temp) - (300 - x + temp) % 100 else: now -= x - 300 if now < 0: print(-(think.index(x) + 1)) a = False break if a: now += mother + mother / 5 print(int(now)) ``` ##### 亲测满分 但是其实这样做十分复杂,一是用了index,如果题目出现重复数据且零花钱不够的那个月刚好是后面的重复数据,则会WA; 例:280 315 315 4 5 6 7 8 9 10 11 12 正确答案是-3,而这个程序会输出-2; 因为index搜索的是目标在列表中位置的下标,如果列表中有多个目标,会得出最靠前的目标的下标。 所以本题不要用index,可以试试其他方法; 比如列表think在你的方法中只有增加数据和寻找下标,而你用了for循环却只用在寻找下标,其实可以用think[x]解决。在for循环中用ifelse判断太复杂,其实可以合并; 循环思路:不要for x in think,就直接in range(12),先将零花钱增加300,再扣除预期,如果零花钱小于0就将a设为false,输出月份的负数(-x-1),退出循环结束程序;不符合if就继续,可以套while循环或者你原来的方法,这样一个循环就结束了。然后最后不要忘了int就可以了。 ###### 我写的代码我这里先不给,如果你需要的话就问我要,希望你已经读到最后,加油! ##### 代码框架可以给一下: ```python mother = 0 now = 0 think = [] a = True for i in range(12): think.append(int(input())) # 输入数据 for x in 【①】: now += 300 # 零花钱增加300 now -= 【②】 # 扣除当月预算 if 【③】: # 如果零花钱不够,终止程序,输出负数月 print(【④】) a = False 【⑤】 while 【⑥】: # 只要n>=100就一直存100,易理解 now -= 100 # 移出零花钱 mother += 100 if 【⑦】: now += mother+mother/5 print(【⑧】) ``` #### 希望洛谷可以帮助您变得更强,也祝愿您可以愉快地使用洛谷。——洛谷官方原话
by Huo_guo_tu @ 2023-07-20 11:57:38


@[superAMei](/user/1041478) if我习惯不加括号直接空格,到时候可以重新加上
by Huo_guo_tu @ 2023-07-20 11:59:18


@[Huo_guo_tu](/user/1037637) 谢谢大佬,改好了
by superAMei @ 2023-07-23 14:06:31


```cpp #include<bits/stdc++.h> using namespace std; struct RAINBOW{ void AC(){ int a,b; cin>>a>>b; cout<<a+b; } void WA(){ puts("QWQ"); } void RE(){ cout << (*(int *)NULL) << endl; } void TLE(){ while(1); } long long *magic[10086]; void MLE(){ for(int i = 0;;){ magic[i] = new long long; } } void OLE(){ for(long long i = 1;true; i++) putchar(i % 26 + 'A'); } void UKE(){ puts("hehe AK IOI"); } }Rainbow; int main(){ int n; n=rand()%6; if(n == 1) Rainbow.AC(); if(n == 2) Rainbow.WA(); if(n == 3) Rainbow.RE(); if(n == 4)Rainbow.TLE(); if(n == 5)Rainbow.MLE(); if(n == 6)Rainbow.OLE(); if(n == 7)Rainbow.UKE(); return 0; } ```
by 李翊辰2012 @ 2023-08-02 14:11:05


|